Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

themeCss.test.ts

113 lines5.2 KB

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 [
27 "background-image",
28 String.raw`url("data:image/svg+xml,<svg xmlns=\"http://www.w3.org/2000/svg\"><circle cx=\"4\" cy=\"4\" r=\"2\" fill=\"red\"/></svg>")`,
29 false,
30 ],
31 ["background-image", String.raw`url("\64 ata:image/png;base64,AA")`, true],
32 ["background-image", String.raw`url("https://example.com/\"a")`, true],
33 // The url( sits inside a string, so the address after that string is a real remote url().
34 ["content", `"url('data:" url("https://evil.example/a") "'"`, true],
35 ["--leak", '/* " */ url(https://evil.example/a) /* " */', true],
36 ["content", String.raw`"Theme preview: \"ready\""`, false],
37 ["--label", String.raw`'It\'s ready'`, false],
38 ["content", String.raw`url("#icon") / "The \"ready\" icon"`, false],
39 ["filter", 'url("#blur")', false],
40 ["background-image", "var(--pattern)", false],
41 ["color", "red", false],
42 ])("%s: %s", (_property, value, fetches) => {
43 expect(themeCssValueFetches(value)).toBe(fetches);
44 });
45});
46​
47describe("themeCssLoadsRemoteFiles", () => {
48 it.each([
49 ['[data-chat-header]::after { content: "@import"; }', false],
50 ['.a::after { content: "say \\"@import\\""; }', false],
51 ['.a::after { content: "a\\\r\n@import"; }', false],
52 ['.a::after { content: "\\22\r\n@import"; }', false],
53 ['.a::after { content: "/*"; } /* note */ .b::after { content: "@import"; }', false],
54 ['/* @import "x.css"; */ .a::after { content: "*/"; }', false],
55 [
56 ".a { background-image: url(data:image/svg+xml,%3Csvg%20xmlns=%22http://www.w3.org/2000/svg%22%3E%3C!--/*@import*/--%3E%3C/svg%3E); }",
57 false,
58 ],
59 ['@import "x.css";', true],
60 ['.a\\" {} @import "x.css"; .b\\" {}', true],
61 ['.a::after { content: "\\\\"; } @import "x.css";', true],
62 [".a { background: url(https://example.invalid/x.png); }", true],
63 ])("%s", (css, loads) => {
64 expect(themeCssLoadsRemoteFiles(css)).toBe(loads);
65 });
66​
67 it("allows a url() inside a data: address", () => {
68 expect(
69 themeCssLoadsRemoteFiles(`.repro { background-image: url('${SVG_WITH_OWN_GRADIENT}'); }`),
70 ).toBe(false);
71 });
72});
73​
74describe("themeCssWithoutDeclarations", () => {
75 it.each([
76 // A harmless var() shorthand and an escape elsewhere name no shorthand that fetches.
77 ["a { border: var(--edge); border-left-width: 0; } .b { c\\6flor: red; }", [" c\\6flor: red"]],
78 [
79 "a { mask: var(--none, url(https://e.com/k)); mask-size: auto; }",
80 [" mask: var(--none, url(https://e.com/k))"],
81 ],
82 ["a { background: var(--none, url(data:image/png;base64,AA)); color: red }", []],
83 // Brackets, strings, comments and url() addresses do not end a declaration.
84 [
85 'a { background: var(--none, { ; } "}" /* } */ [;] url(https://e.com/;}) url(x)); b: c }',
86 [' background: var(--none, { ; } "}" /* } */ [;] url(https://e.com/;}) url(x))'],
87 ],
88 // An unquoted url() ends at its first unescaped ")", so the quote after this one opens a string.
89 [
90 'a { color: u\\72l(a"b) c" ); background: var(--none, url(https://e.com/k)); d: "" }',
91 [' color: u\\72l(a"b) c" ); background: var(--none, url(https://e.com/k)); d: "" '],
92 ],
93 [
94 "a { color: url(a\\)b;c); background: v\\61r(--none, url(x)) }",
95 [" color: url(a\\)b;c)", " background: v\\61r(--none, url(x)) "],
96 ],
97 // A closing bracket of another kind closes nothing, so the declaration stays whole.
98 ["a { background: var(--none, ( ] ; url(x))); }", [" background: var(--none, ( ] ; url(x)))"]],
99 ])("%s", (css, candidates) => {
100 const asked: string[] = [];
101 expect(themeCssWithoutDeclarations(css, (candidate) => asked.push(candidate) < 0)).toBe(css);
102 expect(asked).toEqual(candidates);
103 });
104​
105 it("removes only the declarations that are refused", () => {
106 expect(
107 themeCssWithoutDeclarations(
108 "a { mask: var(--none, url(https://e.com/k)); mask-size: auto } b { cursor: url(x) }",
109 (candidate) => candidate.includes("mask"),
110 ),
111 ).toBe("a {; mask-size: auto } b { cursor: url(x) }");
112 });
113});