Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import {
4 themeCssLoadsRemoteFiles,
5 themeCssValueFetches,
6 themeCssWithoutDeclarations,
7} from "./themeCss.ts";
8
9// %23 decodes to # inside the SVG, so url(%23g) names its own gradient.
10const SVG_WITH_OWN_GRADIENT =
11 "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";
12
13describe("themeCssValueFetches", () => {
14 it.each([
15 ["background-image", 'url("https://example.com/a")', true],
16 ["background-image", 'url("/api/thing")', true],
17 ["background-image", 'image-set("https://example.com/a" 1x)', true],
18 // An unparsed value keeps its escapes, so an escaped function name hides in it.
19 ["--leak", "u\\72l(https://example.com/a)", true],
20 ["background-image", "im\\61ge-set(var(--leak) 1x)", true],
21 ["background-image", "v\\61r(--none, u\\72l(https://example.com/a))", true],
22 // A no-break space before the scheme makes this a path on the app's own origin.
23 ["background-image", 'url("\u00a0data:/../../probe")', true],
24 ["background-image", 'url("data:image/png;base64,AA")', false],
25 ["background-image", `url("${SVG_WITH_OWN_GRADIENT}")`, false],
26 // The url( sits inside a string, so the address after that string is a real remote url().
27 ["content", `"url('data:" url("https://evil.example/a") "'"`, true],
28 ["--leak", '/* " */ url(https://evil.example/a) /* " */', true],
29 ["content", String.raw`"Theme preview: \"ready\""`, false],
30 ["--label", String.raw`'It\'s ready'`, false],
31 ["content", String.raw`url("#icon") / "The \"ready\" icon"`, false],
32 ["filter", 'url("#blur")', false],
33 ["background-image", "var(--pattern)", false],
34 ["color", "red", false],
35 ])("%s: %s", (_property, value, fetches) => {
36 expect(themeCssValueFetches(value)).toBe(fetches);
37 });
38});
39
40describe("themeCssLoadsRemoteFiles", () => {
41 it.each([
42 ['[data-chat-header]::after { content: "@import"; }', false],
43 ['.a::after { content: "say \\"@import\\""; }', false],
44 ['.a::after { content: "a\\\r\n@import"; }', false],
45 ['.a::after { content: "\\22\r\n@import"; }', false],
46 ['.a::after { content: "/*"; } /* note */ .b::after { content: "@import"; }', false],
47 ['/* @import "x.css"; */ .a::after { content: "*/"; }', false],
48 [
49 ".a { background-image: url(data:image/svg+xml,%3Csvg%20xmlns=%22http://www.w3.org/2000/svg%22%3E%3C!--/*@import*/--%3E%3C/svg%3E); }",
50 false,
51 ],
52 ['@import "x.css";', true],
53 ['.a\\" {} @import "x.css"; .b\\" {}', true],
54 ['.a::after { content: "\\\\"; } @import "x.css";', true],
55 [".a { background: url(https://example.invalid/x.png); }", true],
56 ])("%s", (css, loads) => {
57 expect(themeCssLoadsRemoteFiles(css)).toBe(loads);
58 });
59
60 it("allows a url() inside a data: address", () => {
61 expect(
62 themeCssLoadsRemoteFiles(`.repro { background-image: url('${SVG_WITH_OWN_GRADIENT}'); }`),
63 ).toBe(false);
64 });
65});
66
67describe("themeCssWithoutDeclarations", () => {
68 it.each([
69 // A harmless var() shorthand and an escape elsewhere name no shorthand that fetches.
70 ["a { border: var(--edge); border-left-width: 0; } .b { c\\6flor: red; }", [" c\\6flor: red"]],
71 [
72 "a { mask: var(--none, url(https://e.com/k)); mask-size: auto; }",
73 [" mask: var(--none, url(https://e.com/k))"],
74 ],
75 ["a { background: var(--none, url(data:image/png;base64,AA)); color: red }", []],
76 // Brackets, strings, comments and url() addresses do not end a declaration.
77 [
78 'a { background: var(--none, { ; } "}" /* } */ [;] url(https://e.com/;}) url(x)); b: c }',
79 [' background: var(--none, { ; } "}" /* } */ [;] url(https://e.com/;}) url(x))'],
80 ],
81 // An unquoted url() ends at its first unescaped ")", so the quote after this one opens a string.
82 [
83 'a { color: u\\72l(a"b) c" ); background: var(--none, url(https://e.com/k)); d: "" }',
84 [' color: u\\72l(a"b) c" ); background: var(--none, url(https://e.com/k)); d: "" '],
85 ],
86 [
87 "a { color: url(a\\)b;c); background: v\\61r(--none, url(x)) }",
88 [" color: url(a\\)b;c)", " background: v\\61r(--none, url(x)) "],
89 ],
90 // A closing bracket of another kind closes nothing, so the declaration stays whole.
91 ["a { background: var(--none, ( ] ; url(x))); }", [" background: var(--none, ( ] ; url(x)))"]],
92 ])("%s", (css, candidates) => {
93 const asked: string[] = [];
94 expect(themeCssWithoutDeclarations(css, (candidate) => asked.push(candidate) < 0)).toBe(css);
95 expect(asked).toEqual(candidates);
96 });
97
98 it("removes only the declarations that are refused", () => {
99 expect(
100 themeCssWithoutDeclarations(
101 "a { mask: var(--none, url(https://e.com/k)); mask-size: auto } b { cursor: url(x) }",
102 (candidate) => candidate.includes("mask"),
103 ),
104 ).toBe("a {; mask-size: auto } b { cursor: url(x) }");
105 });
106});