The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { describe, expect, it } from "vite-plus/test"; |
| 2 | |
| 3 | import { themeCssLoadsRemoteFiles, themeCssValueFetches } from "./themeCss.ts"; |
| 4 | |
| 5 | // %23 decodes to # inside the SVG, so url(%23g) names its own gradient. |
| 6 | const SVG_WITH_OWN_GRADIENT = |
| 7 | "data:image/svg+xml,%3Csvg xmlns=%22http://www.w3.org/2000/svg%22%3E%3ClinearGradient id=%22g%22/%3E%3Crect fill=%22url(%23g)%22/%3E%3C/svg%3E"; |
| 8 | |
| 9 | describe("themeCssValueFetches", () => { |
| 10 | it.each([ |
| 11 | ["background-image", 'url("https://example.com/a")', true], |
| 12 | ["background-image", 'url("/api/thing")', true], |
| 13 | ["background-image", 'image-set("https://example.com/a" 1x)', true], |
| 14 | // An unparsed value keeps its escapes, so an escaped function name hides in it. |
| 15 | ["--leak", "u\\72l(https://example.com/a)", true], |
| 16 | ["background-image", "im\\61ge-set(var(--leak) 1x)", true], |
| 17 | ["background-image", "v\\61r(--none, u\\72l(https://example.com/a))", true], |
| 18 | // A no-break space before the scheme makes this a path on the app's own origin. |
| 19 | ["background-image", 'url("\u00a0data:/../../probe")', true], |
| 20 | ["background-image", 'url("data:image/png;base64,AA")', false], |
| 21 | ["background-image", `url("${SVG_WITH_OWN_GRADIENT}")`, false], |
| 22 | // The url( sits inside a string, so the address after that string is a real remote url(). |
| 23 | ["content", `"url('data:" url("https://evil.example/a") "'"`, true], |
| 24 | ["--leak", '/* " */ url(https://evil.example/a) /* " */', true], |
| 25 | ["content", String.raw`"Theme preview: \"ready\""`, false], |
| 26 | ["--label", String.raw`'It\'s ready'`, false], |
| 27 | ["content", String.raw`url("#icon") / "The \"ready\" icon"`, false], |
| 28 | ["filter", 'url("#blur")', false], |
| 29 | ["background-image", "var(--pattern)", false], |
| 30 | ["color", "red", false], |
| 31 | ])("%s: %s", (_property, value, fetches) => { |
| 32 | expect(themeCssValueFetches(value)).toBe(fetches); |
| 33 | }); |
| 34 | }); |
| 35 | |
| 36 | describe("themeCssLoadsRemoteFiles", () => { |
| 37 | it.each([ |
| 38 | ['[data-chat-header]::after { content: "@import"; }', false], |
| 39 | ['.a::after { content: "say \\"@import\\""; }', false], |
| 40 | ['.a::after { content: "a\\\r\n@import"; }', false], |
| 41 | ['.a::after { content: "\\22\r\n@import"; }', false], |
| 42 | ['.a::after { content: "/*"; } /* note */ .b::after { content: "@import"; }', false], |
| 43 | ['/* @import "x.css"; */ .a::after { content: "*/"; }', false], |
| 44 | [ |
| 45 | ".a { background-image: url(data:image/svg+xml,%3Csvg%20xmlns=%22http://www.w3.org/2000/svg%22%3E%3C!--/*@import*/--%3E%3C/svg%3E); }", |
| 46 | false, |
| 47 | ], |
| 48 | ['@import "x.css";', true], |
| 49 | ['.a\\" {} @import "x.css"; .b\\" {}', true], |
| 50 | ['.a::after { content: "\\\\"; } @import "x.css";', true], |
| 51 | [".a { background: url(https://example.invalid/x.png); }", true], |
| 52 | ])("%s", (css, loads) => { |
| 53 | expect(themeCssLoadsRemoteFiles(css)).toBe(loads); |
| 54 | }); |
| 55 | |
| 56 | it("allows a url() inside a data: address", () => { |
| 57 | expect( |
| 58 | themeCssLoadsRemoteFiles(`.repro { background-image: url('${SVG_WITH_OWN_GRADIENT}'); }`), |
| 59 | ).toBe(false); |
| 60 | }); |
| 61 | }); |