Fix: importer validates meta fields - #232
Open
DocArmoryTech wants to merge 4 commits into
Open
Conversation
The importer wrote meta-field values without ever validating them. Regexes, values_list and type handlers were all bypassed, so an import could store values the same template rejects through every other route. `marshalData()` builds each meta-field by assigning properties to an empty entity rather than marshalling input through it. Direct assignment skips marshalling, and `save()` applies rules, not validators, so the `validMetaField` rule declared in `MetaFieldsTable::validationDefault()` never ran at any point. The intent to validate was already there but could not fire. `saveMetaFields()` checks `$metaEntity->hasErrors()`, and `hasErrors()` is populated by marshalling - on an entity that was never marshalled it is always empty. As a result, values that fail their template's validation cannot be carried to a newer template version. `migrateMetafieldsToNewestTemplate()` correctly refuses them, so the records are stranded on a superseded version with no non-destructive way forward. This validates each value against its meta-template field as it is assigned, and reports failures the way parent-entity errors are already reported -- listing each one, then aborting before anything is written. `patchEntity()` is deliberately not used: `uuid` is required on create but is only populated by `UUIDBehavior::beforeSave()`, so marshalling would fail on a field the importer is not responsible for supplying. Validating the value directly seemed like the the narrower change. `MetaTemplateFields` are now fetched as entities rather than an `id` list, since validation needs the `type`, `regex` and `values_list`. Adds `--skip-validation`, which downgrades failures to warnings and imports anyway, for operators whose upstream sources are untidier than the template anticipates. Imports that previously succeeded while writing invalid data will otherwise now fail, which is the point, but it is a behaviour change and existing pipelines may depend on the current leniency.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #231
What
cake importerwrote meta-field values without ever validating them. Regexes,values_listand type handlers were all bypassed, so an import could storevalues that the same template rejects through every other route - and that
migrateMetafieldsToNewestTemplate()will later refuse to carry to a newertemplate version.
Cause
marshalData()builds each meta-field by assigning properties to an emptyentity rather than marshalling input through it:
Direct assignment skips marshalling, and
save()applies rules, notvalidators, so the
validMetaFieldrule declared inMetaFieldsTable::validationDefault()never ran at any point.The intent to validate was already present but could not fire:
saveMetaFields()checks$metaEntity->hasErrors(), andhasErrors()ispopulated by marshalling - on an entity that was never marshalled it is always
empty.
Approach
Each value is validated against its meta-template field as it is assigned, via
MetaFieldsTable::isValidMetaFieldForMetaTemplateField(). Failures arereported the way parent-entity errors already are - every failure listed, then
abort before anything is written.
patchEntity()was deliberately not used -uuidis required on create butis only populated by
UUIDBehavior::beforeSave(), so marshalling at this pointfails on a field the importer is not responsible for supplying.
MetaTemplateFieldsare now fetched as entities rather than an id list, sincevalidation needs the
type,regexandvalues_listrather than the primarykey. The existing
$metaTemplateFieldsMappingis derived from them, so thesurrounding code is unchanged.
Behaviour change, and workaround
Imports that previously "succeeded" while writing invalid data will now fail.
That is the intent, but it is a behaviour change and existing pipelines may
depend on the current leniency - particularly where an upstream source is
untidier than the template anticipates.
--skip-validationdowngrades failures to warnings and imports anyway. Theabort message points at it, and both mention that values failing validation
cannot be migrated to a later template version.
Verifying the behaviour
ENISA CSIRT Networkconstrainswebsitetohttps?:\/\/.+.www.example.org../bin/cake importer <config> <source>- before this change it reports success and stores the value; after, it reports the failing value and aborts.--skip-validationrestores the previous behaviour, with warnings.