Skip to content

Commit 374d191

Browse files
Reject arbitrary values with unbalanced parens or brackets (#20577)
<!-- 👋 Hey, thanks for your interest in contributing to Tailwind! **Please ask first before starting work on any significant new features.** It's never a fun experience to have your pull request declined after investing a lot of time and effort into a new feature. To avoid this from happening, we request that contributors create a discussion to first discuss any significant new features. For more info, check out the contributing guide: https://github.com/tailwindlabs/tailwindcss/blob/main/.github/CONTRIBUTING.md --> ## Summary Candidates with an unclosed `(` or `[` inside an arbitray value are accepted and generated invalid css: ```css `w-[calc(20px]` -> `width: calc()20px;` `w-[(]` -> `width: ();` ``` Tailwind play with the bug reproduced: https://play.tailwindcss.com/vQyd5Lh4bz <img width="781" height="1015" alt="Captura de Tela 2026-10-14 às 19 43 48" src="https://github.com/user-attachments/assets/26039daf-000a-44f0-8fbd-3f4c1e28115c" /> Why this was happening: 1. `isValidArbitrary` documents that unbalanced values are not allowed, but it never checked that every `(` and `[` was closed, and it ignored a mismatched closer such as `(]`. 2. the parser validated the value after `decodeArbitraryValue`, and the value parser turns `calc(20px` into `calc()20px`, so the validator only saw a balanced string. This pr: - Makes `isValidArbitrary` return `false` for a mismatched closing char and for any unclosed `(` or `[` at the end of the value, a mismatched `}` is still tolerated, since `{` is not tracked on the stack, e.g. `var(--a, {})` - validates the raw value before decoding, via a small `decodeValidArbitraryValue` helper at all places where an arbitrary value is parsed, like arbitrary properties, utility values, modifiers , arbitrary variants and functional variants suck `[...]` and `(...)` this change is also making the js parser agree with the rust scanner, which already rejects unbalanced brackets (check `arbitrary_value_machine.rs`), this bug is reachable wherever the js parser receives strings directly, such tailwind play or `compile().build([...])` ## Test plan - added test to `candidate.test.ts` covering unbalanced values for utility values, arbitrary properties, modifiers, arbitrary variants and functional variants, they fail on `main` and pass with the change made here - Some existent tests contained unbalanced inputs taht only passed because the value parser repaired or dropped the bad paren `pnpm vitest run src/candidate.test.ts` `pnpm vitest run src/index.test.ts` --------- Co-authored-by: Robin Malfait <[email protected]>
1 parent 9c92dfd commit 374d191

5 files changed

Lines changed: 80 additions & 19 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
5050
- Canonicalization: don't collapse utilities into a utility that uses another theme variable with the same default value (e.g. `mt-card mb-card` → `my-card`, not `my-4`) ([#20574](https://github.com/tailwindlabs/tailwindcss/pull/20574))
5151
- Ensure `@tailwindcss/postcss` rewrites relative `url(…)`s of stylesheets that are inlined before it runs (e.g. `@import`s in Vite) in optimized builds ([#20576](https://github.com/tailwindlabs/tailwindcss/pull/20576))
5252
- Drop nested rules that put a combinator after a pseudo-element when using pseudo-element variants (e.g. `selection:*` on a utility with a nested `& > *` rule) ([#20575](https://github.com/tailwindlabs/tailwindcss/pull/20575))
53+
- Don't generate CSS for arbitrary values with unbalanced parentheses or brackets (e.g. `w-[calc(1px]` or `[--x:a[b]`) ([#20577](https://github.com/tailwindlabs/tailwindcss/pull/20577))
5354

5455
## [4.3.3] - 2026-07-16
5556

‎packages/tailwindcss/src/candidate.test.ts‎

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,56 @@ it('should parse a utility with an arbitrary value with parens', () => {
573573
`)
574574
})
575575

576+
describe('arbitrary values with unbalanced parens or brackets', () => {
577+
let utilities = new Utilities()
578+
utilities.static('flex', () => [])
579+
utilities.functional('w', () => [])
580+
utilities.functional('bg', () => [])
581+
let variants = new Variants()
582+
variants.functional('data', () => {})
583+
584+
it.each([
585+
// Utility values
586+
'w-[calc(1px]',
587+
'w-[(]',
588+
'w-[calc(1px+(2px]',
589+
'w-[1px)]',
590+
'w-[calc(1px))]',
591+
'w-[a(b]c)]',
592+
'w-[var(--a,[1px)]',
593+
'w-[var(--a,[1px]',
594+
595+
// A `}` at the top-level is invalid. `{` is not tracked on the stack,
596+
// otherwise `[&{color:red}]:flex` would become valid.
597+
'w-[{}]',
598+
599+
// Arbitrary properties
600+
'[width:calc(1px]',
601+
602+
// Arbitrary modifiers
603+
'bg-red-500/[calc(1]',
604+
605+
// Arbitrary variants
606+
'[&:is(.a]:flex',
607+
608+
// Functional variants
609+
'data-[foo(]:flex',
610+
])('should not parse `%s`', (candidate) => {
611+
expect(run(candidate, { utilities, variants })).toEqual([])
612+
})
613+
614+
it.each([
615+
'w-[calc(1px+(2px*3))]',
616+
"w-[calc(1px+var(--a,'('))]",
617+
'w-[var(--a,[1px])]',
618+
619+
// A `}` inside parens is tolerated, because `{` is not tracked on the stack
620+
'w-[var(--a,{})]',
621+
])('should parse `%s`', (candidate) => {
622+
expect(run(candidate, { utilities, variants })).not.toEqual([])
623+
})
624+
})
625+
576626
it('should not parse a utility with an arbitrary value with parens that does not start with --', () => {
577627
let utilities = new Utilities()
578628
utilities.functional('bg', () => [])
@@ -1050,13 +1100,13 @@ it('should properly decode escaped underscores but not convert underscores to sp
10501100

10511101
// Due to limitations in the CSS value parser, the `var(…)` inside the `calc(…)` is not correctly
10521102
// scanned here.
1053-
expect(run('flex-[calc(0.2*var(--\\_foo)]', { utilities, variants })).toMatchInlineSnapshot(`
1103+
expect(run('flex-[calc(0.2*var(--\\_foo))]', { utilities, variants })).toMatchInlineSnapshot(`
10541104
[
10551105
{
10561106
"important": false,
10571107
"kind": "functional",
10581108
"modifier": null,
1059-
"raw": "flex-[calc(0.2*var(--\\_foo)]",
1109+
"raw": "flex-[calc(0.2*var(--\\_foo))]",
10601110
"root": "flex",
10611111
"value": {
10621112
"dataType": null,
@@ -1067,13 +1117,13 @@ it('should properly decode escaped underscores but not convert underscores to sp
10671117
},
10681118
]
10691119
`)
1070-
expect(run('flex-[calc(0.2*var(--_foo)]', { utilities, variants })).toMatchInlineSnapshot(`
1120+
expect(run('flex-[calc(0.2*var(--_foo))]', { utilities, variants })).toMatchInlineSnapshot(`
10711121
[
10721122
{
10731123
"important": false,
10741124
"kind": "functional",
10751125
"modifier": null,
1076-
"raw": "flex-[calc(0.2*var(--_foo)]",
1126+
"raw": "flex-[calc(0.2*var(--_foo))]",
10771127
"root": "flex",
10781128
"value": {
10791129
"dataType": null,

‎packages/tailwindcss/src/candidate.ts‎

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -427,10 +427,10 @@ export function* parseCandidate(input: string, designSystem: DesignSystem): Iter
427427
if (idx === -1 || idx === 0 || idx === baseWithoutModifier.length - 1) return
428428

429429
let property = baseWithoutModifier.slice(0, idx)
430-
let value = decodeArbitraryValue(baseWithoutModifier.slice(idx + 1))
430+
let value = decodeValidArbitraryValue(baseWithoutModifier.slice(idx + 1))
431431

432432
// Values can't contain `;` or `}` characters at the top-level.
433-
if (!isValidArbitrary(value)) return
433+
if (value === null) return
434434

435435
yield {
436436
kind: 'arbitrary',
@@ -551,10 +551,10 @@ export function* parseCandidate(input: string, designSystem: DesignSystem): Iter
551551
// Arbitrary values must end with a `]`.
552552
if (value[value.length - 1] !== ']') return
553553

554-
let arbitraryValue = decodeArbitraryValue(value.slice(startArbitraryIdx + 1, -1))
554+
let arbitraryValue = decodeValidArbitraryValue(value.slice(startArbitraryIdx + 1, -1))
555555

556556
// Values can't contain `;` or `}` characters at the top-level.
557-
if (!isValidArbitrary(arbitraryValue)) continue
557+
if (arbitraryValue === null) continue
558558

559559
// Extract an explicit typehint if present, e.g. `bg-[color:var(--my-var)])`
560560
let typehint: string | null = null
@@ -615,10 +615,10 @@ export function* parseCandidate(input: string, designSystem: DesignSystem): Iter
615615

616616
function parseModifier(modifier: string): CandidateModifier | null {
617617
if (modifier[0] === '[' && modifier[modifier.length - 1] === ']') {
618-
let arbitraryValue = decodeArbitraryValue(modifier.slice(1, -1))
618+
let arbitraryValue = decodeValidArbitraryValue(modifier.slice(1, -1))
619619

620620
// Values can't contain `;` or `}` characters at the top-level.
621-
if (!isValidArbitrary(arbitraryValue)) return null
621+
if (arbitraryValue === null) return null
622622

623623
// Empty arbitrary values are invalid. E.g.: `data-[]:`
624624
// ^^
@@ -678,10 +678,10 @@ export function parseVariant(variant: string, designSystem: DesignSystem): Varia
678678
*/
679679
if (variant[1] === '@' && variant.includes('&')) return null
680680

681-
let selector = decodeArbitraryValue(variant.slice(1, -1))
681+
let selector = decodeValidArbitraryValue(variant.slice(1, -1))
682682

683683
// Values can't contain `;` or `}` characters at the top-level.
684-
if (!isValidArbitrary(selector)) return null
684+
if (selector === null) return null
685685

686686
// Empty arbitrary values are invalid. E.g.: `[]:`
687687
// ^^
@@ -759,10 +759,10 @@ export function parseVariant(variant: string, designSystem: DesignSystem): Varia
759759
// Discard values like `foo-[#bar]`
760760
if (value[0] !== '[') continue
761761

762-
let arbitraryValue = decodeArbitraryValue(value.slice(1, -1))
762+
let arbitraryValue = decodeValidArbitraryValue(value.slice(1, -1))
763763

764764
// Values can't contain `;` or `}` characters at the top-level.
765-
if (!isValidArbitrary(arbitraryValue)) return null
765+
if (arbitraryValue === null) return null
766766

767767
// Empty arbitrary values are invalid. E.g.: `data-[]:`
768768
// ^^
@@ -783,10 +783,10 @@ export function parseVariant(variant: string, designSystem: DesignSystem): Varia
783783
// Discard values like `foo-(--bar)`
784784
if (value[0] !== '(') continue
785785

786-
let arbitraryValue = decodeArbitraryValue(value.slice(1, -1))
786+
let arbitraryValue = decodeValidArbitraryValue(value.slice(1, -1))
787787

788788
// Values can't contain `;` or `}` characters at the top-level.
789-
if (!isValidArbitrary(arbitraryValue)) return null
789+
if (arbitraryValue === null) return null
790790

791791
// Empty arbitrary values are invalid. E.g.: `data-():`
792792
// ^^
@@ -1237,3 +1237,10 @@ function escapeUnderscore(value: string): string {
12371237
.replaceAll('_', String.raw`\_`) // Escape underscores to keep them as-is
12381238
.replaceAll(' ', '_') // Replace spaces with underscores
12391239
}
1240+
1241+
// validates the raw value first and only then decodes it
1242+
// the order matters: decoding breaks unbalanced parens (`calc(1px` becomes
1243+
// `calc()1px`) so validating the decoded value would hide them
1244+
function decodeValidArbitraryValue(raw: string): string | null {
1245+
return isValidArbitrary(raw) ? decodeArbitraryValue(raw) : null
1246+
}

‎packages/tailwindcss/src/index.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ describe('compiling CSS', () => {
150150
[
151151
'bg-[no-repeat_url(./my_file.jpg)]',
152152
'ml-[var(--spacing-1_5,_var(--spacing-2_5,_1rem))]',
153-
'ml-[theme(--spacing-1_5,theme(--spacing-2_5,_1rem)))]',
153+
'ml-[theme(--spacing-1_5,theme(--spacing-2_5,_1rem))]',
154154
],
155155
css`
156156
@theme {
@@ -167,7 +167,7 @@ describe('compiling CSS', () => {
167167
--spacing-2_5: 2.5rem;
168168
}
169169
170-
.ml-\\[theme\\(--spacing-1_5\\,theme\\(--spacing-2_5\\,_1rem\\)\\)\\)\\] {
170+
.ml-\\[theme\\(--spacing-1_5\\,theme\\(--spacing-2_5\\,_1rem\\)\\)\\] {
171171
margin-left: 1.5rem;
172172
}
173173

‎packages/tailwindcss/src/utils/is-valid-arbitrary.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,16 @@ export function isValidArbitrary(input: string) {
8181
// the buffer can be dirty for the next use and still be correct since
8282
// reading/writing always starts at position `0`.
8383
stackPos--
84+
} else if (char !== CLOSE_CURLY) {
85+
return false
8486
}
87+
8588
break
8689
case SEMICOLON:
8790
if (stackPos === 0) return false
8891
break
8992
}
9093
}
9194

92-
return true
95+
return stackPos === 0
9396
}

0 commit comments

Comments
 (0)