Skip to content

fix(sanitizer): merge sanitize rules for the same tag instead of overwriting them - #3041

Open
playlikeboy wants to merge 6 commits into
codex-team:nextfrom
playlikeboy:fix/2501-merge-sanitize-config
Open

playlikeboy wants to merge 6 commits into
codex-team:nextfrom
playlikeboy:fix/2501-merge-sanitize-config

Conversation

@playlikeboy

@playlikeboy playlikeboy commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

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 in BlockToolAdapter.sanitizeConfig when merging a block tool's per-field rule onto baseSanitizeConfig, and in Paste when combining pasteConfig.tags with inline tool configs.

Change

  • New helper mergeSanitizerConfigs(...configs) in src/components/utils/sanitizer.ts that merges rules per tag and per attribute instead of replacing them. Semantics (documented in the JSDoc):
    • true on either side keeps the tag with all attributes;
    • false / undefined yields to the other side;
    • object + object = union of allowed attributes; when both declare the same attribute, two trues stay true, a function and anything else become one combined function, and two different attribute-string constraints become an OR check;
    • truthy non-boolean attribute predicate results are coerced with Boolean(...), matching html-janitor;
    • inputs are never mutated.
  • Extended TagConfig in types/configs/sanitizer-config.d.ts so attribute values accept boolean | string | ((value: string, el: Element) => boolean), removing the casts in sanitizer.ts and inline-tool tests.
  • Applied mergeSanitizerConfigs in BlockToolAdapter.baseSanitizeConfig, BlockToolAdapter.sanitizeConfig, Tools.getAllInlineToolsSanitizeConfig, and Paste (processHTML / processTagData).
  • Rebased/merged next (2.32.0-rc.5), bumped package.json to 2.32.0-rc.6, and updated docs/CHANGELOG.md.

Tests

  • test/cypress/tests/sanitisation.cy.ts: two inline tools declaring different span attributes plus a block tool declaring a per-field span rule; 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) and yarn build pass.

playlikeboy and others added 2 commits November 2, 2026 13:10
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>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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

Comment thread src/components/utils/sanitizer.ts Resolved
…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>
playlikeboy and others added 2 commits November 15, 2026 00:48
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 neSpecc left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix and the thorough tests!

A few notes:

  • src/components/modules/paste.ts:222 and :707 still combine pasteConfig.tags rules with the inline config via Object.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 use mergeSanitizerConfigs there 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 to rc.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(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also, mention this in changelog

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/components/utils/sanitizer.ts Outdated
const merged = { ...a };

Object.keys(b).forEach((attr) => {
merged[attr] = mergeAttributeRules(merged[attr] as AttributeRule, b[attr] as AttributeRule) as boolean | string;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@playlikeboy

Copy link
Copy Markdown
Contributor Author

Thanks for the review @neSpecc! Updated the branch with next and addressed all review notes:

  • Updated BlockToolAdapter.sanitizeConfig (and both Object.assign sites in paste.ts) to use mergeSanitizerConfigs instead of overwriting shared tag rules, and added a test case covering a Block Tool field rule + Inline Tools on span.
  • Extended TagConfig in types/configs/sanitizer-config.d.ts to allow attribute predicate functions (value: string, el: Element) => boolean and cleaned up the casts in sanitizer.ts and the tests.
  • Resolved docs/CHANGELOG.md, bumped package.json to 2.32.0-rc.6, and refreshed the PR description.

@playlikeboy

Copy link
Copy Markdown
Contributor Author

@neSpecc Re-checked #3041: commit fd56dec6 already resolved BlockToolAdapter.sanitizeConfig, pasteConfig.tags, TagConfig types, Boolean(...) coercion, and docs/CHANGELOG.md. Note that the Edge Cypress failure (Error: * is not a valid URL at startup during copy-paste.cy.ts) occurred inside Cypress 13.13.3 on Edge 158 rather than an assertion failure.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Sanitize settings are corrupted if multiple inline tools specify the same tag

3 participants