Repository navigation
🐛 fix(extract): read srcset candidates like a browser - #2148
Conversation
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.
Merging this PR will degrade performance by 9.51%1 warning
Warning Please fix the performance issues or acknowledge them on CodSpeed. Performance Changes
Tip Investigate this regression by commenting Comparing Footnotes
|
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.
links(),rewrite_links()andresolve_links()splitsrcsetandimagesrcsetcandidates 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 admitssrcsetcould 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()andresolve_links()report a candidate that follows a parenthesised descriptor as its own candidate, and pass its URL to the callback.Policythat admitssrcsetorimagesrcsetchecks the scheme of each candidate a browser keeps, and no longer checks candidates a browser drops.Fixes #1759.