Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import { themeCssLoadsRemoteFiles, themeCssValueFetches } from "./themeCss.ts";
4
5// %23 decodes to # inside the SVG, so url(%23g) names its own gradient.
6const 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
9describe("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 ["filter", 'url("#blur")', false],
25 ["background-image", "var(--pattern)", false],
26 ["color", "red", false],
27 ])("%s: %s", (_property, value, fetches) => {
28 expect(themeCssValueFetches(value)).toBe(fetches);
29 });
30});
31
32describe("themeCssLoadsRemoteFiles", () => {
33 it("allows a url() inside a data: address", () => {
34 expect(
35 themeCssLoadsRemoteFiles(`.repro { background-image: url('${SVG_WITH_OWN_GRADIENT}'); }`),
36 ).toBe(false);
37 });
38});