You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Ignore arbitrary values that start a CSS comment - #20690
The /* starts a comment that never ends. Browsers ignore the CSS up to the next */, and Lightning CSS drops all the rules after it. So one stray candidate in a scanned file (for example in a code sample or a comment) could silently remove other utilities, like .underline here, from the output.
isValidArbitrary now rejects a /* outside of a string or an unquoted url(…). This applies to arbitrary values, arbitrary properties, arbitrary modifiers and arbitrary variants, because they all go through this function. A complete comment like bg-[red/*x*/] is valid CSS, but no candidate needs one, so we reject it too and keep the check simple. A /* inside of a string stays valid, for example content-['/*'] or bg-[url('/*.png')]. An escaped \/* is rejected too, because PostCSS reads it as the start of a comment.
A /* in an unquoted url(…)
In CSS, an unquoted url(…) is a single token, so a /* inside of it is not a comment. These stay valid:
bg-[url(/a/*.png)]
bg-[url(a.png),_url(/b/*.png)]
bg-[image-set(url(/a/*.png)_1x)]
bg-[no-repeat_url(/a/*.png)]
bg-[image:url(/a/*.png)]
[background-image:url(/a/*.png)]
The PostCSS tokenizer reads a url(…) only in some positions. So we allow a /* only in a lowercase url( that is not followed by whitespace, and that starts the value, or comes after whitespace (or an _), a ( or a type hint like image:. This only applies to values that end up in a declaration (utility values and arbitrary properties), not to modifiers and variants. Other forms like URL(/a/*.png), #url(/a/*.png) or @image:url(/a/*.png) are rejected.
The gradient utilities now print a space after the , in var(--tw-gradient-stops, …), so PostCSS does not read ,url( as one word.
Known limitation: a minifier like Lightning CSS removes the space after a ,, and PostCSS then reads ,url(/b/*.png) as the start of a comment. This also happens for plain CSS like url(a.png), url("/b/*.png"), and is reported upstream in postcss/postcss#2194.
Test plan
Added bg-[red/*], bg-[red/*comment*/], [color:red/*], bg-red-500/[50%/*], [&/*]:flex and data-[a/*]:flex to the invalid candidates in candidate.test.ts. They fail without this change.
Added the unsafe url(…) forms to the invalid candidates, and tests that the safe forms above still parse.
Added a test that content-['/*'] still parses.
Added a test with 5000 seeded random arbitrary values. Each generated CSS goes through Lightning CSS, and a rule after it must survive.
Built bg-[red/*] flex underline [color:red/*] and ran the result through Lightning CSS. Before: all rules after .bg-\[red\/\*\] were gone, .underline too. After: .flex and .underline are there, and the invalid candidates are ignored.
It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.
No actionable comments were generated in the recent review.
Walkthrough
The arbitrary-value validator now rejects comment openers outside strings, unescaped newlines, and unterminated quoted strings. It also handles quoted and unquoted URL arguments, including uppercase URL(…) forms. Arbitrary-property parsing rejects invalid property names. Tests cover invalid comment-open sequences and confirm that quoted comment openers remain in parsed values. The changelog documents the fix.
Merge Risk: Moderate · previous review a1462
The change now rejects most ways an arbitrary value can leave an unterminated CSS comment. One bypass remains: an escaped url function name combined with a quote can still produce a comment that drops later generated rules. Fix it before merge or explicitly accept it.
Pre-merge checks: 4
Check name
Status
Explanation
Title check
The title clearly summarizes the main change: rejecting arbitrary values that could start a CSS comment.
Linked Issues check
Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check
Check skipped because no linked issues were found for this pull request.
Description check
The description explains how the changes reject unsafe comment openers in arbitrary candidates, preserve valid strings and unquoted URL arguments, and test the behavior.
Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.
The reason will be displayed to describe this comment to others. Learn more.
Caution
Some comments are outside the diff and can’t be posted inline due to GitHub limitations.
⚠️ Outside diff range comments (2)
Major · Reject unescaped newlines in quoted values. · is-valid-arbitrary.ts:48-49
packages/tailwindcss/src/utils/is-valid-arbitrary.ts:48-49 Major · Functional Correctness · Quick win
Reject unescaped newlines in quoted values.
The public compile().build() API can receive content-['a\n/*'] with an actual newline. This loop treats /* as part of the string, but CSS ends the string at the newline. The emitted /* can then comment out later rules. Reject unescaped newlines in strings before allowing their contents through this check. (w3.org)
Major · Reject comment starts in arbitrary property names · candidate.ts:395-440
packages/tailwindcss/src/candidate.ts:395-440 Major · Functional Correctness · Quick win
Reject comment starts in arbitrary property names
[a/*b:red] passes parseCandidate and emits:
.\[a\/\*b\:red\]{a/*b: red;}
The unclosed comment can consume subsequent CSS rules. Validate the property name for comment starts before yielding the candidate.
Review info
Run configuration
Source mode: Auto
Repository: Unavailable
Branch: Unavailable
File: No configuration file from this repository applied
Commit: Unavailable
Layers: Repository UI
Review profile: CHILL
Plan: Advanced
Run ID: 16d5690b-985f-4a98-be0f-77bb7b179e99
Commits
Reviewing files that changed from the base of the PR and between 3603b3a and 4891445.
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.
The previous commit checked the value of an arbitrary property, but not
its name. `[a/*b:red]` still emitted `a/*b: red;`, and the comment
removed the CSS after it.
The name now goes through `isValidArbitrary` too. The scanner only
extracts names with `a-z`, `-` or a `--custom` property, but other paths
can reach the parser: the browser build, `@source inline()` and direct
calls to `build()`.
Reported by CodeRabbit on #20690.
CSS ends a string at an unescaped newline. `isValidArbitrary` treated the
rest of the input as part of the string, so `content-['a\n/*']` passed
the check, and the `/*` started a real comment in the output.
A newline, carriage return or form feed in a string now makes the value
invalid. The scanner never extracts a candidate with whitespace, so only
direct calls to `build()` can reach this.
Reported by CodeRabbit on #20690.
CodeRabbit reported two findings outside the diff. Both were correct, and I fixed them:
Comment starts in arbitrary property names (candidate.ts): fixed in e73e72d. The property name now goes through isValidArbitrary too, so [a/*b:red] is ignored. The Oxide scanner only extracts property names with a-z, - or a --custom property, but the browser build, @source inline() and direct build() calls can still reach the parser.
Unescaped newlines in quoted values (is-valid-arbitrary.ts): fixed in 23524ee. A newline, carriage return or form feed in a string now makes the value invalid, so content-['a\n/*'] is ignored. The scanner never extracts a candidate with whitespace, so only direct build() calls could reach this.
Both cases are now in the invalid candidates of candidate.test.ts.
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
Caution
Some comments are outside the diff and can’t be posted inline due to GitHub limitations.
⚠️ Outside diff range comments (1)
Major · Handle quotes inside unquoted URLs. · is-valid-arbitrary.ts:48-49
packages/tailwindcss/src/utils/is-valid-arbitrary.ts:48-49 Major · Functional Correctness · Heavy lift
Handle quotes inside unquoted URLs.
[x:url(foo'bar)/*')] passes validation because this scanner treats the quotes as a string. CSS instead treats the first quote as a bad-URL error, resumes after ), and starts a comment at /*. The compiled declaration can therefore comment out later rules. Reject this malformed URL or track CSS URL-token state when scanning. (drafts.csswg.org)
Review info
Run configuration
Source mode: Auto
Repository: Unavailable
Branch: Unavailable
File: No configuration file from this repository applied
Commit: Unavailable
Layers: Repository UI
Review profile: CHILL
Plan: Advanced
Run ID: 16cdec05-e037-4f3f-8004-3472b733544b
Commits
Reviewing files that changed from the base of the PR and between 4891445 and 23524ee.
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 6 remain after this review.
`isValidArbitrary` accepted a string without a closing quote. In the
generated CSS, that quote can pair up with a quote that comes after it.
`[a':'/*']` has the property `a'` and the value `'/*'`, and becomes
`a': '/*';`. The `/*` is then outside of a string, and starts a comment.
A string without a closing quote now makes the input invalid. This
applies to property names and values.
Reported by CodeRabbit on #20690.
CSS reads an unquoted `url(…)` as a single token that ends at the first
`)`. A quote in it does not start a string, it makes a bad URL.
`isValidArbitrary` treated the quote as the start of a string, so
`bg-[url(foo'bar)/*')]` passed the check. In the generated CSS, the `/*`
after the first `)` started a comment.
In an unquoted `url(…)`, a quote, a `(` or a `/*` now makes the value
invalid, and so does a missing `)`. A quoted `url('…')` stays a normal
function with a string in it, so `bg-[url('/*.png')]` stays valid.
Reported by CodeRabbit on #20690.
`decodeArbitraryValue` keeps the `_` in a lowercase `url(…)` only. In
`URL(…)`, the `_` becomes a space. So `bg-[URL(_'image.png')]` becomes the
valid `URL( 'image.png')`, but the previous commit read it as an unquoted
URL with a quote in it, and rejected it.
`isUnquotedUrl` now skips an `_` like whitespace when the function name
is not a lowercase `url`, the same as the decoder.
Reported by Greptile on #20690.
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 4 remain after this review.
CSS decodes escapes in a function name, so `u\72l(…)` is a `url(…)`. The
`url(…)` check compares the name as written, so
`bg-[u\72l(foo'bar)/*')]` passed as a normal function with a string in
it. CSS reads it as an unquoted URL that ends at the first `)`, and the
`/*` after it started a comment.
`isValidArbitrary` now rejects a `(` after a word with a CSS escape in
it. An escaped name like `u\72l('a.png')` is valid CSS, but no candidate
needs one.
- A hex escape takes up to 6 hex digits and one whitespace after them,
and an `_` becomes that whitespace. So `u\72_l(…)` is a `url(…)` too.
- `\_` is a literal `_` in a candidate, not a CSS escape, so
`w-[--my\_fn(1px)]` stays valid.
- An unescaped `_` ends the word, because it becomes a space, so
`bg-[\#fff_url(a.png)]` stays valid.
Reported by CodeRabbit on #20690.
The previous commit checked the value of an arbitrary property, but not
its name. `[a/*b:red]` still emitted `a/*b: red;`, and the comment
removed the CSS after it.
The name now goes through `isValidArbitrary` too. The scanner only
extracts names with `a-z`, `-` or a `--custom` property, but other paths
can reach the parser: the browser build, `@source inline()` and direct
calls to `build()`.
Reported by CodeRabbit on #20690.
CSS ends a string at an unescaped newline. `isValidArbitrary` treated the
rest of the input as part of the string, so `content-['a\n/*']` passed
the check, and the `/*` started a real comment in the output.
A newline, carriage return or form feed in a string now makes the value
invalid. The scanner never extracts a candidate with whitespace, so only
direct calls to `build()` can reach this.
Reported by CodeRabbit on #20690.
`isValidArbitrary` accepted a string without a closing quote. In the
generated CSS, that quote can pair up with a quote that comes after it.
`[a':'/*']` has the property `a'` and the value `'/*'`, and becomes
`a': '/*';`. The `/*` is then outside of a string, and starts a comment.
A string without a closing quote now makes the input invalid. This
applies to property names and values.
Reported by CodeRabbit on #20690.
CSS reads an unquoted `url(…)` as a single token that ends at the first
`)`. A quote in it does not start a string, it makes a bad URL.
`isValidArbitrary` treated the quote as the start of a string, so
`bg-[url(foo'bar)/*')]` passed the check. In the generated CSS, the `/*`
after the first `)` started a comment.
In an unquoted `url(…)`, a quote, a `(` or a `/*` now makes the value
invalid, and so does a missing `)`. A quoted `url('…')` stays a normal
function with a string in it, so `bg-[url('/*.png')]` stays valid.
Reported by CodeRabbit on #20690.
`decodeArbitraryValue` keeps the `_` in a lowercase `url(…)` only. In
`URL(…)`, the `_` becomes a space. So `bg-[URL(_'image.png')]` becomes the
valid `URL( 'image.png')`, but the previous commit read it as an unquoted
URL with a quote in it, and rejected it.
`isUnquotedUrl` now skips an `_` like whitespace when the function name
is not a lowercase `url`, the same as the decoder.
Reported by Greptile on #20690.
CSS decodes escapes in a function name, so `u\72l(…)` is a `url(…)`. The
`url(…)` check compares the name as written, so
`bg-[u\72l(foo'bar)/*')]` passed as a normal function with a string in
it. CSS reads it as an unquoted URL that ends at the first `)`, and the
`/*` after it started a comment.
`isValidArbitrary` now rejects a `(` after a word with a CSS escape in
it. An escaped name like `u\72l('a.png')` is valid CSS, but no candidate
needs one.
- A hex escape takes up to 6 hex digits and one whitespace after them,
and an `_` becomes that whitespace. So `u\72_l(…)` is a `url(…)` too.
- `\_` is a literal `_` in a candidate, not a CSS escape, so
`w-[--my\_fn(1px)]` stays valid.
- An unescaped `_` ends the word, because it becomes a space, so
`bg-[\#fff_url(a.png)]` stays valid.
Reported by CodeRabbit on #20690.
`canonicalizeCandidates` now replaces CSS escapes in `[…]` with the
character that they stand for, if the escape is not needed:
```
bg-[u\72l('a.png')] → bg-[url('a.png')]
bg-[u\72_l('a.png')] → bg-[url('a.png')]
[\63olor:red] → text-[red]
[&_.f\6f_o]:flex → [&_.foo]:flex
```
This covers arbitrary values, arbitrary properties, arbitrary variants
and arbitrary modifiers (everything in `[…]`). It does not cover the
`(…)` shorthand, like `bg-(--my\-color)`.
## What we replace
Escapes of ASCII letters, digits and `-` in a name, and only if the CSS
stays the same. A hex escape has up to 6 hex digits and one optional
whitespace after them. In a candidate, an `_` is that whitespace.
## What we keep
- `\_`, because it is a literal `_` in a candidate.
- Escapes in strings, e.g. `content-['\61']`.
- Escapes of characters that are not name characters, e.g. `\,`, `\)` or
`\2c`.
- Escapes that make a name out of something else. E.g.: `\31_0` is the
name `10`, but `10` is a number. `1\65_3` is `1` with the unit `e3`, but
`1e3` is `1000`. `\-1` is a name, but `-1` is a number.
- An escape if a hex digit comes after the escape before it. E.g. the
`\61` in `\31\61`, because `\31a` is one escape.
- An escape if `-->` or `<!--` would take part of the name, e.g. `-\->`.
- A hex escape with an `_` after it, if the `_` might not be a space.
`decodeArbitraryValue` keeps an `_` in a `url(…)` and in the first
argument of `var(…)` and `theme(…)`, and an arbitrary property name is
not decoded at all. E.g. `bg-[url(\61_b.png)]` stays as it is.
- The escapes in a function name that would become `url`, `var` or
`theme`, if the function has an `_` in it. E.g. `bg-[u\72l(a_b.png)]`
stays as it is, because the `_` would not be a space anymore in
`url(a_b.png)`.
Tailwind reads function names as written, so `c\61lc(1+2)` gets no math
operator spacing today, but `calc(1+2)` does. CSS reads both as
`calc(…)`, so the new candidate does what the escaped one was meant to
do.
## Interaction with the PR that ignores CSS comments
#20690 rejects a
function name with a CSS escape in it, like `bg-[u\72l('a.png')]`,
because `u\72l(foo'bar)/*')` could start a CSS comment. After it merges,
such a candidate does not parse, and canonicalization could not suggest
anything.
So this replacement runs on the raw candidate, before the parse. We use
the new candidate only if it parses, and canonicalize it further from
there. I tested both cases:
| Candidate | `main` + this PR | With
#20690 + this PR |
| --- | --- | --- |
| `bg-[u\72l('a.png')]` | parses, → `bg-[url('a.png')]` | does not
parse, → `bg-[url('a.png')]` |
| `bg-[u\72_l('a.png')]` | parses, → `bg-[url('a.png')]` | does not
parse, → `bg-[url('a.png')]` |
| `bg-[u\72l(foo'bar)/*')]` | parses, → `bg-[url(foo'bar)/*')]` | does
not parse, stays as it is |
| `bg-[u\72l(a_b.png)]` | parses, stays as it is | does not parse, stays
as it is |
On `main`, the third candidate already produces the comment, with or
without this PR. With
#20690, the new
candidate does not parse either, so we keep the old one.
The two PRs do not change the same files, and can merge in any order.
## Test plan
- Example tests in `canonicalize-candidates.test.ts`, for each strategy
(default, with a variant, important, prefix).
- A seeded random test: it builds 20,000 values from escaped and plain
pieces, in a value, a property name and a selector. It compares the CSS
before and after, minified by Lightning CSS, which decodes escapes and
keeps only the needed ones. When I turn off one of the rules, this test
fails. It found the `<!--` case. I also ran it with 4 other seeds and
200,000 values each.
- With the commits of
#20690 cherry-picked,
`canonicalize-candidates.test.ts` and `candidate.test.ts` pass.
- `pnpm run build`, and all tests of `packages/tailwindcss`.
A `/*` outside of a string in an arbitrary value, property, modifier or
variant (e.g. `bg-[red/*]`) ends up in the generated CSS as the start of
a comment. When it is not closed, it comments out all the CSS that comes
after it, so one stray candidate in a scanned file removes other
utilities from the output.
`isValidArbitrary` now rejects each `/*` outside of a string. A complete
comment is valid CSS, but no candidate needs one. A `/*` in a string,
like `content-['/*']`, stays valid.
The previous commit checked the value of an arbitrary property, but not
its name. `[a/*b:red]` still emitted `a/*b: red;`, and the comment
removed the CSS after it.
The name now goes through `isValidArbitrary` too. The scanner only
extracts names with `a-z`, `-` or a `--custom` property, but other paths
can reach the parser: the browser build, `@source inline()` and direct
calls to `build()`.
Reported by CodeRabbit on #20690.
CSS ends a string at an unescaped newline. `isValidArbitrary` treated the
rest of the input as part of the string, so `content-['a\n/*']` passed
the check, and the `/*` started a real comment in the output.
A newline, carriage return or form feed in a string now makes the value
invalid. The scanner never extracts a candidate with whitespace, so only
direct calls to `build()` can reach this.
Reported by CodeRabbit on #20690.
`isValidArbitrary` accepted a string without a closing quote. In the
generated CSS, that quote can pair up with a quote that comes after it.
`[a':'/*']` has the property `a'` and the value `'/*'`, and becomes
`a': '/*';`. The `/*` is then outside of a string, and starts a comment.
A string without a closing quote now makes the input invalid. This
applies to property names and values.
Reported by CodeRabbit on #20690.
CSS reads an unquoted `url(…)` as a single token that ends at the first
`)`. A quote in it does not start a string, it makes a bad URL.
`isValidArbitrary` treated the quote as the start of a string, so
`bg-[url(foo'bar)/*')]` passed the check. In the generated CSS, the `/*`
after the first `)` started a comment.
In an unquoted `url(…)`, a quote, a `(` or a `/*` now makes the value
invalid, and so does a missing `)`. A quoted `url('…')` stays a normal
function with a string in it, so `bg-[url('/*.png')]` stays valid.
Reported by CodeRabbit on #20690.
`decodeArbitraryValue` keeps the `_` in a lowercase `url(…)` only. In
`URL(…)`, the `_` becomes a space. So `bg-[URL(_'image.png')]` becomes the
valid `URL( 'image.png')`, but the previous commit read it as an unquoted
URL with a quote in it, and rejected it.
`isUnquotedUrl` now skips an `_` like whitespace when the function name
is not a lowercase `url`, the same as the decoder.
Reported by Greptile on #20690.
`decodeArbitraryValue` does not decode the children of a lowercase
`url(…)` or `*_url(…)`, so an `_` in a nested `URL(…)` stays an `_`. The
previous commit treated that `_` as a space. So
`bg-[url('a',URL(_'b)/*'))]` passed as a quoted `URL( 'b)/*')`, but the
output kept `URL(_'b)`. CSS reads that as a bad URL that ends at the
first `)`, and the `/*` after it started a comment.
`isValidArbitrary` now tracks when it is inside a lowercase `url(…)`, and
keeps the `_` there, the same as the decoder.
CSS decodes escapes in a function name, so `u\72l(…)` is a `url(…)`. The
`url(…)` check compares the name as written, so
`bg-[u\72l(foo'bar)/*')]` passed as a normal function with a string in
it. CSS reads it as an unquoted URL that ends at the first `)`, and the
`/*` after it started a comment.
`isValidArbitrary` now rejects a `(` after a word with a CSS escape in
it. An escaped name like `u\72l('a.png')` is valid CSS, but no candidate
needs one.
- A hex escape takes up to 6 hex digits and one whitespace after them,
and an `_` becomes that whitespace. So `u\72_l(…)` is a `url(…)` too.
- `\_` is a literal `_` in a candidate, not a CSS escape, so
`w-[--my\_fn(1px)]` stays valid.
- An unescaped `_` ends the word, because it becomes a space, so
`bg-[\#fff_url(a.png)]` stays valid.
Reported by CodeRabbit on #20690.
`\_` is a literal `_` in a candidate, so `--my\_url(var(--size))` calls
the `--my_url` function, not `url(…)`. The check for an unquoted `url(…)`
took the `_` for a space, and rejected the `(` of `var(…)`. This utility
worked before.
An `_` after an odd number of backslashes is escaped now, so it is part
of the function name. `a\\_url(…)` still has a real `url(…)`, because
`\\` is an escaped `\`.
`decodeArbitraryValue` keeps each `_` in a function with a name that
ends in `_url`, also in `--my\_url(…)`. Since the previous commit, that
function is not a `url(…)` for the check anymore, so the check also
stopped to track that it keeps each `_`. Then `--my\_url(URL(_'a)/*'))`
was valid: the check read `URL( 'a)/*')` as a quoted string, but CSS
gets `URL(_'a)/*')`, an unquoted `url(…)` that ends at the first `)`,
and the `/*` starts a comment.
The check now has two parts. `isUrlFunctionAt` tells if CSS reads the
function as a `url(…)`. `keepsUnderscoresAt` tells if
`decodeArbitraryValue` keeps each `_` in it.
A `/*` in an unquoted `url(…)` is not a comment in CSS, so
`bg-[url(/a/*.png)]` is a valid class. The check rejected it.
PostCSS reads fewer forms as a `url(…)` than CSS does, and Vite and
webpack run PostCSS on the generated CSS. In the other forms, the `/*`
starts a comment for PostCSS. So we only allow the forms that both read
as a `url(…)`: a lowercase `url` at the start of the value, after
whitespace (also an `_`) or after a `(`, without whitespace after the
`(`. E.g.: `url(/a/*.png)`, `url(a.png),_url(/b/*.png)` and
`image-set(url(/a/*.png)_1x)`, but not `URL(/a/*.png)` or
`url(a.png),url(/b/*.png)`.
The start of the input only starts a token in a declaration value. A
`data-[…]` value becomes `[data-…]`, so `data-url(` is a normal function
there. So only the value of a utility and of an arbitrary property can
have a `/*` in a `url(…)`.
CSS reads `#url(` and `@url(` as a hash and an at-keyword, followed by a
normal `(`. So they are not a `url(…)` anymore for the check.
CSS reads `\/` as an escaped `/`, but PostCSS does not. It reads `\/*`
as a `\` and the start of a comment. Tailwind CSS prints the `\/*` as
written, so PostCSS throws "Unclosed comment" for `bg-[\/*]`, and for
`bg-[URL(a\/*b)]` too, because PostCSS does not read an uppercase
`URL(…)` as a `url(…)`.
A `url(…)` that PostCSS reads as a `url(…)` can still have one, because
PostCSS reads it up to the first `)` there.
Each fix in the check for comments found one more edge case. So we now
also test 5000 random values, made from the parts that matter (`url(`,
quotes, `/*`, `\`, `_`, …). For each value that a utility or an
arbitrary property accepts, Lightning CSS must keep the rule after it.
The random numbers have a fixed seed, so a failure can be reproduced.
The test fails on `main`, e.g. with `[--x:,/*]`.
Also add `[--x:a()<//*)*/]` as a case. `decodeArbitraryValue` drops the
`)` in `/*)*/` as unbalanced, so the comment would run into the next
rule.
A type hint like `image:` is not in the CSS, so `bg-[image:url(/a/*.png)]`
gives `background-image: url(/a/*.png)`. PostCSS ends the word before the
`url` at the `:`, so it reads the `url(…)` as a url too.
An at-word is the exception: PostCSS reads `@image:url` as one at-word, so
it reads the `/*` as a comment. These values stay invalid.
`bg-linear-[url(/a/*.png)]` printed `var(--tw-gradient-stops,url(/a/*.png))`.
PostCSS reads `,url` as one word, so it does not read the `(…)` as a url,
and it reads the `/*` as the start of a comment.
An audit of all functional utilities with a `/*` in a `url(…)` found only
these four templates. The snapshots do not change, because the test helper
formats the output.
This PR discards candidates that contain `</style`, so that the
generated CSS is always safe to inline in a `<style>` element.
Some frameworks inline the generated CSS in a `<style>` element (for
example Nuxt, Astro with `inlineStylesheets`, or Next.js with
`inlineCss`). The HTML parser ends a `<style>` element at `</style`, so
a candidate like `content-['</style>']` ends that element early. These
forms could all put `</style` in the output:
- Arbitrary values: `before:content-['</style>']`, `font-[x</style>]`
- Arbitrary properties: `[--x:</style>]`, `[--x</style>:red]`
- Arbitrary variants: `[&[data-x="</style>"]]:flex`
- Arbitrary modifiers: `bg-red-500/[</style>]`
The class name part of the selector was already safe, because it is
escaped.
## Approach
Only arbitrary parts can put text from the candidate in the output
as-is. Named roots, values and modifiers must match a known utility,
variant or theme key. So the check lives in `isValidArbitrary`, which
already validates each arbitrary value, variant, modifier and arbitrary
property name.
After the existing checks pass, `isValidArbitrary` decodes the value and
looks for `</style` (case insensitive). A value without a `<` or a `\`
skips the decode, so most candidates do no extra work.
Escaping the output instead is not enough, because Lightning CSS decodes
escapes when it minifies the CSS. So the check decodes the value the
same way first:
- CSS escapes: `\3c /style`, `\3C/STYLE` and `<\/style`
- Escaped newlines, which CSS removes in strings: `</s\<LF>tyle`
Comments are not a problem anymore: since
#20690,
`isValidArbitrary` rejects every `/*` outside a string or an unquoted
`url(…)`. So two values like `[--x:a()<//*)*/]` and `[--y:*/style>]`
can't join into `</style` when the minifier removes the comment.
Other `</` text, like `content-['</>']`, still works. `@apply` of a
discarded class gives the normal "Cannot apply unknown utility class"
error.
## Test plan
- New tests in `candidate.test.ts` for the forms above, the escaped
forms, escaped newlines, a comment that hides `</style`, and candidates
with `<` that are still valid.
- I ran the bypass candidates through `compile()` and `optimize(css, {
minify: true })`. None of them gives `</style` in the minified output.
- Fuzz runs (random candidate sets with `<`, `/`, `*`, comments,
escapes, quotes and `url(…)` pieces) find no `</style` in the raw or the
minified CSS.
- `pnpm run build` and all vitest projects pass.
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
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.
This PR fixes an issue where a
/*in an arbitrary value could comment out the CSS that comes after it.A candidate like
bg-[red/*]or[color:red/*]passed validation, and generated this CSS:The
/*starts a comment that never ends. Browsers ignore the CSS up to the next*/, and Lightning CSS drops all the rules after it. So one stray candidate in a scanned file (for example in a code sample or a comment) could silently remove other utilities, like.underlinehere, from the output.isValidArbitrarynow rejects a/*outside of a string or an unquotedurl(…). This applies to arbitrary values, arbitrary properties, arbitrary modifiers and arbitrary variants, because they all go through this function. A complete comment likebg-[red/*x*/]is valid CSS, but no candidate needs one, so we reject it too and keep the check simple. A/*inside of a string stays valid, for examplecontent-['/*']orbg-[url('/*.png')]. An escaped\/*is rejected too, because PostCSS reads it as the start of a comment.A
/*in an unquotedurl(…)In CSS, an unquoted
url(…)is a single token, so a/*inside of it is not a comment. These stay valid:bg-[url(/a/*.png)]bg-[url(a.png),_url(/b/*.png)]bg-[image-set(url(/a/*.png)_1x)]bg-[no-repeat_url(/a/*.png)]bg-[image:url(/a/*.png)][background-image:url(/a/*.png)]The PostCSS tokenizer reads a
url(…)only in some positions. So we allow a/*only in a lowercaseurl(that is not followed by whitespace, and that starts the value, or comes after whitespace (or an_), a(or a type hint likeimage:. This only applies to values that end up in a declaration (utility values and arbitrary properties), not to modifiers and variants. Other forms likeURL(/a/*.png),#url(/a/*.png)or@image:url(/a/*.png)are rejected.The gradient utilities now print a space after the
,invar(--tw-gradient-stops, …), so PostCSS does not read,url(as one word.Known limitation: a minifier like Lightning CSS removes the space after a
,, and PostCSS then reads,url(/b/*.png)as the start of a comment. This also happens for plain CSS likeurl(a.png), url("/b/*.png"), and is reported upstream in postcss/postcss#2194.Test plan
bg-[red/*],bg-[red/*comment*/],[color:red/*],bg-red-500/[50%/*],[&/*]:flexanddata-[a/*]:flexto the invalid candidates incandidate.test.ts. They fail without this change.url(…)forms to the invalid candidates, and tests that the safe forms above still parse.content-['/*']still parses.bg-[red/*] flex underline [color:red/*]and ran the result through Lightning CSS. Before: all rules after.bg-\[red\/\*\]were gone,.underlinetoo. After:.flexand.underlineare there, and the invalid candidates are ignored.