Skip to content

Commit fa3d454

Browse files
authored
Ensure arbitrary values only support valid values (#5293)
* ensure arbitrary values only support valid values * ensure we validate balancing [], () and {}
1 parent 048a29e commit fa3d454

File tree

3 files changed

+112
-0
lines changed

3 files changed

+112
-0
lines changed

src/jit/lib/setupContextUtils.js

+62
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,64 @@ function withIdentifiers(styles) {
125125
})
126126
}
127127

128+
let matchingBrackets = new Map([
129+
['{', '}'],
130+
['[', ']'],
131+
['(', ')'],
132+
])
133+
let inverseMatchingBrackets = new Map(
134+
Array.from(matchingBrackets.entries()).map(([k, v]) => [v, k])
135+
)
136+
137+
let quotes = new Set(['"', "'", '`'])
138+
139+
// Arbitrary values must contain balanced brackets (), [] and {}. Escaped
140+
// values don't count, and brackets inside quotes also don't count.
141+
//
142+
// E.g.: w-[this-is]w-[weird-and-invalid]
143+
// E.g.: w-[this-is\\]w-\\[weird-but-valid]
144+
// E.g.: content-['this-is-also-valid]-weirdly-enough']
145+
function isValidArbitraryValue(value) {
146+
let stack = []
147+
let inQuotes = false
148+
149+
for (let i = 0; i < value.length; i++) {
150+
let char = value[i]
151+
152+
// Non-escaped quotes allow us to "allow" anything in between
153+
if (quotes.has(char) && value[i - 1] !== '\\') {
154+
inQuotes = !inQuotes
155+
}
156+
157+
if (inQuotes) continue
158+
if (value[i - 1] === '\\') continue // Escaped
159+
160+
if (matchingBrackets.has(char)) {
161+
stack.push(char)
162+
} else if (inverseMatchingBrackets.has(char)) {
163+
let inverse = inverseMatchingBrackets.get(char)
164+
165+
// Nothing to pop from, therefore it is unbalanced
166+
if (stack.length <= 0) {
167+
return false
168+
}
169+
170+
// Popped value must match the inverse value, otherwise it is unbalanced
171+
if (stack.pop() !== inverse) {
172+
return false
173+
}
174+
}
175+
}
176+
177+
// If there is still something on the stack, it is also unbalanced
178+
if (stack.length > 0) {
179+
return false
180+
}
181+
182+
// All good, totally balanced!
183+
return true
184+
}
185+
128186
function buildPluginApi(tailwindConfig, context, { variantList, variantMap, offsets }) {
129187
function getConfigValue(path, defaultValue) {
130188
return path ? dlv(tailwindConfig, path, defaultValue) : tailwindConfig
@@ -273,6 +331,10 @@ function buildPluginApi(tailwindConfig, context, { variantList, variantMap, offs
273331
return []
274332
}
275333

334+
if (!isValidArbitraryValue(value)) {
335+
return []
336+
}
337+
276338
let includedRules = []
277339
let ruleSets = []
278340
.concat(

tests/jit/arbitrary-values.test.css

+22
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,28 @@
9393
.w-\[calc\(100\%\/3-1rem\*2\)\] {
9494
width: calc(100% / 3 - 1rem * 2);
9595
}
96+
.w-\[\{\}\] {
97+
width: {
98+
}
99+
}
100+
.w-\[\{\{\}\}\] {
101+
width: {
102+
{
103+
}
104+
}
105+
}
106+
.w-\[\(\)\] {
107+
width: ();
108+
}
109+
.w-\[\(\(\)\)\] {
110+
width: (());
111+
}
112+
.w-\[\'\)\(\)\'\] {
113+
width: ')()';
114+
}
115+
.w-\[\'\}\{\}\'\] {
116+
width: '}{}';
117+
}
96118
.min-w-\[3\.23rem\] {
97119
min-width: 3.23rem;
98120
}

tests/jit/arbitrary-values.test.html

+28
Original file line numberDiff line numberDiff line change
@@ -116,5 +116,33 @@
116116
<div class="delay-[var(--delay)]"></div>
117117
<div class="content-['hello']"></div>
118118
<div class="content-[attr(content-before)]"></div>
119+
120+
<!-- Balancing issues, this is not checking the validity of the actual value, but purely syntax-wise -->
121+
<div class="w-[do-not-generate-this]w-[it-is-invalid-syntax]"></div><!-- INVALID -->
122+
<div class="grid-cols-[[linename],1fr,auto]"></div><!-- VALID -->
123+
<div class="w-[{}]"></div><!-- VALID -->
124+
<div class="w-[{{}}]"></div><!-- VALID -->
125+
<div class="w-[[]]"></div><!-- VALID -->
126+
<div class="w-[[[]]]"></div><!-- VALID -->
127+
<div class="w-[()]"></div><!-- VALID -->
128+
<div class="w-[(())]"></div><!-- VALID -->
129+
<div class="w-[][]"></div><!-- INVALID -->
130+
<div class="w-[)(]"></div><!-- INVALID -->
131+
<div class="w-[}{]"></div><!-- INVALID -->
132+
<div class="w-[][]]"></div><!-- INVALID -->
133+
<div class="w-[)()]"></div><!-- INVALID -->
134+
<div class="w-[}{}]"></div><!-- INVALID -->
135+
<div class="w-['][]']"></div><!-- VALID -->
136+
<div class="w-[')()']"></div><!-- VALID -->
137+
<div class="w-['}{}']"></div><!-- VALID -->
138+
<div class="w-[[']']]"></div><!-- VALID -->
139+
<div class="w-[(')')]"></div><!-- VALID -->
140+
<div class="w-[{'}'}]"></div><!-- VALID -->
141+
<div class="w-[{[}]]"></div><!-- INVALID -->
142+
<div class="w-[[{]}]"></div><!-- INVALID -->
143+
<div class="w-[{(})]"></div><!-- INVALID -->
144+
<div class="w-[({)}]"></div><!-- INVALID -->
145+
<div class="w-[([)]]"></div><!-- INVALID -->
146+
<div class="w-[[(])]"></div><!-- INVALID -->
119147
</body>
120148
</html>

0 commit comments

Comments
 (0)