From 01d4faa3de241e9e97e735312b8c8321dcd0f6e7 Mon Sep 17 00:00:00 2001 From: Elias <69803556+elias1218@users.noreply.github.com> Date: Thu, 19 Nov 2026 15:14:35 -0500 Subject: [PATCH 1/5] fix(web): theme CSS shorthands with var() can no longer load remote files A shorthand such as `background: var(--none, url(...))` leaves each longhand reading "" until substitution, so the per-property fetch check never saw the url(). The guard now drops those longhands when the block's cssText fetches, or when cssText lists one bare because a later longhand hid the shorthand. --- apps/web/src/themeCss.browser.test.ts | 12 +++++++++++- apps/web/src/themeCss.ts | 12 +++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/apps/web/src/themeCss.browser.test.ts b/apps/web/src/themeCss.browser.test.ts index 28fccece4c5c5..c6098a583533b 100644 --- a/apps/web/src/themeCss.browser.test.ts +++ b/apps/web/src/themeCss.browser.test.ts @@ -14,7 +14,10 @@ it("applies theme CSS and drops every rule that would load a remote file", () => @namespace svg url(http://www.w3.org/2000/svg); @property --registered { syntax: ""; inherits: false; initial-value: url(https://example.com/d); } @counter-style leak { system: cyclic; symbols: url(https://example.com/f); } - @keyframes leak { to { background-image: url(https://example.com/g); } } + @keyframes leak { + from { background: var(--none, url(https://example.com/i)); } + to { background-image: url(https://example.com/g); } + } :root { --leak: u\\72l(https://example.com/a); } [data-theme-css-probe] { color: rgb(1, 2, 3); @@ -24,6 +27,10 @@ it("applies theme CSS and drops every rule that would load a remote file", () => mask-image: v\\61r(--none, u\\72l(https://example.com/e)); @media (min-width: 0px) { cursor: url(https://example.com/c), auto; } } + [data-theme-css-probe] { background: var(--none, url(https://example.com/h)); } + [data-theme-css-probe] { list-style: var(--none, u\\72l(https://example.com/j)); } + [data-theme-css-probe] { mask: var(--none, url(https://example.com/k)); mask-size: auto; } + [data-workspace-area="conversation"] { background: var(--theme-background-image) center / cover no-repeat; } `); const style = getComputedStyle(probe); @@ -36,6 +43,9 @@ it("applies theme CSS and drops every rule that would load a remote file", () => Array.from(sheet.cssRules, (rule) => rule.cssText), ); expect(kept.join("\n")).not.toContain("example.com"); + expect(kept.join("\n")).toContain( + "background: var(--theme-background-image) center / cover no-repeat;", + ); expect(style.cursor).toBe("auto"); applyThemeCss(undefined); diff --git a/apps/web/src/themeCss.ts b/apps/web/src/themeCss.ts index 437182ce6a191..9a5851e26c88b 100644 --- a/apps/web/src/themeCss.ts +++ b/apps/web/src/themeCss.ts @@ -16,9 +16,19 @@ const isRuleParent = (rule: CSSRule): rule is CSSRule & RuleParent => function stripDeclarations(rule: CSSRule): boolean { if (!("style" in rule) || !(rule.style instanceof CSSStyleDeclaration)) return false; + const pending: string[] = []; for (let item = rule.style.length - 1; item >= 0; item--) { const property = rule.style.item(item); - if (themeCssValueFetches(rule.style.getPropertyValue(property))) { + const value = rule.style.getPropertyValue(property); + if (value === "") pending.push(property); + else if (themeCssValueFetches(value)) rule.style.removeProperty(property); + } + // A shorthand with var() leaves each longhand reading "". cssText shows the shorthand whole, + // or, once another declaration overrides one longhand, lists the rest bare with its text hidden. + const text = rule.style.cssText; + const fetches = themeCssValueFetches(text); + for (const property of pending) { + if (fetches || new RegExp(`(?:^|; )${property}:`).test(text)) { rule.style.removeProperty(property); } } From 074fcb11bab78fe30fe147bdce2e76893da86376 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Thu, 19 Nov 2026 20:31:32 +0000 Subject: [PATCH 2/5] fix(web): keep empty custom properties when stripping var() shorthands An empty custom property (`--on: ;`) also reads "" through getPropertyValue, so the shorthand check removed it. Also trims duplicate regression cases. Co-Authored-By: Claude Opus 5.5 --- apps/web/src/themeCss.browser.test.ts | 9 +++------ apps/web/src/themeCss.ts | 3 ++- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/apps/web/src/themeCss.browser.test.ts b/apps/web/src/themeCss.browser.test.ts index c6098a583533b..6911713169ce5 100644 --- a/apps/web/src/themeCss.browser.test.ts +++ b/apps/web/src/themeCss.browser.test.ts @@ -14,10 +14,7 @@ it("applies theme CSS and drops every rule that would load a remote file", () => @namespace svg url(http://www.w3.org/2000/svg); @property --registered { syntax: ""; inherits: false; initial-value: url(https://example.com/d); } @counter-style leak { system: cyclic; symbols: url(https://example.com/f); } - @keyframes leak { - from { background: var(--none, url(https://example.com/i)); } - to { background-image: url(https://example.com/g); } - } + @keyframes leak { to { background-image: url(https://example.com/g); } } :root { --leak: u\\72l(https://example.com/a); } [data-theme-css-probe] { color: rgb(1, 2, 3); @@ -28,9 +25,8 @@ it("applies theme CSS and drops every rule that would load a remote file", () => @media (min-width: 0px) { cursor: url(https://example.com/c), auto; } } [data-theme-css-probe] { background: var(--none, url(https://example.com/h)); } - [data-theme-css-probe] { list-style: var(--none, u\\72l(https://example.com/j)); } [data-theme-css-probe] { mask: var(--none, url(https://example.com/k)); mask-size: auto; } - [data-workspace-area="conversation"] { background: var(--theme-background-image) center / cover no-repeat; } + [data-workspace-area="conversation"] { --toggle: ; background: var(--theme-background-image) center / cover no-repeat; } `); const style = getComputedStyle(probe); @@ -46,6 +42,7 @@ it("applies theme CSS and drops every rule that would load a remote file", () => expect(kept.join("\n")).toContain( "background: var(--theme-background-image) center / cover no-repeat;", ); + expect(kept.join("\n")).toContain("--toggle:"); expect(style.cursor).toBe("auto"); applyThemeCss(undefined); diff --git a/apps/web/src/themeCss.ts b/apps/web/src/themeCss.ts index 9a5851e26c88b..fce7fc4027166 100644 --- a/apps/web/src/themeCss.ts +++ b/apps/web/src/themeCss.ts @@ -20,7 +20,8 @@ function stripDeclarations(rule: CSSRule): boolean { for (let item = rule.style.length - 1; item >= 0; item--) { const property = rule.style.item(item); const value = rule.style.getPropertyValue(property); - if (value === "") pending.push(property); + // An empty custom property (`--on: ;`) is a real value, not a pending shorthand. + if (value === "" && !property.startsWith("--")) pending.push(property); else if (themeCssValueFetches(value)) rule.style.removeProperty(property); } // A shorthand with var() leaves each longhand reading "". cssText shows the shorthand whole, From c1ff1c34b35aa787b74d0c80979b296820f46cac Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Thu, 19 Nov 2026 20:49:20 +0000 Subject: [PATCH 3/5] fix(web): keep var() shorthands with an override when the theme cannot fetch Overriding one longhand hides a var() shorthand's text from the CSSOM, so the bare-longhand fallback removed harmless borders and gradients. Strip those longhands only when the theme source itself can fetch. Co-Authored-By: Claude Opus 5.5 --- apps/web/src/themeCss.browser.test.ts | 8 ++++++++ apps/web/src/themeCss.ts | 15 ++++++++------- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/apps/web/src/themeCss.browser.test.ts b/apps/web/src/themeCss.browser.test.ts index 6911713169ce5..0be2f632244c9 100644 --- a/apps/web/src/themeCss.browser.test.ts +++ b/apps/web/src/themeCss.browser.test.ts @@ -45,6 +45,14 @@ it("applies theme CSS and drops every rule that would load a remote file", () => expect(kept.join("\n")).toContain("--toggle:"); expect(style.cursor).toBe("auto"); + // Overriding one longhand hides a var() shorthand's text, which is kept when no source can fetch. + applyThemeCss(` + [data-theme-css-probe] { --edge: 4px solid red; border: var(--edge); border-left-width: 0px; } + [data-theme-css-probe] { background: var(--none, linear-gradient(red, blue)); background-color: red; } + `); + expect(style.borderTopWidth).toBe("4px"); + expect(style.backgroundImage).toContain("linear-gradient"); + applyThemeCss(undefined); expect(getComputedStyle(probe).color).not.toBe("rgb(1, 2, 3)"); }); diff --git a/apps/web/src/themeCss.ts b/apps/web/src/themeCss.ts index fce7fc4027166..eb92813dfb5d0 100644 --- a/apps/web/src/themeCss.ts +++ b/apps/web/src/themeCss.ts @@ -14,7 +14,7 @@ type RuleParent = Pick; const isRuleParent = (rule: CSSRule): rule is CSSRule & RuleParent => "cssRules" in rule && "deleteRule" in rule && !(rule instanceof CSSKeyframesRule); -function stripDeclarations(rule: CSSRule): boolean { +function stripDeclarations(rule: CSSRule, sourceFetches: boolean): boolean { if (!("style" in rule) || !(rule.style instanceof CSSStyleDeclaration)) return false; const pending: string[] = []; for (let item = rule.style.length - 1; item >= 0; item--) { @@ -26,10 +26,11 @@ function stripDeclarations(rule: CSSRule): boolean { } // A shorthand with var() leaves each longhand reading "". cssText shows the shorthand whole, // or, once another declaration overrides one longhand, lists the rest bare with its text hidden. + // Only the theme's source still has that text, so a bare one goes when the source can fetch. const text = rule.style.cssText; const fetches = themeCssValueFetches(text); for (const property of pending) { - if (fetches || new RegExp(`(?:^|; )${property}:`).test(text)) { + if (fetches || (sourceFetches && new RegExp(`(?:^|; )${property}:`).test(text))) { rule.style.removeProperty(property); } } @@ -37,15 +38,15 @@ function stripDeclarations(rule: CSSRule): boolean { } /** Keeps only rule shapes it can check, so a new at-rule that fetches is dropped by default. */ -function stripFetches(parent: RuleParent): void { +function stripFetches(parent: RuleParent, sourceFetches: boolean): void { for (let index = parent.cssRules.length - 1; index >= 0; index--) { const rule = parent.cssRules[index]; if (!rule) continue; - const hasStyle = stripDeclarations(rule); + const hasStyle = stripDeclarations(rule, sourceFetches); if (rule instanceof CSSKeyframesRule) { - for (const frame of rule.cssRules) stripDeclarations(frame); + for (const frame of rule.cssRules) stripDeclarations(frame, sourceFetches); } else if (isRuleParent(rule)) { - stripFetches(rule); + stripFetches(rule, sourceFetches); } else if (rule instanceof CSSPropertyRule) { // A registered property's initial value is used wherever var() reads it. if (themeCssValueFetches(rule.initialValue ?? "")) parent.deleteRule(index); @@ -71,5 +72,5 @@ export function applyThemeCss(css: string | undefined): void { document.adoptedStyleSheets = [...document.adoptedStyleSheets, themeSheet]; } themeSheet.replaceSync(next); - stripFetches(themeSheet); + stripFetches(themeSheet, themeCssValueFetches(next)); } From f6c2fd9d4f4e22f76277e6108e0f120a43c8e6a9 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Thu, 19 Nov 2026 21:15:59 +0000 Subject: [PATCH 4/5] fix(web): an unrelated blocked url() no longer drops overridden var() shorthands Gate the bare-longhand fallback on source blocks that use var() and can fetch, instead of a fetch anywhere in the stylesheet. Co-Authored-By: Claude Opus 5.5 --- apps/web/src/themeCss.browser.test.ts | 9 ++++++--- apps/web/src/themeCss.ts | 18 +++++++++--------- packages/shared/src/themeCss.test.ts | 19 ++++++++++++++++++- packages/shared/src/themeCss.ts | 24 ++++++++++++++++++++++++ 4 files changed, 57 insertions(+), 13 deletions(-) diff --git a/apps/web/src/themeCss.browser.test.ts b/apps/web/src/themeCss.browser.test.ts index 0be2f632244c9..48d3fd8cc6239 100644 --- a/apps/web/src/themeCss.browser.test.ts +++ b/apps/web/src/themeCss.browser.test.ts @@ -45,10 +45,13 @@ it("applies theme CSS and drops every rule that would load a remote file", () => expect(kept.join("\n")).toContain("--toggle:"); expect(style.cursor).toBe("auto"); - // Overriding one longhand hides a var() shorthand's text, which is kept when no source can fetch. + // Overriding one longhand hides a var() shorthand's text; a fetch in another rule must not drop it. applyThemeCss(` - [data-theme-css-probe] { --edge: 4px solid red; border: var(--edge); border-left-width: 0px; } - [data-theme-css-probe] { background: var(--none, linear-gradient(red, blue)); background-color: red; } + [data-theme-css-probe] { + --edge: 4px solid red; border: var(--edge); border-left-width: 0px; + background: var(--none, linear-gradient(red, blue)); background-color: red; + } + .unused { cursor: url(https://example.com/x), auto; } `); expect(style.borderTopWidth).toBe("4px"); expect(style.backgroundImage).toContain("linear-gradient"); diff --git a/apps/web/src/themeCss.ts b/apps/web/src/themeCss.ts index eb92813dfb5d0..500e0ecb29fd8 100644 --- a/apps/web/src/themeCss.ts +++ b/apps/web/src/themeCss.ts @@ -1,4 +1,4 @@ -import { themeCssValueFetches } from "@t3tools/shared/themeCss"; +import { themeCssValueFetches, themeCssVarBlockFetches } from "@t3tools/shared/themeCss"; // Theme CSS comes from imported files and from agents, so it must not fetch: a // url() on an attribute selector can send what is on screen to another host. @@ -14,7 +14,7 @@ type RuleParent = Pick; const isRuleParent = (rule: CSSRule): rule is CSSRule & RuleParent => "cssRules" in rule && "deleteRule" in rule && !(rule instanceof CSSKeyframesRule); -function stripDeclarations(rule: CSSRule, sourceFetches: boolean): boolean { +function stripDeclarations(rule: CSSRule, varSourceFetches: boolean): boolean { if (!("style" in rule) || !(rule.style instanceof CSSStyleDeclaration)) return false; const pending: string[] = []; for (let item = rule.style.length - 1; item >= 0; item--) { @@ -26,11 +26,11 @@ function stripDeclarations(rule: CSSRule, sourceFetches: boolean): boolean { } // A shorthand with var() leaves each longhand reading "". cssText shows the shorthand whole, // or, once another declaration overrides one longhand, lists the rest bare with its text hidden. - // Only the theme's source still has that text, so a bare one goes when the source can fetch. + // Only the theme's source still has that text, so a bare one goes when a var() block there fetches. const text = rule.style.cssText; const fetches = themeCssValueFetches(text); for (const property of pending) { - if (fetches || (sourceFetches && new RegExp(`(?:^|; )${property}:`).test(text))) { + if (fetches || (varSourceFetches && new RegExp(`(?:^|; )${property}:`).test(text))) { rule.style.removeProperty(property); } } @@ -38,15 +38,15 @@ function stripDeclarations(rule: CSSRule, sourceFetches: boolean): boolean { } /** Keeps only rule shapes it can check, so a new at-rule that fetches is dropped by default. */ -function stripFetches(parent: RuleParent, sourceFetches: boolean): void { +function stripFetches(parent: RuleParent, varSourceFetches: boolean): void { for (let index = parent.cssRules.length - 1; index >= 0; index--) { const rule = parent.cssRules[index]; if (!rule) continue; - const hasStyle = stripDeclarations(rule, sourceFetches); + const hasStyle = stripDeclarations(rule, varSourceFetches); if (rule instanceof CSSKeyframesRule) { - for (const frame of rule.cssRules) stripDeclarations(frame, sourceFetches); + for (const frame of rule.cssRules) stripDeclarations(frame, varSourceFetches); } else if (isRuleParent(rule)) { - stripFetches(rule, sourceFetches); + stripFetches(rule, varSourceFetches); } else if (rule instanceof CSSPropertyRule) { // A registered property's initial value is used wherever var() reads it. if (themeCssValueFetches(rule.initialValue ?? "")) parent.deleteRule(index); @@ -72,5 +72,5 @@ export function applyThemeCss(css: string | undefined): void { document.adoptedStyleSheets = [...document.adoptedStyleSheets, themeSheet]; } themeSheet.replaceSync(next); - stripFetches(themeSheet, themeCssValueFetches(next)); + stripFetches(themeSheet, themeCssVarBlockFetches(next)); } diff --git a/packages/shared/src/themeCss.test.ts b/packages/shared/src/themeCss.test.ts index 38cc33a793dbd..ed541379d72ea 100644 --- a/packages/shared/src/themeCss.test.ts +++ b/packages/shared/src/themeCss.test.ts @@ -1,6 +1,10 @@ import { describe, expect, it } from "vite-plus/test"; -import { themeCssLoadsRemoteFiles, themeCssValueFetches } from "./themeCss.ts"; +import { + themeCssLoadsRemoteFiles, + themeCssValueFetches, + themeCssVarBlockFetches, +} from "./themeCss.ts"; // %23 decodes to # inside the SVG, so url(%23g) names its own gradient. const SVG_WITH_OWN_GRADIENT = @@ -59,3 +63,16 @@ describe("themeCssLoadsRemoteFiles", () => { ).toBe(false); }); }); + +describe("themeCssVarBlockFetches", () => { + it.each([ + ["a { border: var(--edge); } b { cursor: url(https://e.com/x), auto; }", false], + ["a { mask: var(--none, url(https://e.com/k)); mask-size: auto; }", true], + ['a { background: var(--none, "{" url(https://e.com/k)); }', true], + ["a { background: var(--none, { } url(https://e.com/k)); }", true], + ["a { background: v\\61r(--none, url(https://e.com/k)); }", true], + ["a { background: var(--none, url(data:image/png;base64,AA)); }", false], + ])("%s -> %s", (css, fetches) => { + expect(themeCssVarBlockFetches(css)).toBe(fetches); + }); +}); diff --git a/packages/shared/src/themeCss.ts b/packages/shared/src/themeCss.ts index 9668040f97be4..63dc28d023e70 100644 --- a/packages/shared/src/themeCss.ts +++ b/packages/shared/src/themeCss.ts @@ -45,6 +45,30 @@ export function themeCssValueFetches(value: string): boolean { return code.includes("\\") || fetchesOutsideStrings(code); } +/** + * Whether a declaration block that uses var(), or holds an escape, can load a remote file. Once a + * later declaration overrides one longhand, browsers hide a var() shorthand's text, so only the + * source shows it. Blocks split at braces outside parentheses, after strings are removed. + */ +export function themeCssVarBlockFetches(css: string): boolean { + const code = withoutStrings(css, false); + let depth = 0; + let start = 0; + for (let index = 0; index <= code.length; index++) { + const char = code[index]; + if (char === "(") depth++; + else if (char === ")") depth = Math.max(0, depth - 1); + else if (index === code.length || (depth === 0 && (char === "{" || char === "}"))) { + const block = code.slice(start, index); + if (block.includes("\\") || (/var\(/i.test(block) && fetchesOutsideStrings(block))) { + return true; + } + start = index + 1; + } + } + return false; +} + /** A text check for early feedback. CSS escapes can dodge it; the client check is the guard. */ export function themeCssLoadsRemoteFiles(css: string): boolean { const code = withoutStrings(css, true); From 993b47ee8f90c5f0304b54597176aa0f6ded7089 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Thu, 19 Nov 2026 21:34:33 +0000 Subject: [PATCH 5/5] fix(web): env(), attr() and if() shorthands count as substitutions too An overridden env() shorthand hid its fallback url() from the source check, which only looked for var(). Co-Authored-By: Claude Opus 5.5 --- apps/web/src/themeCss.browser.test.ts | 5 +++++ packages/shared/src/themeCss.test.ts | 1 + packages/shared/src/themeCss.ts | 11 +++++++---- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/apps/web/src/themeCss.browser.test.ts b/apps/web/src/themeCss.browser.test.ts index 48d3fd8cc6239..20f0d9f47ac64 100644 --- a/apps/web/src/themeCss.browser.test.ts +++ b/apps/web/src/themeCss.browser.test.ts @@ -56,6 +56,11 @@ it("applies theme CSS and drops every rule that would load a remote file", () => expect(style.borderTopWidth).toBe("4px"); expect(style.backgroundImage).toContain("linear-gradient"); + applyThemeCss(` + [data-theme-css-probe] { list-style: env(t3-none, url(https://example.com/l)); list-style-type: disc; } + `); + expect(style.listStyleImage).toBe("none"); + applyThemeCss(undefined); expect(getComputedStyle(probe).color).not.toBe("rgb(1, 2, 3)"); }); diff --git a/packages/shared/src/themeCss.test.ts b/packages/shared/src/themeCss.test.ts index ed541379d72ea..c63951d47ce77 100644 --- a/packages/shared/src/themeCss.test.ts +++ b/packages/shared/src/themeCss.test.ts @@ -68,6 +68,7 @@ describe("themeCssVarBlockFetches", () => { it.each([ ["a { border: var(--edge); } b { cursor: url(https://e.com/x), auto; }", false], ["a { mask: var(--none, url(https://e.com/k)); mask-size: auto; }", true], + ["a { background: env(t3-none, url(https://e.com/l)); background-color: red; }", true], ['a { background: var(--none, "{" url(https://e.com/k)); }', true], ["a { background: var(--none, { } url(https://e.com/k)); }", true], ["a { background: v\\61r(--none, url(https://e.com/k)); }", true], diff --git a/packages/shared/src/themeCss.ts b/packages/shared/src/themeCss.ts index 63dc28d023e70..e017eaeb65b99 100644 --- a/packages/shared/src/themeCss.ts +++ b/packages/shared/src/themeCss.ts @@ -45,10 +45,13 @@ export function themeCssValueFetches(value: string): boolean { return code.includes("\\") || fetchesOutsideStrings(code); } +const SUBSTITUTES = /(?:var|env|attr|if)\(/i; + /** - * Whether a declaration block that uses var(), or holds an escape, can load a remote file. Once a - * later declaration overrides one longhand, browsers hide a var() shorthand's text, so only the - * source shows it. Blocks split at braces outside parentheses, after strings are removed. + * Whether a declaration block that uses var(), env(), attr() or if(), or holds an escape, can + * load a remote file. Once a later declaration overrides one longhand, browsers hide such a + * shorthand's text, so only the source shows it. Blocks split at braces outside parentheses, + * after strings are removed. */ export function themeCssVarBlockFetches(css: string): boolean { const code = withoutStrings(css, false); @@ -60,7 +63,7 @@ export function themeCssVarBlockFetches(css: string): boolean { else if (char === ")") depth = Math.max(0, depth - 1); else if (index === code.length || (depth === 0 && (char === "{" || char === "}"))) { const block = code.slice(start, index); - if (block.includes("\\") || (/var\(/i.test(block) && fetchesOutsideStrings(block))) { + if (block.includes("\\") || (SUBSTITUTES.test(block) && fetchesOutsideStrings(block))) { return true; } start = index + 1;