Skip to content

Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active

🧮 fix: Read Math Function Channels in CSS Custom Property Triplets (#16924)

Marco Beretta6c8f659d541f
diff --git a/e2e/specs/mock/scenarios/css-color-triplets.spec.ts b/e2e/specs/mock/scenarios/css-color-triplets.spec.ts
new file mode 100644
index 0000000000..b75f951965
--- /dev/null
+++ b/e2e/specs/mock/scenarios/css-color-triplets.spec.ts
@@ -0,0 +1,51 @@
+import { copyFileSync, mkdirSync, mkdtempSync, rmSync, symlinkSync, writeFileSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { join, resolve } from 'node:path';
+import { expect, test } from '@playwright/test';
+import { inOneProject, repoRoot, run } from './lint.helpers';
+
+/**
+ * A channel triplet in a custom property (`--brand: 255 0 0`) is a colour
+ * literal that `rgb(var(--brand))` paints, so the CSS colour gate rejects it
+ * outside the theme token sources. A CSS math function is a valid channel, so a
+ * triplet written with one is the same literal. Each scenario runs the real
+ * scanner over a scratch tree holding one stylesheet; no browser is involved.
+ */
+
+const STYLESHEET = 'client/src/scratch.css';
+
+function scanStylesheet(css: string): { status: number; output: string } {
+ const root = mkdtempSync(join(tmpdir(), 'lc-css-colors-'));
+ try {
+ mkdirSync(join(root, 'scripts'));
+ mkdirSync(join(root, 'client/src'), { recursive: true });
+ mkdirSync(join(root, 'packages/client/src'), { recursive: true });
+ copyFileSync(resolve(repoRoot, 'scripts/css-colors.mts'), join(root, 'scripts/css-colors.mts'));
+ symlinkSync(resolve(repoRoot, 'node_modules'), join(root, 'node_modules'), 'dir');
+ writeFileSync(join(root, STYLESHEET), css);
+ return run(process.execPath, [join(root, 'scripts/css-colors.mts')], { cwd: root });
+ } finally {
+ rmSync(root, { recursive: true, force: true });
+ }
+}
+
+test.describe('the CSS colour gate', () => {
+ test('a custom property triplet written with math functions is rejected @scenario:css-math-channel-triplet-is-rejected', () => {
+ inOneProject();
+ const result = scanStylesheet(
+ ':root {\n --brand: calc(255) 0 0;\n --accent: 255 min(100, 50 * 2) 0;\n --deep: calc(min(100, max(0, 255))) 0 0;\n}\n',
+ );
+ expect(result.status, result.output).toBe(1);
+ expect(result.output).toContain(`${STYLESHEET}:2 calc(255) 0 0`);
+ expect(result.output).toContain(`${STYLESHEET}:3 255 min(100, 50 * 2) 0`);
+ expect(result.output).toContain(`${STYLESHEET}:4 calc(min(100, max(0, 255))) 0 0`);
+ });
+
+ test('a triplet whose math reads a variable or a length is accepted @scenario:css-math-channel-with-variable-or-unit-is-accepted', () => {
+ inOneProject();
+ const result = scanStylesheet(
+ ':root {\n --brand: calc(var(--r) * 2) 0 0;\n --offset: calc(1px + 2px) 0 0;\n}\n',
+ );
+ expect(result.status, result.output).toBe(0);
+ });
+});
diff --git a/scripts/css-colors.mts b/scripts/css-colors.mts
index 4cca31a00a..6263ef6d90 100644
--- a/scripts/css-colors.mts
+++ b/scripts/css-colors.mts
@@ -44,11 +44,20 @@ const NAMED_COLORS =
);
const HEX = /#[0-9a-fA-F]{3,8}(?![\w-])/g;
-const FUNCTION =
- /(?<![\w-])(?:(?:rgba?|hsla?|hwb|lab|lch|oklab|oklch)\(\s*var\(\s*--[\w-]+\s*,\s*[\d.+-]|(?:rgba?|hsla?|hwb|lab|lch|oklab|oklch)\(\s*(?:[\d.+-]|none\b|(?:calc|min|max|clamp|abs|sign|round|mod|rem|sin|cos|tan|asin|acos|atan2?|pow|sqrt|hypot|log|exp)\(|(?:var\([^)]*\)\s*,?\s*)+[\d.+-])|color\(\s*(?!from\b)[a-z0-9-]+\s+(?:(?:var\([^)]*\)\s*,?\s*)*[\d.+-]|none\b))/gi;
+const MATH_FUNCTIONS =
+ 'calc|min|max|clamp|abs|sign|round|mod|rem|sin|cos|tan|asin|acos|atan2?|pow|sqrt|hypot|log|exp';
+const FUNCTION = new RegExp(
+ String.raw`(?<![\w-])(?:(?:rgba?|hsla?|hwb|lab|lch|oklab|oklch)\(\s*var\(\s*--[\w-]+\s*,\s*[\d.+-]|(?:rgba?|hsla?|hwb|lab|lch|oklab|oklch)\(\s*(?:[\d.+-]|none\b|(?:${MATH_FUNCTIONS})\(|(?:var\([^)]*\)\s*,?\s*)+[\d.+-])|color\(\s*(?!from\b)[a-z0-9-]+\s+(?:(?:var\([^)]*\)\s*,?\s*)*[\d.+-]|none\b))`,
+ 'gi',
+);
const RELATIVE_COLOR = /(?<![\w-])(?:rgba?|hsla?|hwb|lab|lch|oklab|oklch|color)\(\s*from\s+/gi;
const CHANNEL_KEYWORD = /(?<![\w-])(?:[rgbhslwcaxyz]|alpha)(?![\w-])/i;
-const CHANNEL = '[+-]?(?:\\d+\\.?\\d*|\\.\\d+)%?';
+const CHANNEL = '[+-]?(?:\\d+\\.?\\d*|\\.\\d+)(?:e[+-]?\\d+)?%?';
+/** An innermost math function, or grouping parenthesis, over numbers alone: no `var()` and no unit. */
+const NUMERIC_MATH = new RegExp(
+ String.raw`(?<![\w-])(?:${MATH_FUNCTIONS})?\((?:[\d.%+*/,\s-]|e|pi|infinity|nan|nearest|up|down|to-zero)*\)`,
+ 'gi',
+);
const SEPARATOR = '(?:\\s*,\\s*|\\s+)';
const BARE_TRIPLET = `${CHANNEL}${SEPARATOR}${CHANNEL}${SEPARATOR}${CHANNEL}(?:\\s*/\\s*(?:${CHANNEL}|var\\([^)]*\\)))?`;
/** The whole value is three bare channels, or one is the fallback of a `var()`. */
@@ -64,6 +73,12 @@ const NON_COLOR_PROPERTY =
const blank = (match: string): string => match.replace(/[^\n]/g, ' ');
+/** A math function over numbers (`calc(255)`, `min(100, max(0, 255))`) is a fixed channel, so it reads as one. */
+function collapseMath(text: string): string {
+ const collapsed = text.replace(NUMERIC_MATH, '0');
+ return collapsed === text ? text : collapseMath(collapsed);
+}
+
const URL_FUNCTION = /url\(\s*(?:"(?:[^"\\]|\\[\s\S])*"|'(?:[^'\\]|\\[\s\S])*'|[^)]*)\)/gi;
const STRING = /"(?:[^"\\\n]|\\[\s\S])*"|'(?:[^'\\\n]|\\[\s\S])*'/g;
@@ -151,7 +166,7 @@ function relativeLiterals(text: string): Array<{ literal: string; offset: number
const bodyStart = offset + match[0].length;
const isColorFunction = /^color\(/i.test(match[0]);
const [origin, ...rest] = relativeTokens(text, bodyStart);
- if (origin && TRIPLET_FALLBACK.test(origin.text)) {
+ if (origin && TRIPLET_FALLBACK.test(collapseMath(origin.text))) {
return [{ literal: text.slice(offset, origin.end), offset }];
}
const channels = rest.slice(isColorFunction ? 1 : 0, isColorFunction ? 4 : 3);
@@ -198,7 +213,7 @@ export function findCssColorLiterals(
const selector = parent?.type === 'rule' ? parent.selector : '';
if (isAllowed(allowedRules, selector, declaration.prop)) return;
const line = declaration.source?.start?.line ?? 1;
- if (isCustomProperty && TRIPLET.test(colourText(declaration.value))) {
+ if (isCustomProperty && TRIPLET.test(collapseMath(colourText(declaration.value)))) {
findings.push({ line, literal: declaration.value.trim() });
return;
}
diff --git a/scripts/css-colors.test.mts b/scripts/css-colors.test.mts
index 8fc8768138..e59626cb40 100644
--- a/scripts/css-colors.test.mts
+++ b/scripts/css-colors.test.mts
@@ -171,6 +171,42 @@ test('reads a channel triplet in a custom property', () => {
assert.deepEqual(literals('a { margin: 1 2 3; }'), []);
});
+test('reads math function channels in a custom property triplet', () => {
+ assert.deepEqual(literals('a { --brand: calc(255) 0 0; }'), ['calc(255) 0 0']);
+ assert.deepEqual(literals('a { --brand: 255 MIN(100, 50 * 2) clamp(0, (1 + 2) * 3, 9); }'), [
+ '255 MIN(100, 50 * 2) clamp(0, (1 + 2) * 3, 9)',
+ ]);
+ assert.deepEqual(literals('a { --brand: calc(100% - 10%), 0%, 0% / calc(0.5); }'), [
+ 'calc(100% - 10%), 0%, 0% / calc(0.5)',
+ ]);
+ assert.deepEqual(literals('a { --brand: var(--configured, calc(255) 0 0); }'), [
+ 'var(--configured, calc(255) 0 0)',
+ ]);
+ assert.deepEqual(literals('a { --brand: calc(min(100, max(0, 255))) 0 0; }'), [
+ 'calc(min(100, max(0, 255))) 0 0',
+ ]);
+ assert.deepEqual(literals('a { color: rgb(from var(--base, calc(255) 0 0) r g b); }'), [
+ 'rgb(from var(--base, calc(255) 0 0)',
+ ]);
+ assert.deepEqual(literals('a { --brand: calc(1e2) calc(pi * 10) 2.5E1; }'), [
+ 'calc(1e2) calc(pi * 10) 2.5E1',
+ ]);
+ assert.deepEqual(literals('a { --brand: calc(min(var(--r), max(0, 255))) 0 0; }'), []);
+ assert.deepEqual(literals('a { --brand: round(down, 255) round(to-zero, 2.5, 1) 0; }'), [
+ 'round(down, 255) round(to-zero, 2.5, 1) 0',
+ ]);
+ assert.deepEqual(literals('a { color: rgb(from var(--base) calc(255) 0 0); }'), [
+ 'rgb(from var(--base) calc(255)',
+ ]);
+ assert.deepEqual(literals('a { --size: calc(1em + 1in) 0 0; }'), []);
+ assert.deepEqual(literals('a { --brand: calc(var(--r)) 0 0; }'), []);
+ assert.deepEqual(literals('a { --brand: calc(var(--r) * 2) 0 0; }'), []);
+ assert.deepEqual(
+ literals('a { --offset: calc(1px + 2px) 0 0; --size: max(1rem, 2vw) 0 0; }'),
+ [],
+ );
+});
+
test('a token source exempts its custom properties only', () => {
const css = ':root { --x: 255 0 0; --y: #fff; }\na { color: #fff; width: calc(1 * 2); }';
assert.deepEqual(findCssColorLiterals(css, [], true), [{ line: 2, literal: '#fff' }]);