Skip to content

Discard candidates that contain </style - #20691

Merged
RobinMalfait merged 6 commits into
mainfrom
robin/open-pr-reject-/style-in-candidates
Nov 16, 2026
Merged

RobinMalfait merged 6 commits into
mainfrom
robin/open-pr-reject-/style-in-candidates

Conversation

@RobinMalfait

@RobinMalfait RobinMalfait commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

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.

@RobinMalfait
RobinMalfait requested a review from a team as a code owner November 10, 2026 13:06
@greptile-apps

greptile-apps Bot commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; no new actionable issues were found.

Reviews (12) · Last reviewed commit: "Test that a comment can't hide a `</styl..." · Reviewed by Greptile

@coderabbitai

coderabbitai Bot commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

Note

Reviews paused

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.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • Resume reviews
  • Trigger review

No actionable comments were generated in the recent review.

Walkthrough

Arbitrary-value validation and arbitrary-property parsing now reject inputs that contain </style after CSS escapes are decoded, without regard to case. The validator also rejects unterminated comments. Regression tests cover matching sequences in candidate forms, including escaped spellings, and confirm that other uses of < remain parseable. A compile test checks comment handling across candidates. The changelog records the change.

Merge Risk: Minimal · up to 68daa

No actionable merge-blocking issue is established; merge after normal checks.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title clearly states the main change: discarding candidates that contain </style.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Description check Passed The description explains the change to discard candidates that could place </style in generated CSS and gives examples that match the changeset.
  • 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.

Share

Recent 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: b9f7dd7c-d5c6-40e0-8906-42da25b4728f
Commits

Reviewing files that changed from the base of the PR and between a279db0 and 68daa7f.

Files selected for processing: 3

  • packages/tailwindcss/src/candidate.test.ts
  • packages/tailwindcss/src/candidate.ts
  • packages/tailwindcss/src/utils/is-valid-arbitrary.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

Comment thread packages/tailwindcss/src/candidate.ts Resolved
Comment thread packages/tailwindcss/src/candidate.ts Resolved
@RobinMalfait
RobinMalfait force-pushed the robin/open-pr-reject-/style-in-candidates branch from 498dfab to 774b5fb Compare November 10, 2026 14:16
Comment thread packages/tailwindcss/src/candidate.ts Resolved
@RobinMalfait
RobinMalfait force-pushed the robin/open-pr-reject-/style-in-candidates branch from 2962f9d to 728a337 Compare November 10, 2026 21:44
The generated CSS can be inlined in a `<style>` element, and `</style`
would end that element. Candidates whose text contains `</style` (case
insensitive), also after CSS escapes are decoded (e.g. `\3c /style` or
`<\/style`), are now discarded.

Escaping the output is not enough, because Lightning CSS decodes such
escapes again when it minifies the CSS.
CSS ends a hex escape at any whitespace, and `\r\n` counts as one
whitespace. Lightning CSS decodes `\3c\r/style` to `</style` as well.
In a CSS string, a `\` followed by a newline is a line continuation,
and Lightning CSS removes it when it minifies the CSS. So a candidate
like `[content:'</s\<LF>tyle>']` passed the check, but the minified
output contained `content:"</style>"`.
Only arbitrary values, properties, variants and modifiers can put text
from the candidate in the output as-is. Named parts must match a known
utility, variant or theme key. So there is no need to check each
candidate at the start of `parseCandidate`.

`isValidArbitrary` already walks each character of an arbitrary part.
It now notes a `<` or a `\`, and only decodes the value when it saw one.

The property name of an arbitrary property does not go through
`isValidArbitrary`, so it gets its own check.
@RobinMalfait
RobinMalfait force-pushed the robin/open-pr-reject-/style-in-candidates branch from 728a337 to bf5f395 Compare November 11, 2026 00:09
@RobinMalfait RobinMalfait self-assigned this Nov 16, 2026
@RobinMalfait
RobinMalfait merged commit 93d9f24 into main Nov 16, 2026
13 checks passed
@RobinMalfait
RobinMalfait deleted the robin/open-pr-reject-/style-in-candidates branch November 16, 2026 17:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant