Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/engine/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,7 @@ export {
type PublicHttpsTextOptions,
} from "./utils/urlDownloader.js";
export { isBlockedNetworkHost } from "./utils/networkPolicy.js";
export { isInsideAssetRoots } from "./utils/assetRoots.js";
export { onRenderRefusal } from "./services/publicNetwork/captureProxy.js";
export type { RefusedHostClass } from "./services/publicNetwork/publicNetwork.js";
export {
Expand Down
15 changes: 15 additions & 0 deletions packages/engine/src/services/audioMixer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
type AudioMetadata,
} from "../utils/ffprobe.js";
import { isNotMediaPayload } from "../utils/notMediaPayload.js";
import { isInsideAssetRoots } 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";
Expand Down Expand Up @@ -1432,6 +1433,8 @@ export async function processCompositionAudio(
urlByAbsentLocalPath?: ReadonlyMap<string, string>;
audioSidecarUrlByLocalPath?: ReadonlyMap<string, string>;
keepEncoderInputAt?: string;
/** Folders a local source must be in; one outside them is never opened. Unset allows any folder. */
assetRoots?: readonly string[];
},
compiledDir?: string,
): Promise<MixResult> {
Expand Down Expand Up @@ -1496,6 +1499,18 @@ 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;
}
}

Expand Down
13 changes: 13 additions & 0 deletions packages/engine/src/services/videoFrameExtractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +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 { alphaDecoderFor } from "@hyperframes/parsers/media-alpha";
import {
MEDIA_RENDER_ID_ATTR,
Expand Down Expand Up @@ -311,6 +312,8 @@ export interface ExtractionOptions {
/** Directories the caller rewrites on every render (downloaded media); their sources are cached by content. */
contentKeyedDirs?: readonly string[];
urlByAbsentLocalPath?: ReadonlyMap<string, string>;
/** Folders a local source must be in; one outside them is never opened. Unset allows any folder. */
assetRoots?: readonly string[];
shownBox?: { width: number; height: number };
shownBoxes?: ReadonlyMap<string, { width: number; height: number }>;
}
Expand Down Expand Up @@ -2242,6 +2245,16 @@ export async function extractAllVideoFrames(
resolvedVideos.push({ video, videoPath: remoteUrl });
continue;
}
const roots = options.assetRoots;
if (!isHttpUrl(videoPath) && roots && !isInsideAssetRoots(videoPath, roots)) {
errors.push({
videoId: video.id,
kind: "source_missing",
retryable: false,
error: `Video source is outside the project folder: ${video.src}`,
});
continue;
}

if (isHttpUrl(videoPath)) {
const downloadDir = join(options.outputDir, DOWNLOAD_SUBDIR);
Expand Down
9 changes: 9 additions & 0 deletions packages/engine/src/utils/assetRoots.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { existsSync } from "node:fs";
import { isSafePath } from "@hyperframes/core/safe-path";
import { isWithinProjectRoot } from "@hyperframes/parsers/asset-resolution";

export function isInsideAssetRoots(path: string, roots: readonly string[]): boolean {
if (roots.some((root) => isSafePath(root, path))) return true;
// 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));
}
48 changes: 48 additions & 0 deletions packages/producer/src/services/assetMediaType.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { NOT_MEDIA_PAYLOAD, NotMediaPayloadError } from "@hyperframes/engine";
import {
ASSET_MEDIA_TYPE_MISMATCH,
AssetMediaTypeMismatchError,
dropMediaOutsideProject,
preflightCompositionAssetMediaTypes,
} from "./assetMediaType.js";
import { synthesizeMediaFixture } from "./mediaTypeTestFixtures.js";
Expand Down Expand Up @@ -254,6 +255,13 @@ describe("preflightCompositionAssetMediaTypes", () => {
},
);

it.each(["data:x/../streamed-preview.html", "BLOB:a/../streamed-preview.html"])(
"sniffs a project file named behind a URL scheme: %s",
async (videoSrc) => {
await expect(run({ videoSrc })).rejects.toBeInstanceOf(NotMediaPayloadError);
},
);

it("leaves an SVG image source alone", async () => {
// ffprobe reads SVG through its svg_pipe demuxer, so an XML body is a
// legitimate <img> payload and must not be swept up by the sniff.
Expand All @@ -274,3 +282,43 @@ describe("preflightCompositionAssetMediaTypes", () => {
await expect(run({ videoSrc: "mutable-media" }, signal)).resolves.toBeUndefined();
});
});

describe("dropMediaOutsideProject", () => {
const root = mkdtempSync(join(tmpdir(), "hf-media-outside-"));
const projectDir = join(root, "project");
const workDir = join(root, "work");
afterAll(() => rmSync(root, { recursive: true, force: true }));

const keptVideos = (srcs: Record<string, string>) => {
mkdirSync(projectDir, { recursive: true });
mkdirSync(join(root, "outside"), { recursive: true });
writeFileSync(join(root, "outside", "secret.txt"), "secret");
const video = ([id, src]: [string, string]) => ({
...{ id, src, start: 0, end: 1, mediaStart: 0 },
...{ loop: false, hasAudio: false },
});
const composition = { videos: Object.entries(srcs).map(video), audios: [], images: [] };
const log = { error() {}, info() {}, debug() {}, warn() {} };
dropMediaOutsideProject({ projectDir, workDir, composition, log });
return composition.videos.map((v) => v.id);
};

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(
schemes.map((scheme, i) => [`v${i}`, `${scheme}x/../../outside/secret.txt`]),
);
expect(keptVideos(srcs)).toEqual([]);
});

it("keeps genuine inline, blob and remote media", () => {
const srcs = {
data: "data:video/mp4;base64,AAAAIGZ0eXBpc29t",
long: `data:video/mp4;base64,${"A".repeat(5000)}/${"B".repeat(300)}`,
blob: "blob:https://studio.example/0f6c9a52-6f1e-4d1e-9a51-2b1f3c4d5e6f",
remote: "https://media.example.com/clip.mp4",
local: "clip.mp4",
};
expect(keptVideos(srcs)).toEqual(Object.keys(srcs));
});
});
18 changes: 10 additions & 8 deletions packages/producer/src/services/assetMediaType.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { existsSync } from "node:fs";
import { join } from "node:path";
import { dirname, join } from "node:path";
import {
fingerprintElementId,
isHttpUrl,
isInsideAssetRoots,
isNotMediaPayload,
playlistLeavesItsFolder,
NotMediaPayloadError,
Expand All @@ -13,7 +15,7 @@ import {
type VideoElement,
} from "@hyperframes/engine";
import { withMediaProbeSlot } from "../utils/mediaProbeConcurrency.js";
import { escapesAssetRoot, leftOutOfRender, outsideAssetRefusal } from "../utils/paths.js";
import { leftOutOfRender, outsideAssetRefusal } from "../utils/paths.js";
import type { ProducerLogger } from "../logger.js";

export const ASSET_MEDIA_TYPE_MISMATCH = "ASSET_MEDIA_TYPE_MISMATCH" as const;
Expand Down Expand Up @@ -91,8 +93,8 @@ interface MediaReference {
expected: AssetElementMediaType;
}

function isRemoteOrInlineSource(src: string): boolean {
return /^(?:https?:|data:|blob:|about:)/i.test(src.trim());
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. */
Expand All @@ -105,11 +107,11 @@ export function dropMediaOutsideProject(input: {
}): void {
const { projectDir, workDir, composition, log } = input;
const compiledDir = join(workDir, "compiled");
const roots = [projectDir, compiledDir, join(workDir, "downloads")];
const roots = mediaAssetRoots(projectDir, compiledDir);
const staysInside = ({ id, src }: { id: string; src: string }) => {
if (!src || isRemoteOrInlineSource(src)) return true;
if (!src || isHttpUrl(src)) return true;
const path = resolveProjectRelativeSrc(src, projectDir, compiledDir);
if (roots.some((root) => !escapesAssetRoot(path, root))) return true;
if (isInsideAssetRoots(path, roots)) return true;
if (input.allowOutsideProjectAssets) {
const refusal = outsideAssetRefusal(projectDir, path);
if (refusal) log.warn(`[Render] ${leftOutOfRender(`Media "${id}"`, src, refusal)}`);
Expand Down Expand Up @@ -166,7 +168,7 @@ export async function preflightCompositionAssetMediaTypes(input: {

const byPath = new Map<string, MediaReference[]>();
for (const reference of references) {
if (!reference.src || isRemoteOrInlineSource(reference.src)) continue;
if (!reference.src || isHttpUrl(reference.src)) continue;
const resolvedPath = resolveProjectRelativeSrc(
reference.src,
input.projectDir,
Expand Down
2 changes: 2 additions & 0 deletions packages/producer/src/services/distributed/plan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1469,6 +1469,7 @@ function mixPlanAudio(c: PlanComposition, compiledDir: string) {
compiledDir,
urlByAbsentLocalPath: c.urlByAbsentLocalPath,
audioSidecarUrlByLocalPath: c.audioSidecarUrlByLocalPath,
allowOutsideProjectAssets: c.job.config.allowOutsideProjectAssets,
totalFrames: c.totalFrames,
fps: c.job.config.fps,
ffmpegProcessTimeout: c.cfg.ffmpegProcessTimeout,
Expand Down Expand Up @@ -1878,6 +1879,7 @@ export async function buildPlanAudio(
await appendVideoAudioFromProbe({
...c,
downloadDir: audioWorkDir(c.workDir),
allowOutsideProjectAssets: c.job.config.allowOutsideProjectAssets,
signal: c.abortSignal,
});
logPlanStage(c.log, "extract", stageStarted);
Expand Down
87 changes: 84 additions & 3 deletions packages/producer/src/services/outsideProjectAssets.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ import { join, relative, resolve } from "node:path";
import { pathToFileURL } from "node:url";
import { resolveConfig } from "@hyperframes/engine";
import { runCompileStage } from "./render/stages/compileStage.js";
import { runExtractVideosStage } from "./render/stages/extractVideosStage.js";
import {
appendVideoAudioFromProbe,
runExtractVideosStage,
} from "./render/stages/extractVideosStage.js";
import { runAudioStage } from "./render/stages/audioStage.js";
import { collectRenderMedia } from "./renderMediaCollector.js";
import { createRenderJob } from "./renderOrchestrator.js";
import { createFileServer } from "./fileServer.js";
import { plan } from "./distributed/plan.js";
Expand Down Expand Up @@ -99,7 +104,10 @@ function outsideProject(media: boolean) {
video("dotdot", "../outside/clip.mp4"),
video("encoded", "..%2Foutside%2Fclip.mp4"),
video("remote", "https://media.example.com/clip.mp4"),
video("data-scheme", "data:x/../../outside/clip.mp4"),
video("blob-scheme", "BLOB:a/../../outside/clip.mp4"),
`<audio id="tone" src="../outside/clip.mp4" data-start="0" data-duration="1"></audio>`,
`<audio id="tone-scheme" src="Data:x/../../outside/clip.mp4" data-start="0" data-duration="1"></audio>`,
].join("");
writeFileSync(
join(projectDir, "index.html"),
Expand Down Expand Up @@ -227,9 +235,10 @@ describe.skipIf(!HAS_FFMPEG)("render servers keep markup inside the project", ()
).toEqual([]);
expect(existsSync(join(compiledDir, "hf-ext"))).toBe(false);
expect(result.composition.videos.map((video) => video.id)).toEqual(["remote"]);
expect(result.composition.audios.filter((audio) => audio.id === "tone")).toEqual([]);
expect(result.composition.audios.filter((audio) => audio.id.startsWith("tone"))).toEqual([]);
expect(result.composition.images).toEqual([]);
for (const id of ["absolute", "fileurl", "dotdot", "encoded", "tone"]) {
const dropped = ["absolute", "fileurl", "dotdot", "encoded", "data-scheme", "blob-scheme"];
for (const id of [...dropped, "tone", "tone-scheme"]) {
expect(warnings.some((line) => line.includes(`"${id}" source is missing`))).toBe(true);
}
expect(extracted.extractionResult?.extracted.map((frames) => frames.videoId)).toEqual([
Expand Down Expand Up @@ -355,3 +364,75 @@ describe.skipIf(!HAS_FFMPEG)("render servers keep markup inside the project", ()
expect(result.compiled.html).toContain("OUTSIDE-SCENE");
});
});

// These sources skip the earlier filters on purpose: each reader must refuse them on its own.
describe.skipIf(!HAS_FFMPEG)("render readers open nothing outside the project", () => {
const escaping = (file: string) => [
`data:x/../../outside/${file}`,
`BLOB:a/../../outside/${file}`,
];
const videos = escaping("clip.mp4")
.map(
(src, i) =>
`<video id="v${i}" src="${src}" data-start="0" data-duration="1" data-has-audio="true"></video>`,
)
.join("");

function reachedReaders(media: string) {
const { root, projectDir, outsideDir } = outsideProject(false);
const workDir = join(root, "work");
const compiledDir = join(workDir, "compiled");
mkdirSync(compiledDir, { recursive: true });
const collected = collectRenderMedia(
`<div data-composition-id="root" data-width="320" data-height="240" data-duration="1">${media}</div>`,
);
const composition = { ...collected, duration: 1, width: 320, height: 240 };
tool.calls.length = 0;
const openedOutside = () => tool.calls.filter((call) => call.includes(outsideDir));
return { projectDir, workDir, compiledDir, composition, openedOutside };
}

it.each(["force-sdr", "auto"] as const)(
"video frame extraction and HDR probes (hdrMode %s)",
async (hdrMode) => {
const images = escaping("secret.png")
.map((src, i) => `<img id="i${i}" src="${src}" data-start="0" data-duration="1">`)
.join("");
const { projectDir, compiledDir, composition, openedOutside } = reachedReaders(
videos + images,
);
expect(composition.videos).toHaveLength(2);
expect(composition.images).toHaveLength(2);
const job = createRenderJob({ fps: { num: 30, den: 1 }, quality: "draft", hdrMode });
const log = { error() {}, info() {}, debug() {}, warn() {} };
await runExtractVideosStage({
...{ projectDir, compiledDir, job, cfg: resolveConfig(), log, composition },
...{ abortSignal: undefined, assertNotAborted: () => {} },
}).catch(() => undefined);
expect(openedOutside()).toEqual([]);
},
);

it("audio probes of video sound", async () => {
const { projectDir, compiledDir, composition, openedOutside } = reachedReaders(videos);
composition.audios.length = 0;
await appendVideoAudioFromProbe({ projectDir, compiledDir, composition });
expect(openedOutside()).toEqual([]);
expect(composition.audios).toEqual([]);
});

it("audio mix", async () => {
const audios = escaping("clip.mp4")
.map((src, i) => `<audio id="a${i}" src="${src}" data-start="0" data-duration="1"></audio>`)
.join("");
const { projectDir, workDir, compiledDir, composition, openedOutside } = reachedReaders(audios);
expect(composition.audios).toHaveLength(2);
const result = await runAudioStage({
...{ projectDir, workDir, compiledDir, audios: composition.audios },
...{ totalFrames: 30, fps: { num: 30, den: 1 }, ffmpegProcessTimeout: 30_000, audioGain: 1 },
...{ abortSignal: undefined, assertNotAborted: () => {} },
});
expect(openedOutside()).toEqual([]);
expect(result.hasAudio).toBe(false);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ describe("runAudioStage", () => {
ffmpegProcessTimeout: 3_600_000,
audioGain: 1,
keepEncoderInputAt: expect.stringMatching(/audio-source\.wav$/),
assetRoots: expect.any(Array),
},
expect.any(String),
);
Expand Down Expand Up @@ -268,6 +269,7 @@ describe("runAudioStage", () => {
audioGain: 1,
masterFxChain: chain,
keepEncoderInputAt: expect.stringMatching(/audio-source\.wav$/),
assetRoots: expect.any(Array),
},
expect.any(String),
);
Expand Down
5 changes: 5 additions & 0 deletions packages/producer/src/services/render/stages/audioStage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
type MixResult,
} from "@hyperframes/engine";
import { audioWorkDir, type CompositionMetadata } from "../shared.js";
import { mediaAssetRoots } from "../../assetMediaType.js";
import type { ProducerLogger } from "../../../logger.js";
import { encoderFailureError } from "../encoderInterruption.js";
import { limitDeliveryPeaks } from "../audioDeliveryLimit.js";
Expand All @@ -52,6 +53,7 @@ export interface AudioStageInput {
masterFxChain?: string;
urlByAbsentLocalPath?: ReadonlyMap<string, string>;
audioSidecarUrlByLocalPath?: ReadonlyMap<string, string>;
allowOutsideProjectAssets?: boolean;
abortSignal: AbortSignal | undefined;
assertNotAborted: () => void;
/** Where a per-track failure's detail goes. Optional so tests need not pass one. */
Expand Down Expand Up @@ -213,6 +215,9 @@ async function mixAudioForDelivery(
urlByAbsentLocalPath,
audioSidecarUrlByLocalPath,
keepEncoderInputAt: pcmSourcePath,
assetRoots: input.allowOutsideProjectAssets
? undefined
: mediaAssetRoots(projectDir, compiledDir),
},
compiledDir,
);
Expand Down
Loading
Loading