fix(classes): treat duplicate attributes as a change

golang.org/x/net/html's tokenizer keeps only the first of repeated
attribute names, so FormatHTML's byte-preserving comparison saw
<a href="https://x" href="javascript:...">, <img src=ok
src="javascript:..."> and a repeated style as unchanged and stored the
raw bytes, and CheckHTML accepted them.

htmlTokens now scans each start tag's raw bytes for its attribute names,
following the tokenizer's own readTag rules, and reports the document as
changed when that list differs from the attributes the tokenizer kept.
Such input is stored as bluemonday's output, which keeps the first value
as browsers do, and CheckHTML refuses it. Input without repeated
attributes is still stored byte for byte. A differential run of 2M
random tags found no disagreement between the scan and the tokenizer.
This commit is contained in:
2026-10-10 00:13:17 +02:00
parent 0d00ec8e0d
commit 5f47083450
4 changed files with 207 additions and 5 deletions

View File

@@ -34,6 +34,11 @@ func TestCheckHTML(t *testing.T) {
{"form", `<form action="https://evil.test"><input type=password></form>`, false},
{"style element", `<style>p { color: red }</style>`, false},
{"comment", `<p>a</p><!-- hidden -->`, false},
{"duplicate href", `<p><a href="https://x" href="javascript:alert(1)">x</a></p>`, false},
{"duplicate src", `<img src=ok src="javascript:alert(1)">`, false},
{"duplicate style", `<p style="color: red" style="position: fixed">x</p>`, false},
{"duplicate of an allowed attribute", `<p class="a" class="a">x</p>`, false},
{"attribute names inside a value", `<p><a href="https://example.test/?src=a&amp;href=b">x</a></p>`, true},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {