Skip to content

🐛 fix(sanitize): check each value in the form XML output writes - #2116

Merged
gaborbernat merged 1 commit into
tox-dev:mainfrom
gaborbernat:sec-fix/euy7tjp9
Nov 15, 2026
Merged

gaborbernat merged 1 commit into
tox-dev:mainfrom
gaborbernat:sec-fix/euy7tjp9

Conversation

@gaborbernat

@gaborbernat gaborbernat commented Nov 15, 2026 •

Copy link
Copy Markdown
Member

The sanitizer checked values as written, but the XML serializer drops every character that XML 1.0 cannot hold, and dropping one joins the characters around it. Once written as XML, a passing value could turn into a CSS declaration, a url(), an expression(), a template marker or a <meta> refresh that the same checks refuse in plain form. This reached Policy(xml=True) output, and also trees from sanitize_node() under an HTML-mode policy when written with inner_xml. 🔒 After this change each check keeps a value only when it passes both the written form and the XML form.

Architecture

The CSS scrubber, the template-marker pass, the URL checks, the refresh test and the policy callbacks each judge the value in both forms. Values written by attribute_filter and set_attributes pass through the same checks, and literal attribute_values entries must list both forms. The tree is the same whichever serializer writes it, so the rule applies in both modes. Comments need no change, since the serializer's comment guards already skip the dropped characters.

Behavior changes

  • A value whose XML form a check refuses is now refused, scrubbed or collapsed in HTML output too, where before its HTML form passed. Only values holding a character XML cannot hold change.
  • A value that passes in both forms keeps those characters in HTML output, as before.
  • attribute_predicate is called a second time, without those characters, for a value that holds them, and both calls must return true.

Fixes #1728.

The XML serializer drops each character XML 1.0 cannot hold, which joins
the two characters around it. The sanitizer's checks read the value with
those characters still in place, so a value they cleared could turn into
CSS, a URL, a template marker or a meta refresh they refuse once written
as XML. This hit Policy(xml=True) output, and a sanitize_node() tree from
an HTML-mode policy that is later written with inner_xml.

Every check that reads a value now also judges the form the XML
serializer writes, in both modes, and the value survives only when both
pass. The CSS scrubbers rescrub their output's XML form when it holds such
a character, the template-marker pass pairs delimiters across them, and
the URL, srcset, refresh, value allowlist and predicate checks run on
both forms. HTML output keeps the characters whenever both forms pass.

Fixes tox-dev#1728
@gaborbernat gaborbernat added the bug Something isn't working label Nov 15, 2026
@codspeed

codspeed Bot commented Nov 15, 2026

Copy link
Copy Markdown

Merging this PR will degrade performance by 6.28%

2 warnings

⚠️ 4 benchmarks with a significant change ran on different hardware

Their base and head results were measured on machines with a different CPU, core count or memory, so the changes might not come from the code. Run base and head on the same runner type.

Learn more about Macro Runners

See affected benchmarks

⚠️ 4 benchmarks with a significant change ran in a different software environment

Their base and head results were measured with a different OS or toolchain, such as compiler, runtime or library versions, so the changes might come from this difference rather than from the code. Open an affected benchmark to see what changed.

See affected benchmarks

⚡ 2 improved benchmarks
❌ 2 regressed benchmarks
✅ 578 untouched benchmarks
⏩ 89 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Benchmark BASE HEAD Efficiency
❌ test_mutating_feature[path-xpath-one-cold] 107.3 µs 143.3 µs -25.09%
❌ test_feature[query-closest] 255.6 µs 275.8 µs -7.34%
⚡ test_feature[serialize-inner] 5.8 ms 5.5 ms +5.54%
⚡ test_feature[serialize-inner-indent] 6.2 ms 5.9 ms +5.33%

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing gaborbernat:sec-fix/euy7tjp9 (1ca96e6) with main (8613363)

Open in CodSpeed

Footnotes

  1. 89 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 09fdd39 into tox-dev:main Nov 15, 2026
30 of 31 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

XML output drops control characters after the style checks run, so refused CSS returns

1 participant