Skip to content

🐛 fix(encoding): read meta declarations whole and fall through - #2155

Merged
gaborbernat merged 5 commits into
tox-dev:mainfrom
gaborbernat:sec-fix/wq2ubrc9
Nov 15, 2026
Merged

gaborbernat merged 5 commits into
tox-dev:mainfrom
gaborbernat:sec-fix/wq2ubrc9

Conversation

@gaborbernat

@gaborbernat gaborbernat commented Nov 15, 2026 •

Copy link
Copy Markdown
Member

The <meta> encoding detection could pick an encoding that the document does not declare in full. 🌐 It cut long charset and content values before the lookup, so the prefix of a value could name an encoding that the whole value does not. A charset that named no encoding could also hide a valid content declaration, so parse() and detect() could disagree on the same element. After this change both read each meta element the way the tree-construction rules do.

The WHATWG get an encoding step takes the whole label. The tree-construction rule for a meta element tries charset first, and it tries content only when charset names no encoding and http-equiv is Content-Type. The prescan text would stop at a charset that names no encoding. The detector follows the tree rule instead, because the tree rule decides what parse() returns.

Behavior changes

  • A charset value, and the charset= token in content, are judged whole. A label padded with whitespace now resolves, and a value that named an encoding only after being cut is ignored.
  • A charset that names no encoding no longer hides a content declaration on the same element. This applies to parse() and detect().
  • A charset that names an encoding still wins over content.

Fixes #1775.

The <meta> prescan and the tree pass copied a charset value into a
fixed-size buffer before looking it up. A value longer than the buffer
was cut, and the remaining prefix was trimmed of whitespace and matched
as a label. A value that names no encoding could therefore select one,
and a label padded with whitespace could be ignored. parse() and
detect() then disagreed with browsers on such pages.

The lookup now runs on the whole value as each attribute is read, and
the content label stays a span of the value instead of a copy. The
lookup already applies the length limit to the trimmed label, so a
value that does not name an encoding is ignored and a padded label
resolves, as in the WHATWG algorithm.
@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 not alter performance

✅ 582 untouched benchmarks
⏩ 89 skipped benchmarks1


Comparing gaborbernat:sec-fix/wq2ubrc9 (9ecf07b) with main (f5b7d14)

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. ↩

The tree pass recorded a meta element's charset attribute and, when that
attribute had any value, skipped its content value. The tree-construction
rule for meta elements tries content whenever the charset value names no
encoding, so a charset of "" or an unknown label hid a valid
Content-Type declaration from parse(). The prescan handled an empty
charset by reading content, but an unknown label blocked it, so detect()
and parse() could disagree on the same meta.

The prescan and the tree pass now use the same order per meta element: a
charset value that names an encoding wins, otherwise a content value
counts when http-equiv is Content-Type. A failing charset attribute no
longer hides content in either path. The prescan's own text in the
standard lets a failing charset block content, which differs from the
tree-construction rule; the tree rule decides what parse() returns, so
the prescan follows it too.
@gaborbernat gaborbernat changed the title 🐛 fix(encoding): judge meta charset labels on the whole value 🐛 fix(encoding): read meta declarations whole and fall through Nov 15, 2026
@gaborbernat
gaborbernat merged commit 3c0219d into tox-dev:main Nov 15, 2026
30 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.

A long meta charset value is cut to 63 bytes before its whitespace is trimmed

1 participant