diff --git a/packages/lint/src/project.networkRoot.test.ts b/packages/lint/src/project.networkRoot.test.ts new file mode 100644 index 0000000000..10a7bb6acb --- /dev/null +++ b/packages/lint/src/project.networkRoot.test.ts @@ -0,0 +1,62 @@ +import { afterEach, describe, expect, it, vi } from "vitest"; +import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; +import { join } from "node:path"; +import { tmpdir } from "node:os"; + +vi.mock("node:child_process", () => { + const mocked = { ChildProcess: class {}, execFile: vi.fn(), execSync: vi.fn() }; + return { ...mocked, default: mocked }; +}); + +// Which srcs name another computer's share is Windows path logic, tested in parsers; here any src on +// `attacker.example` does, and every src lint resolves on disk is recorded. +const resolved = vi.hoisted(() => [] as string[]); +vi.mock("@hyperframes/parsers/asset-resolution", async (importOriginal) => { + const actual = await importOriginal(); + return { + ...actual, + srcOnAnotherNetworkRoot: (_projectDir: string, src: string) => src.includes("attacker.example"), + resolveProjectRelativeSrc: (src: string, ...rest: [string, string?]) => { + resolved.push(src); + return actual.resolveProjectRelativeSrc(src, ...rest); + }, + }; +}); + +const { lintProject } = await import("./project.js"); + +let project: string | undefined; +afterEach(() => { + if (project) rmSync(project, { recursive: true, force: true }); + project = undefined; + resolved.length = 0; +}); + +async function lintAudio(src: string): Promise { + project = mkdtempSync(join(tmpdir(), "hf-lint-network-root-")); + writeFileSync( + join(project, "index.html"), + ` +
+ +
+ +`, + ); + const { results } = await lintProject(project); + return results.flatMap((result) => result.result.findings.map((finding) => finding.code)); +} + +describe("audio on another computer's share", () => { + it("is not looked for or reported", async () => { + const codes = await lintAudio("\\\\attacker.example\\share\\voice.wav"); + expect(resolved.filter((src) => src.includes("attacker.example"))).toEqual([]); + expect(codes).not.toContain("audio_src_not_found"); + }); + + it("leaves a missing project audio file reported", async () => { + const codes = await lintAudio("assets/gone.wav"); + expect(resolved).toContain("assets/gone.wav"); + expect(codes).toContain("audio_src_not_found"); + }); +}); diff --git a/packages/lint/src/project.ts b/packages/lint/src/project.ts index 71e386c43e..1d53f08578 100644 --- a/packages/lint/src/project.ts +++ b/packages/lint/src/project.ts @@ -35,6 +35,7 @@ import { resolveExistingLocalAsset, resolveLocalAssetCandidates, resolveProjectRelativeSrc, + srcOnAnotherNetworkRoot, readTextFile, } from "@hyperframes/parsers/asset-resolution"; import { encodeUrlPath } from "@hyperframes/parsers"; @@ -555,6 +556,7 @@ function lintAudioSrcNotFound( const rootRelative = compSrcPath ? rewriteAssetPath(compSrcPath, src, (path) => existsSync(join(projectDir, path))) : src; + if (srcOnAnotherNetworkRoot(projectDir, rootRelative)) continue; if (!existsSync(resolveProjectRelativeSrc(rootRelative, projectDir))) { const safe = urlSafeSrcOf(projectDir, rootRelative); if (safe) urlSafe.set(src, safe); diff --git a/packages/parsers/src/assetResolution.ts b/packages/parsers/src/assetResolution.ts index 5b1ce169d0..cc51c65a03 100644 --- a/packages/parsers/src/assetResolution.ts +++ b/packages/parsers/src/assetResolution.ts @@ -138,6 +138,11 @@ export function namesAnotherNetworkRoot(base: string, target: string): boolean { return /^[\\/]{2}/.test(root) && root.toLowerCase() !== parse(resolve(base)).root.toLowerCase(); } +export function srcOnAnotherNetworkRoot(projectDir: string, src: string): boolean { + const path = fileUrlAsPath(cleanAssetUrl(src)); + return isAbsolute(path) && namesAnotherNetworkRoot(projectDir, path); +} + const LEADING_PARENT_SEGMENT = /^\.\.(?:[\\/]|$)/; function staysInsideProject(projectDir: string, path: string): boolean { diff --git a/packages/parsers/src/assetResolution.win32.test.ts b/packages/parsers/src/assetResolution.win32.test.ts index 80c34f8a08..a00fd7dd7b 100644 --- a/packages/parsers/src/assetResolution.win32.test.ts +++ b/packages/parsers/src/assetResolution.win32.test.ts @@ -22,6 +22,7 @@ const { resolveAuthoredAssetFile, resolveCompositionSrc, resolveLocalAssetCandidates, + srcOnAnotherNetworkRoot, } = await import("./assetResolution.js"); const { resolve } = await import("node:path"); @@ -142,3 +143,27 @@ describe("localAbsoluteAssetFile under Windows rules", () => { expect(localAbsoluteAssetFile(PROJECT, "C:\\media\\gone.mp4")).toBeNull(); }); }); + +describe("srcOnAnotherNetworkRoot under Windows rules", () => { + it.each([ + "\\\\attacker.example\\share\\x.wav", + "//attacker.example/share/x.wav", + "/\\attacker.example\\share\\x.wav", + "\\\\attacker.example\\share\\x.wav?v=2", + ])("is true for %s", (src) => { + touched.length = 0; + expect(srcOnAnotherNetworkRoot(PROJECT, src)).toBe(true); + expect(touched).toEqual([]); + }); + + it.each(["C:\\media\\voice.wav", "assets\\voice.wav", "/assets/voice.wav", "../voice.wav"])( + "is false for %s", + (src) => { + expect(srcOnAnotherNetworkRoot(PROJECT, src)).toBe(false); + }, + ); + + it("is false for a file on the share the project itself lives on", () => { + expect(srcOnAnotherNetworkRoot("\\\\nas\\films\\project", "//NAS/films/voice.wav")).toBe(false); + }); +});