diff --git a/packages/cli/src/registry/registryExamples.test.ts b/packages/cli/src/registry/registryExamples.test.ts new file mode 100644 index 0000000000..8eb23a59b8 --- /dev/null +++ b/packages/cli/src/registry/registryExamples.test.ts @@ -0,0 +1,95 @@ +import { readFileSync, readdirSync } from "node:fs"; +import { dirname, join, relative, resolve } from "node:path"; +import { fileURLToPath } from "node:url"; +import { describe, expect, it } from "vitest"; + +const examplesDir = resolve( + dirname(fileURLToPath(import.meta.url)), + "../../../../registry/examples", +); + +// Outside hosts the starters already load from: the script CDN, the font +// service, and HeyGen's own asset host. Anything else a starter shows has to +// ship with it, so a new project keeps rendering the same frames whatever +// happens to an outside site. Do not grow this list for a new starter. +const knownHosts = new Set([ + "cdn.jsdelivr.net", + "fonts.googleapis.com", + "fonts.gstatic.com", + "static.heygen.ai", +]); + +const scannedFile = /\.(?:html|css|svg|js|mjs)$/i; +// Bundled third-party builds cite outside pages in their license comments. +const vendoredDir = /(?:^|\/)vendor\//i; +const minifiedFile = /\.min\.js$/i; + +function remoteHosts(text: string): string[] { + // XML namespace names are identifiers, never fetched. + const withoutNamespaces = text + .replace(/\bxmlns(?::[\w-]+)?=(["'])[^"']*\1/gi, "") + .replace(/http:\/\/www\.w3\.org\/(?:1998|1999|2000)\/[\w/]+/g, ""); + const hosts: string[] = []; + // A scheme, or a protocol-relative "//" that opens a quoted or url() value. + const pattern = /(?:https?:\\?\/\\?\/|(?<=["'`(=]\s*)\/\/)(?:[^\s/"'()?#\\@]*@)?([a-z0-9.-]+)/gi; + for (const match of withoutNamespaces.matchAll(pattern)) { + hosts.push((match[1] ?? "").toLowerCase()); + } + return hosts; +} + +function starterFiles(dir: string): string[] { + return readdirSync(dir, { withFileTypes: true }).flatMap((entry) => { + const path = join(dir, entry.name); + return entry.isDirectory() ? starterFiles(path) : [path]; + }); +} + +describe("registry examples", () => { + it("finds a host in each way a starter file can name one", () => { + expect(remoteHosts(`background: url("https://textures.example/paper.png");`)).toEqual([ + "textures.example", + ]); + expect(remoteHosts(``)).toEqual(["images.example"]); + expect(remoteHosts(`url(//img.example/a.png)`)).toEqual(["img.example"]); + expect(remoteHosts("const src = `//img.example/a.png`;")).toEqual(["img.example"]); + expect(remoteHosts(``)).toEqual(["img.example"]); + expect(remoteHosts(`url(https://img.example#a)`)).toEqual(["img.example"]); + expect(remoteHosts(`"https:\\/\\/img.example\\/a.png"`)).toEqual(["img.example"]); + expect(remoteHosts(``)).toEqual(["203.0.113.9"]); + expect(remoteHosts(`fetch("http://localhost:3000/a.png")`)).toEqual(["localhost"]); + expect(remoteHosts(``)).toEqual(["img.example"]); + }); + + it("does not take a local path, a comment or a namespace name for a host", () => { + expect(remoteHosts(``)).toEqual([]); + expect(remoteHosts(``)).toEqual([]); + expect(remoteHosts(`//el.style is set below`)).toEqual([]); + expect(remoteHosts(``)).toEqual([]); + expect(remoteHosts(`document.createElementNS("http://www.w3.org/2000/svg", "rect")`)).toEqual( + [], + ); + expect(remoteHosts(``)).toEqual(["www.w3.org"]); + expect( + remoteHosts(``), + ).toEqual([]); + }); + + it("load nothing from an outside host that is not already known", () => { + const files = starterFiles(examplesDir) + .map((path) => relative(examplesDir, path).split("\\").join("/")) + .filter( + (path) => scannedFile.test(path) && !vendoredDir.test(path) && !minifiedFile.test(path), + ); + expect(files.some((path) => path.endsWith(".html"))).toBe(true); + + const unexpected: string[] = []; + for (const path of files) { + for (const host of remoteHosts(readFileSync(join(examplesDir, path), "utf8"))) { + if (!knownHosts.has(host)) unexpected.push(`${path}: ${host}`); + } + } + + expect(unexpected).toEqual([]); + }); +}); diff --git a/registry/examples/warm-grain/index.html b/registry/examples/warm-grain/index.html index 6b7a2775b8..3f56d7e597 100644 --- a/registry/examples/warm-grain/index.html +++ b/registry/examples/warm-grain/index.html @@ -76,7 +76,7 @@ left: -50%; width: 200%; height: 200%; - background: url("https://www.transparenttextures.com/patterns/natural-paper.png"); + background: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='256' height='256'%3E%3Cfilter id='grain' x='0' y='0' width='100%25' height='100%25'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.65' numOctaves='3' stitchTiles='stitch'/%3E%3CfeColorMatrix values='0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 1 0 0 0 -0.37'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23grain)'/%3E%3C/svg%3E"); opacity: 0.15; animation: grain-noise 0.5s steps(1) infinite; }