Repository navigation
Discard candidates that contain </style - #20691
Conversation
[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 |
|
Note Reviews pausedIt 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 Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
No actionable comments were generated in the recent review. WalkthroughArbitrary-value validation and arbitrary-property parsing now reject inputs that contain Merge Risk:
|
498dfab to
774b5fb
Compare
2962f9d to
728a337
Compare
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.
728a337 to
bf5f395
Compare
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 withinlineStylesheets, or Next.js withinlineCss). The HTML parser ends a<style>element at</style, so a candidate likecontent-['</style>']ends that element early. These forms could all put</stylein the output:before:content-['</style>'],font-[x</style>][--x:</style>],[--x</style>:red][&[data-x="</style>"]]:flexbg-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,
isValidArbitrarydecodes 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:
\3c /style,\3C/STYLEand<\/style</s\<LF>tyleComments are not a problem anymore: since #20690,
isValidArbitraryrejects every/*outside a string or an unquotedurl(…). So two values like[--x:a()<//*)*/]and[--y:*/style>]can't join into</stylewhen the minifier removes the comment.Other
</text, likecontent-['</>'], still works.@applyof a discarded class gives the normal "Cannot apply unknown utility class" error.Test plan
candidate.test.tsfor the forms above, the escaped forms, escaped newlines, a comment that hides</style, and candidates with<that are still valid.compile()andoptimize(css, { minify: true }). None of them gives</stylein the minified output.<,/,*, comments, escapes, quotes andurl(…)pieces) find no</stylein the raw or the minified CSS.pnpm run buildand all vitest projects pass.