[6.x] Reject self-referencing Content Block fields at save time#19225
Open
brianjhanson wants to merge 1 commit into
Open
[6.x] Reject self-referencing Content Block fields at save time#19225brianjhanson wants to merge 1 commit into
brianjhanson wants to merge 1 commit into
Conversation
ContentBlock::ensureNoRecursion() resolved nested fields from the DB via the field layout's custom fields, so on a field's first save a nested layout element referencing the field's own UID couldn't be resolved, the FieldNotFoundException was swallowed, and the self-reference passed validation — persisting a field whose nested layout references itself. Iterate the layout's CustomField elements directly instead, comparing each element's raw fieldUid against the field's own uid when the nested field can't be resolved yet, and track ancestor UIDs through the recursion so indirect cycles are caught at any depth and already- corrupted data can't cause infinite recursion. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
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.
What changed
ContentBlock::ensureNoRecursion()now iterates the nested layout'sCustomFieldlayout elements directly instead ofFieldLayout::getCustomFields():FieldNotFoundException), the element's rawfieldUidis compared against the field's ownuid, catching self-references before the field row exists.idcomparison is kept (now null-guarded so two unsaved fields don't false-positive), plus auidcomparison.Why
Recursion validation had a first-save blind spot:
ensureNoRecursion()relied on resolving nested fields viaFields::getFieldByUid(). On a field's first save the row doesn't exist yet, so a self-referencingfieldUidin the nested layout threwFieldNotFoundException, which was swallowed — and the self-reference passed validation. This actually happened in dev: a saved field's settings JSON contained its own UID as a nestedCustomFieldfieldUid. Upstream Craft 5 rejects this with "Including a Content Block field recursively is not allowed."Tests
Three new Pest feature tests in
tests/Feature/Field/FieldsTest.php(written test-first; the first one reproduced the bug before the fix):Notes for reviewers
FieldLayout::getAvailableCustomFields()) doesn't exclude the field being edited — same as upstream Craft 5, which also relies on save-time rejection. Hiding it from the picker is a possible follow-up UX improvement, tracked separately.setFieldLayouts()), which makes loading corrupted rows safe; this PR stops the corruption from being saved in the first place. The two touch different regions of the same files and merge cleanly.🤖 Generated with Claude Code