Skip to content

🐛 fix(extract): read srcset candidates like a browser - #2148

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

gaborbernat merged 8 commits into
tox-dev:mainfrom
gaborbernat:sec-fix/qwlsgixz

Conversation

@gaborbernat

@gaborbernat gaborbernat commented Nov 15, 2026 •

Copy link
Copy Markdown
Member

links(), rewrite_links() and resolve_links() split srcset and imagesrcset candidates differently from a browser. 📷 A comma inside a descriptor's parentheses ended a candidate early, and candidates with invalid descriptors were reported and checked although a browser discards them. A callback could receive a URL the browser never loads, and a sanitizer policy that admits srcset could skip the scheme check of a URL the browser fetches. After this change link extraction and the sanitizer both follow the parse a srcset attribute algorithm.

Link extraction and the sanitizer now share one candidate scanner, so both see the same candidates. The scanner follows the standard's parenthesis rule for descriptors, and it drops a candidate whose descriptors are invalid. The standard's invalid cases include a repeated width, density or height, a width mixed with a density, a zero width, a negative density, and an unknown descriptor.

Behavior changes

  • links(), rewrite_links() and resolve_links() report a candidate that follows a parenthesised descriptor as its own candidate, and pass its URL to the callback.
  • A value with an unclosed parenthesis treats the rest of the value as one descriptor, so the candidates after it are not reported.
  • A candidate with invalid descriptors is no longer reported, rewritten or resolved.
  • A sanitizer Policy that admits srcset or imagesrcset checks the scheme of each candidate a browser keeps, and no longer checks candidates a browser drops.

Fixes #1759.

A srcset descriptor can hold parentheses. HTML's descriptor tokenizer
reads a comma inside them as ordinary text, but the scanner behind
links(), rewrite_links() and resolve_links(), and the sanitizer's srcset
check, ended the candidate at the first comma. The candidate boundaries
then differed from the URLs a browser loads.

Both scanners now skip from an open parenthesis to the next close
parenthesis, and only a comma outside parentheses ends the candidate.
The standard does not nest parentheses, so a second open parenthesis
inside a group is skipped with it. An unclosed parenthesis runs to the
end of the value. The URL scan is unchanged.
@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 9.51%

1 warning

⚠️ 2 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 regressed benchmarks
✅ 580 untouched benchmarks
⏩ 89 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Benchmark BASE HEAD Efficiency
❌ test_feature[query-closest] 237.5 µs 274.5 µs -13.48%
❌ test_feature[select-nth] 438 µs 462.8 µs -5.35%

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/qwlsgixz (2a14856) with main (659fcc5)

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 HTML "parse a srcset attribute" algorithm discards an image candidate
whose descriptors are invalid: a repeated width or density, a width or
density mixed with the other, a height without a width, a zero width or
height, a negative density, a value that is not a number, or an unknown
unit. The link scanner and the sanitizer kept such candidates, so links(),
rewrite_links() and resolve_links() reported URLs that a browser does not
load, and a sanitizer policy checked the scheme of those URLs.

The descriptor check lives in core/srcset.h and both scanners call it, so
they keep the same candidates. A candidate with no descriptor or a single
integer width or density is decided while the scan skips it; any other
descriptor list is tokenized and classified in full.
@gaborbernat gaborbernat changed the title 🐛 fix(extract): end srcset candidates at commas outside parens 🐛 fix(extract): read srcset candidates like a browser Nov 15, 2026
@gaborbernat
gaborbernat merged commit e015135 into tox-dev:main Nov 15, 2026
29 of 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.

links() splits srcset candidates at commas inside descriptor parentheses

1 participant