diff --git a/packages/cli/src/server/studioServer.renderBrowser.test.ts b/packages/cli/src/server/studioServer.renderBrowser.test.ts index 73f58ba00b..6ca37a10c4 100644 --- a/packages/cli/src/server/studioServer.renderBrowser.test.ts +++ b/packages/cli/src/server/studioServer.renderBrowser.test.ts @@ -3,6 +3,7 @@ import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { createStudioServer, type StudioServer } from "./studioServer.js"; +import { terminalText } from "../utils/terminalText.js"; const mocks = vi.hoisted(() => ({ resolveRenderBrowser: vi.fn(), @@ -43,10 +44,14 @@ afterEach(() => { for (const dir of dirs.splice(0)) rmSync(dir, { recursive: true, force: true }); }); -function startStudioRender(extra: Record = {}, receivedFiles?: string[]) { +function startStudioRender( + extra: Record = {}, + receivedFiles?: string[], + entry = `
`, +) { const dir = mkdtempSync(join(tmpdir(), "hf-studio-render-browser-")); dirs.push(dir); - writeFileSync(join(dir, "index.html"), `
`); + writeFileSync(join(dir, "index.html"), entry); writeFileSync(join(dir, "scene.html"), "
"); writeFileSync(join(dir, "other.html"), "
"); if (receivedFiles) { @@ -186,4 +191,28 @@ describe("Studio render of a received composition", () => { expect.objectContaining({ allowOutsideProjectAssets: allowed }), ); }); + + it("logs the references it leaves out without their control characters", async () => { + mocks.resolveRenderBrowser.mockResolvedValue({ + executablePath: "/opt/chrome", + source: "cache", + }); + const outside = mkdtempSync(join(tmpdir(), "hf-studio-render-outside-")); + dirs.push(outside); + writeFileSync(join(outside, "pic.png"), "png"); + const warn = vi.spyOn(console, "warn").mockImplementation(() => {}); + + startStudioRender({}, ["index.html"], ``); + await vi.waitFor(() => expect(mocks.createRenderJob).toHaveBeenCalledTimes(1), { + timeout: 10_000, + }); + + const lines = warn.mock.calls + .map(([line]) => String(line)) + .filter((l) => l.startsWith("[studio]")); + warn.mockRestore(); + expect(lines).toHaveLength(1); + expect(lines[0]).toContain("/pic.png?\\x1b[2K\\x1b[1A"); + expect(lines[0]).toBe(terminalText(lines[0]!)); + }); }); diff --git a/packages/cli/src/utils/leftOutReference.test.ts b/packages/cli/src/utils/leftOutReference.test.ts index b23aa34f45..6d8b11e266 100644 --- a/packages/cli/src/utils/leftOutReference.test.ts +++ b/packages/cli/src/utils/leftOutReference.test.ts @@ -4,6 +4,7 @@ import { dirname, join } from "node:path"; import { afterEach, describe, expect, it } from "vitest"; import { stagePublishFiles } from "./publishProject.js"; import type { LeftOutReference } from "./leftOutReference.js"; +import { terminalText } from "./terminalText.js"; const dirs: string[] = []; afterEach(() => dirs.splice(0).forEach((dir) => rmSync(dir, { recursive: true, force: true }))); @@ -88,3 +89,26 @@ describe("each file an upload leaves out is also reported for a program", () => ]); }); }); + +describe("a left-out warning", () => { + it("prints the reference without its control characters and reports it as written", () => { + const query = "?\u001b[2K\u001b[1A\u0007"; + const { root, heard } = leftOutOf("p", { + ".ssh/id_ed25519": "KEY", + "p/index.html": ``, + }); + const reference = `${root}/.ssh/id_ed25519${query}`; + expect(heard).toHaveLength(1); + expect(heard[0]?.warning).toContain(`/.ssh/id_ed25519?\\x1b[2K\\x1b[1A\\x07,`); + expect(heard[0]?.warning).toBe(terminalText(heard[0]?.warning ?? "")); + expect(JSON.stringify(heard[0]?.reference)).toBe( + JSON.stringify({ + file: "index.html", + reference, + path: "../.ssh/id_ed25519", + reason: "blocklisted", + levels_up: 1, + }), + ); + }); +}); diff --git a/packages/cli/src/utils/leftOutReference.ts b/packages/cli/src/utils/leftOutReference.ts index b17b81e7de..17ca76708f 100644 --- a/packages/cli/src/utils/leftOutReference.ts +++ b/packages/cli/src/utils/leftOutReference.ts @@ -1,8 +1,15 @@ import { isAbsolute, relative } from "node:path"; -import { type LeftOutReason, leftOutWarning } from "@hyperframes/studio-server/outside-file"; +import { + type LeftOutReason, + leftOutWarning as leftOutText, +} from "@hyperframes/studio-server/outside-file"; import { realFilePath } from "@hyperframes/studio-server/safe-path"; +import { terminalText } from "./terminalText.js"; -export { type LeftOutReason, leftOutWarning }; +export type { LeftOutReason }; + +export const leftOutWarning = (file: string, reference: string, reason: LeftOutReason): string => + terminalText(leftOutText(file, reference, reason)); /** One file an upload left out. `path` is from the project folder; `levels_up` is null on another drive. */ export interface LeftOutReference { @@ -44,7 +51,9 @@ export function leftOutReference( export function outsideFilesNotice(references: OutsideFileReference[]): string { const count = references.length === 1 ? "1 file" : `${references.length} files`; const lines = references.map(({ file, reference, path }) => - file === reference ? ` ${path} (linked from ${file})` : ` ${path} (referenced by ${file})`, + terminalText( + file === reference ? ` ${path} (linked from ${file})` : ` ${path} (referenced by ${file})`, + ), ); return [`Uploading ${count} from outside the project folder:`, ...lines].join("\n"); } diff --git a/packages/cli/src/utils/publishProject.outsideList.test.ts b/packages/cli/src/utils/publishProject.outsideList.test.ts index 004ae01c35..22f8d94565 100644 --- a/packages/cli/src/utils/publishProject.outsideList.test.ts +++ b/packages/cli/src/utils/publishProject.outsideList.test.ts @@ -113,4 +113,21 @@ describe("the notice that names them", () => { outsideFilesNotice([{ file: "a.html", reference: "/x.png", path: "../x.png", levels_up: 1 }]), ).toMatch(/^Uploading 1 file from outside/); }); + + it("prints names and paths from the project without their control characters", () => { + const notice = outsideFilesNotice([ + { + file: "scène\u001b[2K.html", + reference: "../a.png", + path: "../a\u009b31m\u007f.png", + levels_up: 1, + }, + ]); + expect(notice).toBe( + [ + "Uploading 1 file from outside the project folder:", + " ../a\\x9b31m\\x7f.png (referenced by scène\\x1b[2K.html)", + ].join("\n"), + ); + }); }); diff --git a/packages/cli/src/utils/receivedRender.test.ts b/packages/cli/src/utils/receivedRender.test.ts index dd07c4b868..8bac58c0e2 100644 --- a/packages/cli/src/utils/receivedRender.test.ts +++ b/packages/cli/src/utils/receivedRender.test.ts @@ -13,6 +13,7 @@ import { tmpdir } from "node:os"; import { join } from "node:path"; import { compileForRender } from "../../../producer/src/services/htmlCompiler.js"; import { outsideAssetsAllowed } from "./receivedRender.js"; +import { terminalText } from "./terminalText.js"; const SIGNATURES = { png: Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0, 0, 0, 0x0d]), @@ -253,6 +254,24 @@ describe("outsideAssetsAllowed", () => { expect(line).toContain("stops on the compositions ../outside/shared.html"); }); + it("prints references and file names from the project without their control characters", () => { + const dir = project(received("notes\u001b[31m\u009b2J.txt")); + write(join(dir, "..", "outside", "café.png"), SIGNATURES.png); + write( + join(dir, "index.html"), + ` + +
`, + ); + const { allowed, line } = decide(dir); + expect(allowed).toBe(false); + expect(line).toBe(terminalText(line)); + expect(line).toContain("notes\\x1b[31m\\x9b2J.txt"); + expect(line).toContain("../outside/pic.png?\\x1b[2K\\x1b[1Aspoof\\x07"); + expect(line).toContain("../outside/bg.png#\\x9b31m\\x7f"); + expect(line).toContain("../outside/café.png"); + }); + it.runIf(process.platform !== "win32")( "skips a stylesheet it cannot read instead of failing", () => { diff --git a/packages/cli/src/utils/receivedRender.ts b/packages/cli/src/utils/receivedRender.ts index 58fe07824f..47a03df131 100644 --- a/packages/cli/src/utils/receivedRender.ts +++ b/packages/cli/src/utils/receivedRender.ts @@ -11,8 +11,12 @@ import { dirname, extname, isAbsolute, join, relative, resolve } from "node:path import { fileURLToPath } from "node:url"; import { parseHTML } from "linkedom"; import { isNonRelativeUrl, isPathInside } from "@hyperframes/core"; -import { resolveCompositionSrc } from "@hyperframes/parsers/asset-resolution"; +import { + namesAnotherNetworkRoot, + resolveCompositionSrc, +} from "@hyperframes/parsers/asset-resolution"; import { linksOutside, receivedRecordStatus } from "./privateProjectFile.js"; +import { terminalText } from "./terminalText.js"; const REFERENCE_ATTRS = ["src", "href", "poster"]; const MAX_SCANNED_BYTES = 2 * 1024 * 1024; @@ -70,6 +74,13 @@ function htmlReferences(html: string): Reference[] { return refs; } +const WINDOWS_DEVICE_PREFIX = /^[\\/]{2}[?.][\\/]/; + +function withoutQuery(raw: string): string { + const device = WINDOWS_DEVICE_PREFIX.exec(raw)?.[0] ?? ""; + return device + raw.slice(device.length).replace(/[?#].*$/, ""); +} + function localPath(raw: string, fromDir: string): string | null { if (raw.startsWith("file:")) { try { @@ -78,7 +89,7 @@ function localPath(raw: string, fromDir: string): string | null { return null; } } - const path = raw.replace(/[?#].*$/, ""); + const path = withoutQuery(raw); if (isAbsolute(path) && !path.startsWith("//")) return path; if (path === "" || isNonRelativeUrl(path)) return null; try { @@ -147,6 +158,9 @@ function referencesIn(found: OutsideReferences, file: string): Reference[] { return /\.css$/i.test(file) ? cssReferences(read.text) : htmlReferences(read.text); } +const noteOutside = (found: OutsideReferences, ref: Reference) => + (ref.composition ? found.compositions : found.assets).add(ref.raw); + function noteReference( found: OutsideReferences, ref: Reference, @@ -155,7 +169,7 @@ function noteReference( ): string | null { try { if (linksOutside(projectDir, path)) { - (ref.composition ? found.compositions : found.assets).add(ref.raw); + noteOutside(found, ref); return null; } } catch { @@ -169,7 +183,12 @@ function scanFile(found: OutsideReferences, file: string, projectDir: string): s const next: string[] = []; for (const ref of referencesIn(found, file)) { const path = referencePath(ref, file, projectDir); - if (path === null || !isFile(path)) continue; + if (path === null) continue; + if (namesAnotherNetworkRoot(projectDir, path)) { + noteOutside(found, ref); + continue; + } + if (!isFile(path)) continue; const follow = noteReference(found, ref, path, projectDir); if (follow !== null) next.push(follow); } @@ -186,8 +205,9 @@ function outsideReferences(projectDir: string, entryFile: string): OutsideRefere const visited = new Set(); const pending = [resolve(projectDir, entryFile)]; for (let file = pending.pop(); file !== undefined; file = pending.pop()) { + if (!isPathInside(file, projectDir)) continue; const real = realOrNull(file); - if (real === null || visited.has(real) || !isPathInside(file, projectDir)) continue; + if (real === null || visited.has(real)) continue; if (visited.size >= MAX_SCANNED_FILES) { found.complete = false; break; @@ -421,6 +441,6 @@ export function outsideAssetsAllowed( const refusal = record === null ? null : receivedRefusal(absProjectDir, record); if (refusal === null) return true; const refused = refusedReferences(outsideReferences(absProjectDir, entryFile ?? "index.html")); - if (refusal.alwaysWarn || refused !== "") warn(`${refusal.reason}${refused}`); + if (refusal.alwaysWarn || refused !== "") warn(terminalText(`${refusal.reason}${refused}`)); return false; } diff --git a/packages/cli/src/utils/receivedRender.win32.test.ts b/packages/cli/src/utils/receivedRender.win32.test.ts new file mode 100644 index 0000000000..66a222b373 --- /dev/null +++ b/packages/cli/src/utils/receivedRender.win32.test.ts @@ -0,0 +1,138 @@ +import { describe, expect, it, vi } from "vitest"; + +// Windows path rules on any host, and a disk that records every path it is asked about. +const disk = vi.hoisted(() => ({ + files: new Map(), + touched: [] as string[], + open: new Map(), +})); +vi.mock("node:path", async () => { + const path = await vi.importActual("node:path"); + return { ...path.win32, default: path.win32 }; +}); +// file: URLs read as Windows reads them: file://host/share/x is \\host\share\x. +vi.mock("node:url", async () => { + const url = await vi.importActual("node:url"); + const fileURLToPath = (href: string | URL) => url.fileURLToPath(href, { windows: true }); + return { ...url, fileURLToPath, default: { ...url, fileURLToPath } }; +}); +vi.mock("node:fs", async () => { + const fs = await vi.importActual("node:fs"); + const missing = (path: string) => Object.assign(new Error(`ENOENT: ${path}`), { code: "ENOENT" }); + const look = (path: string) => { + disk.touched.push(path); + return disk.files.get(path.toLowerCase()); + }; + const statSync = (path: string, options?: { throwIfNoEntry?: boolean }) => { + const text = look(path); + if (text === undefined && options?.throwIfNoEntry === false) return undefined; + if (text === undefined) throw missing(path); + return { + isFile: () => true, + isDirectory: () => false, + size: text.length, + dev: 1n, + ino: 1n, + }; + }; + const existsSync = (path: string) => look(path) !== undefined; + const realpathSync = Object.assign((path: string) => (disk.touched.push(path), path), { + native: (path: string) => (disk.touched.push(path), path), + }); + const openSync = (path: string) => { + if (look(path) === undefined) throw missing(path); + const fd = disk.open.size + 100; + disk.open.set(fd, path); + return fd; + }; + const readSync = (fd: number, buffer: Buffer, offset: number, length: number) => + Buffer.from(disk.files.get(disk.open.get(fd)!.toLowerCase())!).copy(buffer, offset, 0, length); + const closeSync = (fd: number) => disk.open.delete(fd); + const overrides = { + statSync, + existsSync, + realpathSync, + openSync, + readSync, + closeSync, + }; + return { ...fs, ...overrides, default: { ...fs, ...overrides } }; +}); +// The record says index.html came from someone else; deciding where a file leads would realpath it. +vi.mock("./privateProjectFile.js", async () => { + const { win32 } = await vi.importActual("node:path"); + return { + receivedRecordStatus: (projectDir: string) => ({ + path: win32.join(projectDir, ".hyperframes", "received-files.json"), + listed: [win32.join(projectDir, "index.html")], + }), + linksOutside: (projectDir: string, path: string) => { + disk.touched.push(path); + const rel = win32.relative(projectDir, path); + return rel.startsWith("..") || win32.isAbsolute(rel); + }, + }; +}); + +const { outsideAssetsAllowed } = await import("./receivedRender.js"); + +const SHARES = [ + "\\\\host\\share\\a.png", + "/\\host\\share\\b.png", + "\\/host/share/c.png", + "\\\\?\\UNC\\host\\share\\d.png", + "\\\\.\\pipe\\e", + "\\\\.\\UNC\\host\\share\\j.png", + "\\\\?\\D:\\media\\f.png", + "file://host/share/g.png", + "%5C%5Chost%5Cshare%5Ch.png", + "\\\\host\\share\\i.png?v=2", +]; +const onAShare = (path: string) => /^[\\/]{2}/.test(path); + +function decide(project: string, html: string, css = "") { + disk.files.clear(); + disk.touched.length = 0; + disk.files.set(`${project}\\index.html`.toLowerCase(), html); + disk.files.set(`${project}\\style.css`.toLowerCase(), css); + disk.files.set("d:\\media\\pic.png", "png"); + for (const share of ["\\\\host\\share\\a.png", "\\\\host\\share\\x.css"]) { + disk.files.set(share.toLowerCase(), "x"); + } + const warn = vi.fn(); + const allowed = outsideAssetsAllowed(project, undefined, warn); + return { allowed, line: String(warn.mock.calls[0]?.[0] ?? "") }; +} + +describe("outsideAssetsAllowed under Windows rules", () => { + const PROJECT = "C:\\project"; + const html = + `` + + SHARES.map((src) => ``).join("") + + `
` + + `
` + + ``; + const css = `body { background: url(\\\\host\\share\\bg.png) }`; + + it("never looks at another computer's share or a device path, however it is written", () => { + const { allowed } = decide(PROJECT, html, css); + expect(allowed).toBe(false); + expect(disk.touched.filter(onAShare)).toEqual([]); + }); + + it("still names those references and a local file outside the project in the notice", () => { + const { line } = decide(PROJECT, html, css); + for (const ref of [...SHARES, "\\\\host\\share\\x.css", "\\\\host\\share\\bg.png"]) { + expect(line).toContain(ref); + } + expect(line).toContain("D:\\media\\pic.png"); + expect(disk.touched).toContain("D:\\media\\pic.png"); + }); + + it("still checks a file on the share the project itself lives on", () => { + const project = "\\\\nas\\films\\project"; + const { line } = decide(project, ``); + expect(line).toBe(""); + expect(disk.touched).toContain("\\\\NAS\\films\\shared\\pic.png"); + }); +});