Repository navigation
Conversation
- Rewrite url() regexp so whitespace and content groups can't overlap; worst-case matching is now linear instead of cubic (url( + 8000 spaces took ~71s before, a few ms now) - Support @import "x" at end of input and @import"x" without space - Don't include trailing characters after quoted @import - Ignore functions whose name only ends with "url" (e.g. my-url()) - Respect backslash escapes so escaped quotes/parens don't end the url - Don't strip comment-like sequences inside strings - Make data URI detection case-insensitive Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Bump ESLint ecmaVersion to 2018 to allow regexp lookbehind (supported by all Node.js versions in the CI matrix). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Unterminated strings now end at newline/end of input and unterminated comments at end of input (as in CSS spec), so every match attempt succeeds and no text is rescanned. Fixes CodeQL js/polynomial-redos - Add regression tests for unterminated strings/comments and CodeQL inputs - Enable mocha env and disable func-names for tests in ESLint config Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Replace the separate comment-stripping pass with one regexp which skips comments, strings and identifiers as whole tokens: - find @import url (...) with whitespace before the parenthesis - do not start strings from escaped quotes outside of strings - do not treat /* inside unquoted url() as a comment - find urls right after a comment - skip invalid unquoted url() with quotes inside as a whole - ignore url() inside strings and attribute selectors - drop regexp lookbehind to keep old runtimes working - deduplicate urls in linear time - lint tests with npm run eslint and fix lint errors in them Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The path could run over following @import rules separated only by comments and was rescanned from each of them. Exclude @ from the path characters. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
- Unroll alternations inside loops: they cost a backtrack frame per character and overflowed the regexp stack on tokens longer than ~8M characters (e.g. embedded fonts) - Skip comments around the value inside url(...). For unquoted value comments have to be separated from it by whitespace - Treat \r and \f as newlines which end a string and escaped \r\n as a single line continuation, so CRLF stylesheets do not desynchronise the tokenizer Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
- Do not return url(...) from @namespace and from @document prelude: these are not resources to load - Skip comments with closing parenthesis inside url(...); invalid url(...) now skips comments as a whole - Throw TypeError for non-string input as before instead of coercing it - Performance test compares time for two input sizes instead of a fixed wall-clock limit Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
aivus
marked this pull request as draft
December 3, 2026 08:30
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
ReDoS (most important)
The
url()branchurl\s*\(\s*(...|(.*?))\s*\)allowed\s*, the lazy(.*?), and the trailing\s*to all consume the same whitespace, so unterminated input backtracked cubically. This package parses untrusted CSS from scraped sites:url(+ 2,000 spacesurl(+ 8,000 spacesurl(N.png)entriesThe regexp is now built from mutually exclusive character classes (unquoted content can't start/end with whitespace), so matching is linear. Deduplication uses a lookup map instead of
indexOf, so many distinct URLs are linear too.Long tokens
Loops in the regexp are unrolled (
[^"\\]*(?:\\.[^"\\]*)*instead of(?:\\.|[^"\\])*), so a long run of plain characters is consumed by a single character class. An alternation inside a loop costs a backtrack frame per character and would throwRangeError: Maximum call stack size exceededon a single token longer than ~8M characters (e.g. an embedded font inurl(data:...)). This is not a bug of the released version, only a constraint on how the new regexp is written; 10M-character tokens are covered by a test.Correctness fixes
@import "a.css"(end of input)[]a.css@import"a.css";[]a.css@import "a.css"screen;"a.css"screena.cssmy-url(evil.png)evil.png[]url(a\).png)a\a\).pngurl("a.png)(unclosed)"a.pngurl("x/*y*/z.png")xz.pngx/*y*/z.pngurl(x/*y*/z.png)(unquoted)xz.pngx/*y*/z.pngcontent:"/*"…url(keep.png)…/* */[]keep.pngurl(DATA:image/png;...)url(a.png) /* url(b.png)(unterminated comment)a.png,b.pnga.png(comment runs to end of input, per CSS spec)content:"see url(foo.png)"foo.png[][data-x="url(q.png)"]q.png[]url(a'b.png)(quote in unquoted url)a'b.png[]@namespace url(http://www.w3.org/1999/xhtml);[]@document url(http://example.com/) { .a{background:url(a.png)} }a.pngBehaviour changes to be aware of
url(...)and@importtext inside ordinary strings and attribute selectors is no longer returned.url()containing a quote (url(a'b.png),url(it's.png)) is invalid per the CSS spec and is now skipped as a whole, up to its closing parenthesis, instead of returning the raw body.url(...)in@namespaceand in the prelude of@document/@-moz-documentis no longer returned: it is not a resource to load. Urls in the rules inside@document { ... }are still returned.TypeError(now explicitly; before it came fromtext.replace).Notes
url(...),@import ...) or a token to skip as a whole: a comment, a string, or an identifier (including backslash escapes). There is no separate comment-stripping pass, so/*inside strings and inside the value ofurl()is not treated as a comment, and escaped quotes in selectors (e.g..content-\[\'\'\]) do not start a string.url(...)are skipped:url("a.png" /* c */),url(/* c */ "a.png"),url( /* c */ a.png ). For an unquoted value the comment has to be separated from it by whitespace;/*glued to the value is a part of it, sourl(http://example.com/*/a.png)andurl(x/*y*/z.png)are returned as is.\n,\ror\f) or end of input, and an unterminated comment at end of input (as in the CSS spec). An escaped\r\nis a single line continuation, so CRLF stylesheets do not desynchronise the tokenizer.my-url(is skipped because the identifier token consumes the whole name. No regexp lookbehind is used, so the supported runtimes do not change.@importand its URL are allowed (@import/**/url(a.css),@import /* c */ "b.css"), as is whitespace before the parenthesis (@import url ("a.css")).@import pathform does not allow@in the path, so a path cannot run across following@importrules and be rescanned from each of them (this was quadratic: ~1.9 s for a 190 KB input, now ~3 ms).@import foo@2x.css;(unquoted, not valid CSS) therefore no longer matches; the quoted form is unaffected.Test plan
npm run eslintclean; it now lintstestas well aslibandindex.jsenv: mocha,func-names: off)🤖 Generated with Claude Code