diff --git a/packages/cli/src/utils/checkAudio.ts b/packages/cli/src/utils/checkAudio.ts index 7007e1fba8..5d8605785d 100644 --- a/packages/cli/src/utils/checkAudio.ts +++ b/packages/cli/src/utils/checkAudio.ts @@ -139,9 +139,8 @@ async function videoSoundsAutoDetected( dependencies: AudioCheckDependencies, ): Promise { for (const video of media.videos) { - if (!video.hasAudio) continue; - if (await dependencies.videoHasAudio(mediaPath(video.src, projectDir)).catch(() => false)) - return true; + const path = video.hasAudio ? mediaPath(video.src, projectDir) : null; + if (path && (await dependencies.videoHasAudio(path).catch(() => false))) return true; } return false; } @@ -156,7 +155,7 @@ async function inspectClip( ): Promise { const path = mediaPath(clip.src, projectDir); // ponytail: remote sources are not fetched; missing local files are lint's audio_src_not_found. - if (path === clip.src || !existsSync(path)) return []; + if (path === null || path === clip.src || !existsSync(path)) return []; const problem = await clipProblem(path, clip, dependencies); if (!problem) return []; const id = clip.type === "video" ? clip.id.replace(/-audio$/, "") : clip.id; diff --git a/packages/cli/src/utils/hdrPromotion.ts b/packages/cli/src/utils/hdrPromotion.ts index 567cfa15b2..049c6b2de5 100644 --- a/packages/cli/src/utils/hdrPromotion.ts +++ b/packages/cli/src/utils/hdrPromotion.ts @@ -20,11 +20,10 @@ export async function inspectHdrAutoPromotion( colorSpace: Awaited>["colorSpace"]; }> = []; for (const { src } of [...media.videos, ...media.images]) { + const path = mediaPath(src, project.dir); + if (path === null) continue; try { - assets.push({ - asset: src, - colorSpace: (await extractMetadata(mediaPath(src, project.dir))).colorSpace, - }); + assets.push({ asset: src, colorSpace: (await extractMetadata(path)).colorSpace }); } catch { continue; } diff --git a/packages/cli/src/utils/projectMedia.test.ts b/packages/cli/src/utils/projectMedia.test.ts index 54c10ebc8c..f50fa6c5b7 100644 --- a/packages/cli/src/utils/projectMedia.test.ts +++ b/packages/cli/src/utils/projectMedia.test.ts @@ -2,7 +2,7 @@ import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { afterEach, describe, expect, it } from "vitest"; -import { heardClips } from "./projectMedia.js"; +import { heardClips, mediaPath } from "./projectMedia.js"; let dir = ""; afterEach(() => rmSync(dir, { recursive: true, force: true })); @@ -29,6 +29,36 @@ ${scene} const probe = { seconds: async () => 6 }; +describe("mediaPath", () => { + it("never hands check a local file outside the project or private in it", () => { + dir = mkdtempSync(join(tmpdir(), "hf-media-path-")); + for (const rel of [ + ".git/clip.mp4", + ".hyperframes/clip.mp4", + "media/id_rsa", + "media/clip.mp4", + ]) { + mkdirSync(join(dir, rel, ".."), { recursive: true }); + writeFileSync(join(dir, rel), "x"); + } + expect(mediaPath(".git/clip.mp4", dir)).toBeNull(); + expect(mediaPath(".hyperframes/clip.mp4", dir)).toBeNull(); + expect(mediaPath("media/id_rsa", dir)).toBeNull(); + expect(mediaPath("../../outside.mp4", dir)).toBeNull(); + expect(mediaPath("media/clip.mp4", dir)).toBe(join(dir, "media", "clip.mp4")); + expect(mediaPath("https://cdn.example.com/a.mp4", dir)).toBe("https://cdn.example.com/a.mp4"); + }); + + it("does not probe a private file a scene's audio names", async () => { + const seen: string[] = []; + const root = project(''); + mkdirSync(join(root, ".git")); + writeFileSync(join(root, ".git", "voice.wav"), "x"); + await heardClips(root, { seconds: async (path) => (seen.push(path), 6) }); + expect(seen).toEqual([]); + }); +}); + describe("heardClips", () => { it("hears a speaking video inside a timed wrapper in a scene, where the render plays it", async () => { const clips = await heardClips( diff --git a/packages/cli/src/utils/projectMedia.ts b/packages/cli/src/utils/projectMedia.ts index 73bdf56689..6d85c14120 100644 --- a/packages/cli/src/utils/projectMedia.ts +++ b/packages/cli/src/utils/projectMedia.ts @@ -1,4 +1,5 @@ import { + assetRootsRefusal, extractAudioMetadata, resolveProjectRelativeSrc, type AudioElement, @@ -26,9 +27,11 @@ export async function collectProjectMedia(projectDir: string): Promise>(); const length = (src: string): Promise => { const path = mediaPath(src, projectDir); + if (path === null) return Promise.resolve(null); const known = lengths.get(path) ?? probe.seconds(path).catch(() => null); lengths.set(path, known); return known; diff --git a/packages/engine/src/index.ts b/packages/engine/src/index.ts index 27c7e8f917..a884f95af1 100644 --- a/packages/engine/src/index.ts +++ b/packages/engine/src/index.ts @@ -441,7 +441,11 @@ export { type PublicHttpsTextOptions, } from "./utils/urlDownloader.js"; export { isBlockedNetworkHost } from "./utils/networkPolicy.js"; -export { isInsideAssetRoots } from "./utils/assetRoots.js"; +export { + assetRootsRefusal, + isInsideAssetRoots, + isPrivateUnderAssetRoots, +} from "./utils/assetRoots.js"; export { onRenderRefusal } from "./services/publicNetwork/captureProxy.js"; export type { RefusedHostClass } from "./services/publicNetwork/publicNetwork.js"; export { diff --git a/packages/engine/src/services/audioMixer.test.ts b/packages/engine/src/services/audioMixer.test.ts index 8cb1902ef2..802909e7e1 100644 --- a/packages/engine/src/services/audioMixer.test.ts +++ b/packages/engine/src/services/audioMixer.test.ts @@ -309,6 +309,37 @@ describe("processCompositionAudio", () => { ]); }); + it.each([".git/music.mp3", ".hyperframes/music.mp3", ".claude/music.mp3", "keys/id_rsa"])( + "never opens the private project file %s as an audio source", + async (src) => { + const baseDir = mkdtempSync(join(tmpdir(), "hf-audio-private-")); + const workDir = mkdtempSync(join(tmpdir(), "hf-audio-work-")); + tempDirs.push(baseDir, workDir); + mkdirSync(join(baseDir, src, ".."), { recursive: true }); + writeFileSync(join(baseDir, src), "not for the render"); + + const result = await processCompositionAudio( + [{ id: "bed", src, start: 0, end: 2, mediaStart: 0, layer: 0, volume: 1, type: "audio" }], + baseDir, + workDir, + join(workDir, "out.m4a"), + 2, + undefined, + { assetRoots: [baseDir] }, + ); + + expect(result.failures).toEqual([ + expect.objectContaining({ + reason: "source_not_found", + elementId: "bed", + detail: "Audio element bed source is a private project file", + }), + ]); + expect(extractAudioMetadataMock).not.toHaveBeenCalled(); + expect(runFfmpegMock.mock.calls.flat(2)).not.toContain(join(baseDir, src)); + }, + ); + it.each([ { message: "AbortError: ffprobe operation aborted", diff --git a/packages/engine/src/services/audioMixer.ts b/packages/engine/src/services/audioMixer.ts index 58cd3ff1bc..a363b82a8f 100644 --- a/packages/engine/src/services/audioMixer.ts +++ b/packages/engine/src/services/audioMixer.ts @@ -13,7 +13,7 @@ import { type AudioMetadata, } from "../utils/ffprobe.js"; import { isNotMediaPayload } from "../utils/notMediaPayload.js"; -import { isInsideAssetRoots } from "../utils/assetRoots.js"; +import { assetRootsRefusal } from "../utils/assetRoots.js"; import { urlInputArgs } from "../utils/urlInput.js"; import { clampAudioGain, clipGain } from "@hyperframes/core/audio-gain"; import { clampFadesToDuration, readElementFades } from "@hyperframes/core/audio-fade"; @@ -1434,7 +1434,7 @@ export async function processCompositionAudio( urlByAbsentLocalPath?: ReadonlyMap; audioSidecarUrlByLocalPath?: ReadonlyMap; keepEncoderInputAt?: string; - /** Folders a local source must be in; one outside them is never opened. Unset allows any folder. */ + /** Folders a local source must be in; one outside them, or private there, is never opened. Unset allows any folder. */ assetRoots?: readonly string[]; }, compiledDir?: string, @@ -1500,18 +1500,21 @@ export async function processCompositionAudio( audioSidecarUrl = config?.audioSidecarUrlByLocalPath?.get(srcPath); srcPath = remoteUrl; readInPlace = true; - } else if (config?.assetRoots && !isInsideAssetRoots(srcPath, config.assetRoots)) { - failures.push({ - stage: "source", - reason: "source_not_found", - owner: "user", - retryable: false, - elementId: element.id, - detail: boundedDetail( - `Audio element ${element.id} source is outside the project folder`, - ), - }); - return; + } else { + const refusal = config?.assetRoots + ? assetRootsRefusal(srcPath, config.assetRoots) + : null; + if (refusal) { + failures.push({ + stage: "source", + reason: "source_not_found", + owner: "user", + retryable: false, + elementId: element.id, + detail: boundedDetail(`Audio element ${element.id} source is ${refusal}`), + }); + return; + } } } diff --git a/packages/engine/src/services/videoFrameExtractor.test.ts b/packages/engine/src/services/videoFrameExtractor.test.ts index 39e8c390c1..f821ee8b96 100644 --- a/packages/engine/src/services/videoFrameExtractor.test.ts +++ b/packages/engine/src/services/videoFrameExtractor.test.ts @@ -691,6 +691,49 @@ describe("video extraction failure taxonomy and bounded retry", () => { rmSync(dir, { recursive: true, force: true }); } }); + + it.skipIf(!HAS_FFMPEG)("refuses a text file that ffmpeg would draw as video", async () => { + const dir = mkdtempSync(join(tmpdir(), "hf-extract-text-art-")); + try { + writeFileSync(join(dir, "notes.nfo"), "API_TOKEN=not-for-the-frame\nhello\n"); + const video = { id: "v1", src: "notes.nfo", start: 0, end: 1, mediaStart: 0, loop: false }; + const extraction = extractAllVideoFrames([{ ...video, hasAudio: false }], dir, { + fps: 30, + outputDir: join(dir, "out"), + assetRoots: [dir], + }); + await expect(extraction).rejects.toThrow(/text file, not a media container/); + } finally { + rmSync(dir, { recursive: true, force: true }); + } + }); + + it.each([".git/clip.mp4", ".hyperframes/clip.mp4", ".ssh/clip.mp4"])( + "treats the private project file %s as a missing source", + async (src) => { + const dir = mkdtempSync(join(tmpdir(), "hf-extract-private-")); + try { + mkdirSync(join(dir, src, ".."), { recursive: true }); + writeFileSync(join(dir, src), "not for the render"); + const video = { id: "v1", src, start: 0, end: 1, mediaStart: 0, loop: false }; + const result = await extractAllVideoFrames([{ ...video, hasAudio: false }], dir, { + fps: 30, + outputDir: join(dir, "out"), + assetRoots: [dir], + }); + expect(result.extracted).toEqual([]); + expect(result.errors).toEqual([ + expect.objectContaining({ + videoId: "v1", + kind: "source_missing", + error: `Video source is a private project file: ${src}`, + }), + ]); + } finally { + rmSync(dir, { recursive: true, force: true }); + } + }, + ); }); // Codec-based alpha defaulting replaces tag-based detection (the diff --git a/packages/engine/src/services/videoFrameExtractor.ts b/packages/engine/src/services/videoFrameExtractor.ts index 3bbc1b7414..e65e91eca9 100644 --- a/packages/engine/src/services/videoFrameExtractor.ts +++ b/packages/engine/src/services/videoFrameExtractor.ts @@ -15,7 +15,7 @@ import { parseHTML } from "linkedom"; import { HF_COLOR_GRADING_ATTR } from "@hyperframes/core"; import { isPathInside } from "@hyperframes/parsers/asset-paths"; import { resolveProjectRelativeSrc } from "@hyperframes/parsers/asset-resolution"; -import { isInsideAssetRoots } from "../utils/assetRoots.js"; +import { assetRootsRefusal } from "../utils/assetRoots.js"; import { alphaDecoderFor } from "@hyperframes/parsers/media-alpha"; import { MEDIA_RENDER_ID_ATTR, @@ -319,7 +319,7 @@ export interface ExtractionOptions { /** Directories the caller rewrites on every render (downloaded media); their sources are cached by content. */ contentKeyedDirs?: readonly string[]; urlByAbsentLocalPath?: ReadonlyMap; - /** Folders a local source must be in; one outside them is never opened. Unset allows any folder. */ + /** Folders a local source must be in; one outside them, or private there, is never opened. Unset allows any folder. */ assetRoots?: readonly string[]; shownBox?: { width: number; height: number }; shownBoxes?: ReadonlyMap; @@ -2275,12 +2275,13 @@ export async function extractAllVideoFrames( continue; } const roots = options.assetRoots; - if (!isHttpUrl(videoPath) && roots && !isInsideAssetRoots(videoPath, roots)) { + const refusal = !isHttpUrl(videoPath) && roots ? assetRootsRefusal(videoPath, roots) : null; + if (refusal) { errors.push({ videoId: video.id, kind: "source_missing", retryable: false, - error: `Video source is outside the project folder: ${video.src}`, + error: `Video source is ${refusal}: ${video.src}`, }); continue; } diff --git a/packages/engine/src/utils/assetRoots.test.ts b/packages/engine/src/utils/assetRoots.test.ts new file mode 100644 index 0000000000..9c6ed0b578 --- /dev/null +++ b/packages/engine/src/utils/assetRoots.test.ts @@ -0,0 +1,53 @@ +import { mkdirSync, mkdtempSync, rmSync, symlinkSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { dirname, join } from "node:path"; +import { afterAll, describe, expect, it } from "vitest"; +import { assetRootsRefusal, isPrivateUnderAssetRoots } from "./assetRoots.js"; + +describe("isPrivateUnderAssetRoots", () => { + const root = mkdtempSync(join(tmpdir(), "hf-asset-roots-")); + const projectDir = join(root, "project"); + const workDir = join(projectDir, ".hyperframes", "share-renders", "work"); + const roots = [projectDir, join(workDir, "compiled"), join(workDir, "downloads")]; + const file = (rel: string) => { + const path = join(projectDir, rel); + mkdirSync(dirname(path), { recursive: true }); + writeFileSync(path, "x"); + return path; + }; + afterAll(() => rmSync(root, { recursive: true, force: true })); + + it.each([ + ".git/objects/clip.mp4", + ".hyperframes/agent-handoff.mp4", + ".claude/voice.wav", + ".ssh/frames.gif", + "keys/id_ed25519", + "certs/server.pem", + ".env", + ])("refuses the project file %s", (rel) => { + expect(isPrivateUnderAssetRoots(file(rel), roots)).toBe(true); + expect(assetRootsRefusal(file(rel), roots)).toBe("a private project file"); + }); + + it("refuses a link whose target is a private project file", () => { + const link = join(projectDir, "clip.mp4"); + symlinkSync(file(".git/config"), link); + expect(isPrivateUnderAssetRoots(link, roots)).toBe(true); + }); + + it("keeps project media and the render's own files inside .hyperframes", () => { + expect(isPrivateUnderAssetRoots(file("media/clip.mp4"), roots)).toBe(false); + expect(assetRootsRefusal(file("media/clip.mp4"), roots)).toBeNull(); + const downloaded = file(".hyperframes/share-renders/work/downloads/remote.mp4"); + expect(isPrivateUnderAssetRoots(downloaded, roots)).toBe(false); + }); + + it("leaves a file outside every root to the containment check", () => { + const outside = join(root, ".git", "config"); + mkdirSync(dirname(outside), { recursive: true }); + writeFileSync(outside, "x"); + expect(isPrivateUnderAssetRoots(outside, roots)).toBe(false); + expect(assetRootsRefusal(outside, roots)).toBe("outside the project folder"); + }); +}); diff --git a/packages/engine/src/utils/assetRoots.ts b/packages/engine/src/utils/assetRoots.ts index e4c1f4e5db..d9c30dfde3 100644 --- a/packages/engine/src/utils/assetRoots.ts +++ b/packages/engine/src/utils/assetRoots.ts @@ -1,5 +1,5 @@ import { existsSync } from "node:fs"; -import { isSafePath } from "@hyperframes/core/safe-path"; +import { isPrivateFileUnder, isSafePath, isStudioPrivatePath } from "@hyperframes/core/safe-path"; import { isWithinProjectRoot } from "@hyperframes/parsers/asset-resolution"; export function isInsideAssetRoots(path: string, roots: readonly string[]): boolean { @@ -7,3 +7,17 @@ export function isInsideAssetRoots(path: string, roots: readonly string[]): bool // A name the file system refuses, such as a long data: URL, opens nothing, so its spelling decides. return !existsSync(path) && roots.some((root) => isWithinProjectRoot(root, path)); } + +// Judged from every root holding it, since a render's own work folder may sit in the project's `.hyperframes/`. +export function isPrivateUnderAssetRoots(path: string, roots: readonly string[]): boolean { + const holders = roots.filter((root) => isSafePath(root, path)); + return ( + holders.length > 0 && + holders.every((root) => isPrivateFileUnder(root, path, isStudioPrivatePath)) + ); +} + +export function assetRootsRefusal(path: string, roots: readonly string[]): string | null { + if (!isInsideAssetRoots(path, roots)) return "outside the project folder"; + return isPrivateUnderAssetRoots(path, roots) ? "a private project file" : null; +} diff --git a/packages/engine/src/utils/ffprobe.test.ts b/packages/engine/src/utils/ffprobe.test.ts index 0de060fc99..8defc883c0 100644 --- a/packages/engine/src/utils/ffprobe.test.ts +++ b/packages/engine/src/utils/ffprobe.test.ts @@ -349,6 +349,56 @@ describe("probeMediaProfile", () => { vi.doUnmock("child_process"); }); + it.each([ + { format_name: "tty", codec_name: "ansi" }, + { format_name: "bin", codec_name: "bintext" }, + { format_name: "xbin", codec_name: "xbin" }, + { format_name: "idf", codec_name: "idf" }, + { format_name: "tty", codec_name: undefined }, + ])( + "refuses a text file ffmpeg reads as $format_name video", + async ({ format_name, codec_name }) => { + const outcome = { + kind: "exit" as const, + code: 0, + stdout: JSON.stringify({ + streams: [{ codec_type: "video", codec_name }], + format: { format_name }, + }), + }; + const { spawn } = createSpawnSpy([outcome, outcome]); + vi.resetModules(); + vi.doMock("child_process", () => ({ spawn })); + const { probeMediaProfile, extractMediaMetadata } = await import("./ffprobe.js"); + const { NotMediaPayloadError } = await import("./notMediaPayload.js"); + + await expect(probeMediaProfile("/tmp/notes-profile")).rejects.toBeInstanceOf( + NotMediaPayloadError, + ); + await expect(extractMediaMetadata("/tmp/notes-meta")).rejects.toThrow(/text file/); + }, + ); + + it.skipIf(spawnSync("ffprobe", ["-version"]).status !== 0)( + "refuses a real .asc or .nfo text file named as media", + async () => { + vi.resetModules(); + vi.doUnmock("child_process"); + const dir = mkdtempSync(resolve(tmpdir(), "hf-text-art-")); + const { probeMediaProfile, extractMediaMetadata } = await import("./ffprobe.js"); + try { + for (const name of ["notes.asc", "notes.nfo"]) { + const path = resolve(dir, name); + writeFileSync(path, "API_TOKEN=not-for-the-frame\nhello\n"); + await expect(probeMediaProfile(path)).rejects.toThrow(/text file/); + await expect(extractMediaMetadata(path)).rejects.toThrow(/text file/); + } + } finally { + rmSync(dir, { recursive: true, force: true }); + } + }, + ); + it("classifies still, moving, audio-only, and mixed streams from probe data", async () => { const { spawn } = createSpawnSpy([ { diff --git a/packages/engine/src/utils/ffprobe.ts b/packages/engine/src/utils/ffprobe.ts index 8b3d3c4065..a42f04c77b 100644 --- a/packages/engine/src/utils/ffprobe.ts +++ b/packages/engine/src/utils/ffprobe.ts @@ -12,6 +12,7 @@ import { videoStreamHasAlpha, } from "@hyperframes/parsers/media-alpha"; import { urlInputArgs } from "./urlInput.js"; +import { TextArtMediaError } from "./notMediaPayload.js"; import { decodableAudioStream, misstatesPcmByteRate } from "@hyperframes/parsers/media-streams"; import { runFfmpeg } from "./runFfmpeg.js"; import { FFPROBE_PATH_ENV, getFfprobeBinary } from "./ffmpegBinaries.js"; @@ -571,7 +572,9 @@ async function probeMediaOutput(filePath: string, signal?: AbortSignal): Promise } return cached.promise; } - const promise = readProbeJson(filePath, signal).then(parseProbeJson); + const promise = readProbeJson(filePath, signal).then((json) => + refuseTextArt(parseProbeJson(json)), + ); if (identity !== null) { cache.set(filePath, { identity, promise }); if (!signal && cache.size > MEDIA_PROBE_OUTPUT_CACHE_MAX_ENTRIES) { @@ -589,6 +592,18 @@ async function probeMediaOutput(filePath: string, signal?: AbortSignal): Promise class StructurallyIncompletePngError extends Error {} +const TEXT_ART_CODECS = new Set(["ansi", "bintext", "xbin", "idf"]); + +function refuseTextArt(output: FFProbeOutput): FFProbeOutput { + const formats = (output.format?.format_name ?? "").toLowerCase().split(","); + const drawsText = (output.streams ?? []).some( + (stream) => + stream.codec_type === "video" && TEXT_ART_CODECS.has(stream.codec_name?.toLowerCase() ?? ""), + ); + if (formats.includes("tty") || drawsText) throw new TextArtMediaError(); + return output; +} + async function readFileRange( filePath: string, start: number, diff --git a/packages/engine/src/utils/notMediaPayload.test.ts b/packages/engine/src/utils/notMediaPayload.test.ts index 83a584054a..66adabe092 100644 --- a/packages/engine/src/utils/notMediaPayload.test.ts +++ b/packages/engine/src/utils/notMediaPayload.test.ts @@ -271,6 +271,27 @@ describe("playlistLeavesItsFolder", () => { expect(await isNotMediaPayload(file)).toBe(true); }); + it.each([ + ["a git segment", hls("#EXTINF:1,", ".git/objects/seg.ts")], + ["an encoded app record", hls("#EXTINF:1,", "%2ehyperframes/agent-handoff.ts")], + ["a key file", hls('#EXT-X-KEY:METHOD=AES-128,URI="keys/id_rsa"', "#EXTINF:1,", "seg.ts")], + ["a credential folder in a map", hls('#EXT-X-MAP:URI=".ssh/init.mp4"', "#EXTINF:1,", "seg.ts")], + ["a concat entry in agent state", concat("'.claude/part.mp4'")], + ["an env file", concat(".env")], + ])("refuses %s that stays in its folder", async (_name, contents) => { + const file = writeFixture("clip.m3u8", contents); + expect(await playlistLeavesItsFolder(file)).toBe(true); + expect(await isNotMediaPayload(file)).toBe(true); + }); + + it("refuses a nested list naming a private file from its own folder", async () => { + const master = writeTree({ + "master.m3u8": "#EXTM3U\n#EXT-X-STREAM-INF:BANDWIDTH=1\nsub/inner.m3u8\n", + "sub/inner.m3u8": hls("#EXTINF:1,", ".git/seg.ts"), + }); + expect(await playlistLeavesItsFolder(master)).toBe(true); + }); + it("refuses a list whose nested list names a file outside the top folder", async () => { const master = writeTree({ "master.m3u8": "#EXTM3U\n#EXT-X-STREAM-INF:BANDWIDTH=1\nsub/inner.m3u8\n", diff --git a/packages/engine/src/utils/notMediaPayload.ts b/packages/engine/src/utils/notMediaPayload.ts index 906401a043..e3206f4ef9 100644 --- a/packages/engine/src/utils/notMediaPayload.ts +++ b/packages/engine/src/utils/notMediaPayload.ts @@ -2,6 +2,7 @@ import { createHash } from "node:crypto"; import { constants } from "node:fs"; import { open as openFile } from "node:fs/promises"; import { dirname, isAbsolute, relative, resolve } from "node:path"; +import { isPrivateFileUnder, isStudioPrivatePath } from "@hyperframes/core/safe-path"; export const NOT_MEDIA_PAYLOAD = "NOT_MEDIA_PAYLOAD" as const; @@ -56,6 +57,14 @@ export class NotMediaPayloadError extends Error { } } +/** A file ffmpeg reads as text art, which it would draw into video frames. */ +export class TextArtMediaError extends NotMediaPayloadError { + constructor() { + super([]); + this.message = "Media source is a text file, not a media container"; + } +} + /** Stable, bounded correlation key. Never the raw element id or source. */ export function fingerprintElementId(elementId: string): string { return createHash("sha256").update(elementId).digest("hex").slice(0, 16); @@ -330,7 +339,7 @@ interface Walk { } /** - * True for an HLS or ffconcat list naming a file outside its folder; ffmpeg would open it. Never throws. + * True for an HLS or ffconcat list naming a file outside its folder or private in it; ffmpeg would open it. * `placedAt` is where the file will live when it is read from elsewhere first (a download not yet published). */ export async function playlistLeavesItsFolder( @@ -430,6 +439,7 @@ async function entryLeaves( if (HTTP_URL.test(entry)) return false; for (const local of new Set([entry, percentDecoded(entry)])) { const nested = resolve(base, local); + if (isPrivateFileUnder(walk.folder, nested, isStudioPrivatePath)) return true; let verdict = walk.verdicts.get(nested); if (verdict === undefined) { verdict = await listLeaves(nested, nested, walk, depth + 1); diff --git a/packages/producer/src/services/animatedGifPrep.test.ts b/packages/producer/src/services/animatedGifPrep.test.ts index 44948841ea..9c9c6dc927 100644 --- a/packages/producer/src/services/animatedGifPrep.test.ts +++ b/packages/producer/src/services/animatedGifPrep.test.ts @@ -1,11 +1,12 @@ import { describe, expect, it } from "bun:test"; -import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; +import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; -import { join } from "node:path"; +import { dirname, join } from "node:path"; import { parseHTML } from "linkedom"; import { buildAnimatedGifTranscodeArgs, prepareAnimatedGifInputs, + resolveLocalImageSourcePath, type AnimatedGifTranscodeRequest, } from "./animatedGifPrep.js"; @@ -97,6 +98,36 @@ describe("buildAnimatedGifTranscodeArgs", () => { }); describe("prepareAnimatedGifInputs", () => { + it.each([".git/sticker.gif", ".hyperframes/sticker.gif", ".claude/sticker.gif"])( + "never transcodes the private project file %s", + async (src) => { + const projectDir = makeProject(); + try { + mkdirSync(dirname(join(projectDir, src)), { recursive: true }); + writeFileSync(join(projectDir, src), gif([...frame(5), ...frame(15)], 0)); + const calls: AnimatedGifTranscodeRequest[] = []; + const result = await prepareAnimatedGifInputs( + ``, + { + projectDir, + downloadDir: projectDir, + transcode: async (request) => { + calls.push(request); + writeFileSync(request.outputPath, "webm"); + }, + }, + ); + expect(calls).toEqual([]); + expect(result.preparedGifs).toEqual([]); + expect( + resolveLocalImageSourcePath(src, { projectDir, downloadDir: projectDir }), + ).toBeNull(); + } finally { + rmSync(projectDir, { recursive: true, force: true }); + } + }, + ); + it("rewrites animated GIF images to muted looped videos", async () => { const projectDir = makeProject(); writeFileSync(join(projectDir, "sticker.gif"), gif([...frame(5), ...frame(15)], 0)); diff --git a/packages/producer/src/services/animatedGifPrep.ts b/packages/producer/src/services/animatedGifPrep.ts index ae5249ca3f..373f10dee8 100644 --- a/packages/producer/src/services/animatedGifPrep.ts +++ b/packages/producer/src/services/animatedGifPrep.ts @@ -4,7 +4,12 @@ import { copyFileSync, existsSync, mkdirSync, readFileSync, rmSync, statSync } f import { dirname, isAbsolute, join, resolve } from "node:path"; import { parseHTML } from "linkedom"; import { parseAnimatedGifMetadata, type AnimatedGifMetadata } from "@hyperframes/core"; -import { DEFAULT_VP9_CPU_USED, runFfmpeg, renameWithRetry } from "@hyperframes/engine"; +import { + DEFAULT_VP9_CPU_USED, + isPrivateUnderAssetRoots, + runFfmpeg, + renameWithRetry, +} from "@hyperframes/engine"; import { namesAnotherNetworkRoot } from "@hyperframes/parsers/asset-resolution"; import { isHttpUrl } from "../utils/urlDownloader.js"; import { escapesAssetRoot, outsideAssetRefusal } from "../utils/paths.js"; @@ -106,6 +111,7 @@ export function resolveLocalImageSourcePath( const allowed = (candidate: string) => !namesAnotherNetworkRoot(options.projectDir, candidate) && + !isPrivateUnderAssetRoots(candidate, [options.projectDir, options.downloadDir]) && ((!escapesAssetRoot(candidate, options.assetRoot) && outsideAssetRefusal(options.projectDir, candidate) !== "blocklisted") || !escapesAssetRoot(candidate, options.downloadDir)); diff --git a/packages/producer/src/services/assetMediaType.test.ts b/packages/producer/src/services/assetMediaType.test.ts index f0d99daa57..45894ed35b 100644 --- a/packages/producer/src/services/assetMediaType.test.ts +++ b/packages/producer/src/services/assetMediaType.test.ts @@ -1,7 +1,15 @@ import { afterAll, beforeAll, describe, expect, it } from "vitest"; -import { copyFileSync, existsSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; +import { + copyFileSync, + existsSync, + mkdirSync, + mkdtempSync, + rmSync, + symlinkSync, + writeFileSync, +} from "node:fs"; import { tmpdir } from "node:os"; -import { join } from "node:path"; +import { dirname, join } from "node:path"; import { NOT_MEDIA_PAYLOAD, NotMediaPayloadError } from "@hyperframes/engine"; import { ASSET_MEDIA_TYPE_MISMATCH, @@ -289,7 +297,7 @@ describe("dropMediaOutsideProject", () => { const workDir = join(root, "work"); afterAll(() => rmSync(root, { recursive: true, force: true })); - const keptVideos = (srcs: Record) => { + const keptVideos = (srcs: Record, allowOutsideProjectAssets = false) => { mkdirSync(projectDir, { recursive: true }); mkdirSync(join(root, "outside"), { recursive: true }); writeFileSync(join(root, "outside", "secret.txt"), "secret"); @@ -299,10 +307,32 @@ describe("dropMediaOutsideProject", () => { }); const composition = { videos: Object.entries(srcs).map(video), audios: [], images: [] }; const log = { error() {}, info() {}, debug() {}, warn() {} }; - dropMediaOutsideProject({ projectDir, workDir, composition, log }); + dropMediaOutsideProject({ projectDir, workDir, composition, log, allowOutsideProjectAssets }); return composition.videos.map((v) => v.id); }; + it.each([false, true])( + "drops media that names a private project file (outside assets allowed: %s)", + (allowOutside) => { + const srcs = { + git: ".git/clip.mp4", + app: ".hyperframes/clip.mp4", + agent: ".claude/clip.mp4", + ssh: "media/.ssh/clip.mp4", + key: "media/id_rsa", + link: "linked.mp4", + local: "media/clip.mp4", + }; + for (const src of Object.values(srcs).filter((src) => src !== "linked.mp4")) { + mkdirSync(dirname(join(projectDir, src)), { recursive: true }); + writeFileSync(join(projectDir, src), "x"); + } + rmSync(join(projectDir, "linked.mp4"), { force: true }); + symlinkSync(join(projectDir, ".git", "clip.mp4"), join(projectDir, "linked.mp4")); + expect(keptVideos(srcs, allowOutside)).toEqual(["local"]); + }, + ); + it("drops a URL scheme followed by parent folders, in any letter case", () => { const schemes = ["data:", "DATA:", "blob:", "Blob:", "about:", "https:", "HTTP:"]; const srcs = Object.fromEntries( diff --git a/packages/producer/src/services/assetMediaType.ts b/packages/producer/src/services/assetMediaType.ts index 33840c305a..90bb432f73 100644 --- a/packages/producer/src/services/assetMediaType.ts +++ b/packages/producer/src/services/assetMediaType.ts @@ -5,6 +5,7 @@ import { isHttpUrl, isInsideAssetRoots, isNotMediaPayload, + isPrivateUnderAssetRoots, playlistLeavesItsFolder, NotMediaPayloadError, probeMediaProfile, @@ -97,7 +98,7 @@ export function mediaAssetRoots(projectDir: string, compiledDir: string): string return [projectDir, compiledDir, join(dirname(compiledDir), "downloads")]; } -/** Treats local media outside the project, compiled and download dirs as missing: no probe or extraction opens it. */ +/** Treats local media outside the project, compiled and download dirs, or private there, as missing: none is opened. */ export function dropMediaOutsideProject(input: { projectDir: string; workDir: string; @@ -111,6 +112,10 @@ export function dropMediaOutsideProject(input: { const staysInside = ({ id, src }: { id: string; src: string }) => { if (!src || isHttpUrl(src)) return true; const path = resolveProjectRelativeSrc(src, projectDir, compiledDir); + if (isPrivateUnderAssetRoots(path, roots)) { + log.warn(`[Render] Media "${id}" source is missing: it is a private project file`, { src }); + return false; + } if (isInsideAssetRoots(path, roots)) return true; if (input.allowOutsideProjectAssets) { const refusal = outsideAssetRefusal(projectDir, path); diff --git a/packages/producer/src/services/htmlCompiler.test.ts b/packages/producer/src/services/htmlCompiler.test.ts index a862891235..a9df90c83e 100644 --- a/packages/producer/src/services/htmlCompiler.test.ts +++ b/packages/producer/src/services/htmlCompiler.test.ts @@ -3398,6 +3398,25 @@ describe("compileForRender non-media payload sniff (STUDIO-5433)", () => { expect(warnings.join("\n")).toContain("text document"); expect(warnings.join("\n")).toContain("a1"); }); + + it.each([".git/clip.mp4", ".hyperframes/clip.mp4", ".claude/clip.mp4", "keys/id_rsa"])( + "never probes the private project file %s that a