:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(canvas): reject CSS variables that collide with Quill element tokens (#94311)
Peter Kirkham0cb5a2d5fd70
diff --git a/products/canvas/backend/source.py b/products/canvas/backend/source.pyindex de9435eb49a1..e0a8ed981d9b 100644--- a/products/canvas/backend/source.py+++ b/products/canvas/backend/source.py@@ -67,6 +67,38 @@ ALLOWED_CONFIG_SCHEMA_KEYWORDS = frozenset(# File extensions whose content is scanned as source code._CODE_EXTENSIONS = (".ts", ".tsx", ".js", ".jsx")+# Files scanned for CSS custom property declarations: stylesheets, the entry+# HTML, and code, because inline <style> blocks and CSS-in-JS carry CSS as+# strings.+_STYLE_EXTENSIONS = (".css", ".html", *_CODE_EXTENSIONS)++# Quill design tokens that the bundled platform stylesheet (@posthog/quill+# color-system.css) declares on the universal selector `*`, not on `:root`.+# Every element then gets Quill's value directly, so an author's `:root` or+# `html.dark` declaration of the same name never reaches any descendant and+# text colored with `var(--muted)` silently becomes Quill's pale surface color.+# Keep in sync with the `* { ... }` rule of the pinned Quill version.+_PLATFORM_ELEMENT_TOKENS = frozenset(+ {+ "background",+ "border",+ "card",+ "chrome",+ "fill-expanded",+ "fill-hover",+ "fill-selected",+ "input",+ "muted",+ "primary",+ }+)+# A declaration sits at the start of a line or right after `{`, `;`, or `,`+# (CSS rule bodies and inline-style object keys). Requiring that context keeps+# a comment such as `// --muted: legacy` or a `var(--muted)` use from matching.+_PLATFORM_TOKEN_DECLARATION_RE = re.compile(+ r"(?:^|[{;,])\s*[\"']?--(" + "|".join(sorted(_PLATFORM_ELEMENT_TOKENS)) + r")[\"']?\s*:",+ re.MULTILINE,+)# The synthetic entry shell presented for pre-relational canvases whose only# source is a single stored React component.@@ -296,6 +328,24 @@ def _validate_network_capabilities(path: str, content: str, declared_origins: sereturn diagnostics+def _validate_platform_tokens(path: str, content: str) -> list[dict[str, Any]]:+ diagnostics: list[dict[str, Any]] = []+ for match in _PLATFORM_TOKEN_DECLARATION_RE.finditer(content):+ token = match.group(1)+ diagnostics.append(+ diagnostic(+ "error",+ "platform_token_redeclared",+ f"--{token} is a platform design token that the platform stylesheet sets on every element, "+ f"so this declaration is ignored and text colored with var(--{token}) can become unreadable. "+ f"Rename it, for example --canvas-{token}, or use the platform token without redefining it",+ path=path,+ line=_line_of(content, match.start(1)),+ )+ )+ return diagnostics++def _validate_code_file(path: str, code: str) -> list[dict[str, Any]]:diagnostics: list[dict[str, Any]] = []@@ -739,6 +789,8 @@ def validate_source_project(project: dict[str, Any], *, kind: str = "freeform")if not isinstance(content, str):continuediagnostics.extend(_validate_network_capabilities(path, content, declared_network_origins))+ if path.endswith(_STYLE_EXTENSIONS):+ diagnostics.extend(_validate_platform_tokens(path, content))if path.endswith(_CODE_EXTENSIONS):diagnostics.extend(_validate_code_file(path, content))diagnostics.extend(_validate_capabilities(path, content, capabilities))diff --git a/products/canvas/backend/tests/test_cloud_builder.py b/products/canvas/backend/tests/test_cloud_builder.pyindex c35bad73b775..7d9fcd020b5a 100644--- a/products/canvas/backend/tests/test_cloud_builder.py+++ b/products/canvas/backend/tests/test_cloud_builder.py@@ -1,4 +1,5 @@import os+import reimport hashlibimport tempfileimport subprocess@@ -14,7 +15,7 @@ from parameterized import parameterizedfrom products.canvas.backend.build_service import node_executable, run_cloud_builder, validate_builder_outputfrom products.canvas.backend.contract import allowed_import_specifiers, canvas_sdk_version, platform_dependenciesfrom products.canvas.backend.presentation.serializers import CanvasSourceProjectSerializer-from products.canvas.backend.source import synthetic_source_project, validate_source_project+from products.canvas.backend.source import _PLATFORM_ELEMENT_TOKENS, synthetic_source_project, validate_source_projectclass TestCanvasCloudBuilder(SimpleTestCase):@@ -55,6 +56,13 @@ class TestCanvasCloudBuilder(SimpleTestCase):self.assertIn(".md\\:grid-cols-2", stylesheet["content"])self.assertIn(".quill-button", stylesheet["content"])self.assertIn("--background", stylesheet["content"])+ # Source validation rejects author declarations of the tokens Quill sets+ # on every element; that list must follow the pinned Quill version.+ universal_rule = re.search(r"^\* \{\n(.*?)^\}", stylesheet["content"], re.MULTILINE | re.DOTALL)+ assert universal_rule is not None+ self.assertEqual(+ set(re.findall(r"^\s*--([\w-]+):", universal_rule.group(1), re.MULTILINE)), _PLATFORM_ELEMENT_TOKENS+ )def test_publication_validation_allows_relative_worker_and_asset_imports(self) -> None:payload = synthetic_source_project(diff --git a/products/canvas/backend/tests/test_source.py b/products/canvas/backend/tests/test_source.pyindex e83643116f2d..d32011ae87df 100644--- a/products/canvas/backend/tests/test_source.py+++ b/products/canvas/backend/tests/test_source.py@@ -137,6 +137,23 @@ class TestCanvasSourceAdapter(SimpleTestCase):project(files={CANVAS_COMPONENT_PATH: 'const html = "<script src=x></script>";'}),"forbidden_inline_script",),+ (+ "platform_token_in_stylesheet",+ project(files={CANVAS_COMPONENT_PATH: CODE, "src/styles.css": ":root { --muted: #5d5a52; }"}),+ "platform_token_redeclared",+ ),+ (+ "platform_token_in_inline_style",+ project(files={CANVAS_COMPONENT_PATH: CODE + "const css = `html.dark { --background:#141416 }`;"}),+ "platform_token_redeclared",+ ),+ (+ "platform_token_in_entry_html",+ project(+ files={CANVAS_ENTRY_HTML: '<style>\n:root {\n --muted: #5d5a52;\n}\n</style><div id="root"></div>'}+ ),+ "platform_token_redeclared",+ ),("file_too_large",project(files={CANVAS_COMPONENT_PATH: "a" * (MAX_FILE_BYTES + 1)}),@@ -193,6 +210,25 @@ class TestCanvasSourceAdapter(SimpleTestCase):diagnostics = validate_source_project(candidate)self.assertNotIn("capability_missing_state", [d["code"] for d in diagnostics])+ @parameterized.expand(+ [+ (+ "prefixed_and_suffixed_names",+ "src/styles.css",+ ":root { --doc-muted: #5d5a52; --muted-foreground: #444; } .lead { color: var(--muted); }",+ ),+ ("comment_mentioning_token", CANVAS_COMPONENT_PATH, CODE + "// --muted: legacy name, do not use\n"),+ (+ "prose_mentioning_token",+ CANVAS_COMPONENT_PATH,+ CODE + 'const hint = "Rename --muted: it is reserved";\n',+ ),+ ]+ )+ def test_platform_token_uses_and_mentions_pass(self, _name: str, path: str, content: str) -> None:+ candidate = project(files={CANVAS_COMPONENT_PATH: CODE, path: content})+ self.assertEqual(validate_source_project(candidate), [])+def test_direct_network_calls_warn_but_stay_publishable(self):candidate = project(files={CANVAS_COMPONENT_PATH: CODE + "fetch(dynamicUrl);"})diagnostics = validate_source_project(candidate)diff --git a/products/canvas/skills/building-html-canvases/SKILL.md b/products/canvas/skills/building-html-canvases/SKILL.mdindex b6ff60d13773..0408ec28c852 100644--- a/products/canvas/skills/building-html-canvases/SKILL.md+++ b/products/canvas/skills/building-html-canvases/SKILL.md@@ -47,6 +47,12 @@ build pipeline's dependency admission ships.Define your colors as CSS variables under `:root { … }` with overrides under `html.dark { … }`,or use theme token utilities (`bg-background`, `text-foreground`, `border-border`) — never alight-only hardcoded color.+- Give your own CSS variables a prefix (`--doc-bg`, `--doc-muted`). Never reuse a platform token+ name: the bundled Quill stylesheet sets `--background`, `--border`, `--card`, `--chrome`,+ `--input`, `--muted`, `--primary`, and `--fill-*` on every element, so a `:root` or `html.dark`+ value with one of those names never reaches any element. A page that colors its text with its own+ `--muted` then renders unreadable (pale text on a pale page). Validation rejects such a+ declaration with `platform_token_redeclared`.- For canvas/WebGL drawing colors, read the resolved token at runtime(`getComputedStyle(document.documentElement).getPropertyValue("--primary")`) or your own CSSvariables, and re-read on theme change if the scene is long-lived.diff --git a/products/canvas/skills/building-react-quill-canvases/SKILL.md b/products/canvas/skills/building-react-quill-canvases/SKILL.mdindex b36c115eafba..59809c973315 100644--- a/products/canvas/skills/building-react-quill-canvases/SKILL.md+++ b/products/canvas/skills/building-react-quill-canvases/SKILL.md@@ -71,6 +71,11 @@ text-card-foreground`; borders `border-border`. Never a hardcoded hex or light-oalways use the `-foreground` utility; a filled pill pairs `bg-success text-success-foreground`.Prefer the Quill `Badge` (`variant="success"`/`"destructive"`) for deltas so you don't hand-pick.- `bg-secondary`, `text-secondary`, `bg-accent`, and `bg-popover` are not defined in the canvas — avoid them.+- Never declare a CSS variable with a platform token name (`--background`, `--border`, `--card`,+ `--chrome`, `--input`, `--muted`, `--primary`, `--fill-*`), in a stylesheet or a `<style>` block.+ Quill sets those on every element, so your value never applies and text can turn unreadable.+ Prefix your own variables (`--doc-muted`); validation rejects the collision with+ `platform_token_redeclared`.- recharts strokes/fills use token CSS variables (`stroke="var(--primary)"`, grid/axes in`var(--border)`/`var(--muted-foreground)`).- Write Unicode glyphs (curly quotes, ellipsis, arrows, emoji) as literal characters in JSX —diff --git a/products/canvas/skills/validating-and-publishing-canvases/SKILL.md b/products/canvas/skills/validating-and-publishing-canvases/SKILL.mdindex 7127d0e090fe..e6fa4382b8b9 100644--- a/products/canvas/skills/validating-and-publishing-canvases/SKILL.md+++ b/products/canvas/skills/validating-and-publishing-canvases/SKILL.md@@ -69,7 +69,9 @@ Diagnostics carry `severity`, a stable `code`, a `message`, and (for file-specif`invalid_path`, `capability_missing_insight` / `capability_missing_capture_event` /`capability_missing_inline_queries` / `capability_missing_agent_requests` /`capability_missing_network_origin`,- `dependency_not_admitted` / `dependency_version_mismatch`, and path/size violations.+ `dependency_not_admitted` / `dependency_version_mismatch`, `platform_token_redeclared` (a CSS+ variable named like a Quill token that the platform stylesheet sets on every element, so the+ value never applies; prefix your own variables), and path/size violations.- `warning` diagnostics don't block, but heed them: `network_fetch` / `network_xhr` mean the codereaches for the network directly. Declare the exact HTTPS origin or use the `ph` bridge.