Skip to content

✨ feat(sanitize): rename tags during sanitize#563

Merged
gaborbernat merged 3 commits into
tox-dev:mainfrom
gaborbernat:feat/531-transform-tags
Jul 7, 2026
Merged

✨ feat(sanitize): rename tags during sanitize#563
gaborbernat merged 3 commits into
tox-dev:mainfrom
gaborbernat:feat/531-transform-tags

Conversation

@gaborbernat

Copy link
Copy Markdown
Member

Sanitizing could drop or keep an element but not rewrite it, so migrating a sanitize-html config that renames deprecated presentational tags (<b><strong>, <center><div>) had no equivalent. This adds Policy.transform_tags, the port of sanitize-html's transformTags, so a rename happens inside the single sanitize pass rather than as a second edit over the tree.

Map a source tag to a string to rename it, or to a Transform to rename it and add attributes (sanitize-html's simpleTransform). The rename runs at the top of the element walk, before the allowlist reads the tag, and the walk then continues on the renamed element as if the author had written the target. 🔒 That ordering is the safety argument: the allowlist still decides the element's disposition, the unconditional baseline still escapes a script or iframe target, and any injected attribute joins the element's own to be scrubbed by the same gates. {"b": "script"} cannot smuggle a live <script>, and an injected href cannot carry a javascript: URL. Only HTML elements are transformed, matched by tag name.

The rename mutates the node in place through the existing tree primitives (rewrite the name, relookup the atom, preserve the source-closed flag), so a transform adds no extra pass over the document. The migration guide benchmarks the transform op end-to-end against sanitize-html's Node runner over stdin, the cost a Python service pays to reach it as a subprocess.

closes #531

Add Policy.transform_tags, sanitize-html's transformTags: map a source
tag to a target string to rename it, or to a Transform to rename it and
add attributes (simpleTransform). The rename runs before the allowlist
in the same C walk, so the renamed element is re-checked from scratch --
a transform decides an element's name but never its safety. Mapping a
tag to script still drops it, and an added attribute is scrubbed like
the element's own.

closes tox-dev#531
@gaborbernat gaborbernat added the enhancement New feature or request label Jul 7, 2026
@codspeed-hq

codspeed-hq Bot commented Jul 7, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 66 untouched benchmarks
🆕 1 new benchmark
⏩ 18 skipped benchmarks1

Performance Changes

Benchmark BASE HEAD Efficiency
🆕 test_feature[sanitize-transform-spec] N/A 12.7 ms N/A

Comparing gaborbernat:feat/531-transform-tags (65e05a3) with main (e9032d7)

Open in CodSpeed

Footnotes

  1. 18 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@gaborbernat gaborbernat merged commit 50a8866 into tox-dev:main Jul 7, 2026
48 checks passed
@gaborbernat gaborbernat deleted the feat/531-transform-tags branch July 10, 2026 18:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Sanitizer: rename elements during sanitize (transformTags)

1 participant