Repository navigation
fix(sanitizer): merge sanitize rules for the same tag instead of overwriting them - #3041
playlikeboy wants to merge 6 commits into
Conversation
When several Inline Tools (or Block Tunes) declare sanitize rules for the same tag, the configs were combined with Object.assign, so the last tool's rule replaced the previous ones. Two tools that both wrap text in <span> with different allowed attributes lost one set of attributes on save. Add mergeSanitizerConfigs() which combines rules per tag: boolean, attribute object and function rules are merged so that an attribute allowed by any contributor stays allowed. Use it in BlockToolAdapter.baseSanitizeConfig and Tools.getAllInlineToolsSanitizeConfig. Closes codex-team#2501 Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
There was a problem hiding this comment.
🟡 Changes recommended
The strict comparison in sanitizer.ts rejects truthy non-boolean predicate results, potentially stripping previously allowed attributes when rules are merged; boolean coercion and a regression test are needed.
1 open issue
What changed in this PR
This PR addresses #2501 by merging sanitizer rules from tools that share an HTML tag, preventing one tool’s allowed attributes from overwriting another’s.
Changes:
- Adds per-tag and per-attribute merging and applies it to both sanitizer configuration collectors.
- Adds unit and save-path regression tests for shared-tag rules.
- Updates the release version and changelog.
| File | Description |
|---|---|
test/cypress/tests/utils/sanitizer.cy.ts |
Adds 11 cases covering merge semantics. |
test/cypress/tests/sanitisation.cy.ts |
Tests saving attributes allowed by multiple inline tools. |
src/components/utils/sanitizer.ts |
Adds the shared sanitizer configuration merge helper. |
src/components/tools/block.ts |
Merges inline-tool and tune sanitizer rules. |
src/components/modules/tools.ts |
Merges sanitizer rules across all inline tools. |
package.json |
Bumps the version to 2.32.0-rc.4. |
docs/CHANGELOG.md |
Documents the shared-tag sanitizer fix. |
Review effort: Balanced
…te predicates html-janitor allows an attribute when its predicate returns any truthy value, so the combined rule produced by mergeSanitizerConfigs must not require a strict boolean true. Adds a regression test with a predicate that returns a RegExp match array. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Bump the version to 2.32.0-rc.5, as next is now at rc.4. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
neSpecc
left a comment
There was a problem hiding this comment.
Thanks for the fix and the thorough tests!
A few notes:
src/components/modules/paste.ts:222and:707still combinepasteConfig.tagsrules with the inline config viaObject.assign, so a paste-config rule for a tag is dropped when an inline tool declares the same tag. Rare in practice, but it'd be nice to usemergeSanitizerConfigsthere too for consistency (optional).- The PR description is a bit stale: it says the version was bumped to
2.32.0-rc.4, while the diff bumps torc.5(which is correct). Also, "as discussed on the issue" — there was no maintainer reply in #2501; the decision on the block rule precedence is in the inline comment below.
| .forEach(tune => Object.assign(baseConfig, tune.sanitizeConfig)); | ||
|
|
||
| return baseConfig; | ||
| return mergeSanitizerConfigs( |
There was a problem hiding this comment.
The same problem remains a few lines above, in sanitizeConfig (line 191): Object.assign({}, baseConfig, rule) lets the Block Tool's own field rule overwrite the merged inline rules for the same tag (e.g. text: { span: { class: true } } drops inline tools' span attributes). Let's use mergeSanitizerConfigs(baseConfig, rule) there as well, ideally with a test.
There was a problem hiding this comment.
also, mention this in changelog
There was a problem hiding this comment.
Addressed in fd56dec6: BlockToolAdapter.sanitizeConfig now calls mergeSanitizerConfigs(baseConfig, rule) (with test coverage in sanitisation.cy.ts) and docs/CHANGELOG.md documents Inline Tools, Block Tool field rules, and paste configs.
| const merged = { ...a }; | ||
|
|
||
| Object.keys(b).forEach((attr) => { | ||
| merged[attr] = mergeAttributeRules(merged[attr] as AttributeRule, b[attr] as AttributeRule) as boolean | string; |
There was a problem hiding this comment.
Re your note about types: yes, let's extend TagConfig in types/configs/sanitizer-config.d.ts so attribute values can be boolean | string | ((value: string, el: Element) => boolean) (html-janitor supports it). That removes the casts here and on line 111, and the as unknown as SanitizerConfig in tests.
|
Thanks for the review @neSpecc! Updated the branch with
|
|
@neSpecc Re-checked #3041: commit |

Resolves #2501
Problem
When several Inline Tools declare a sanitize rule for the same tag, the rules were combined with
Object.assign, so the last tool to register silently overwrote the others. The same overwriting occurred inBlockToolAdapter.sanitizeConfigwhen merging a block tool's per-field rule ontobaseSanitizeConfig, and inPastewhen combiningpasteConfig.tagswith inline tool configs.Change
mergeSanitizerConfigs(...configs)insrc/components/utils/sanitizer.tsthat merges rules per tag and per attribute instead of replacing them. Semantics (documented in the JSDoc):trueon either side keeps the tag with all attributes;false/undefinedyields to the other side;trues staytrue, a function and anything else become one combined function, and two different attribute-string constraints become an OR check;Boolean(...), matching html-janitor;TagConfigintypes/configs/sanitizer-config.d.tsso attribute values acceptboolean | string | ((value: string, el: Element) => boolean), removing the casts insanitizer.tsand inline-tool tests.mergeSanitizerConfigsinBlockToolAdapter.baseSanitizeConfig,BlockToolAdapter.sanitizeConfig,Tools.getAllInlineToolsSanitizeConfig, andPaste(processHTML/processTagData).next(2.32.0-rc.5), bumpedpackage.jsonto2.32.0-rc.6, and updateddocs/CHANGELOG.md.Tests
test/cypress/tests/sanitisation.cy.ts: two inline tools declaring differentspanattributes plus a block tool declaring a per-fieldspanrule; all allowed attributes survive.test/cypress/tests/utils/sanitizer.cy.ts: unit cases covering merge semantics including truthy non-boolean predicate results, function+object rules, and undefined configs.Locally:
yarn lint(0 errors) andyarn buildpass.