Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix: enforce raw source byte limits before transcoding

Richie McIlroy3724271cd5f0
diff --git a/apps/render-farm/src/coordinator.test.ts b/apps/render-farm/src/coordinator.test.ts
index 1461d00c2..8bf74479c 100644
--- a/apps/render-farm/src/coordinator.test.ts
+++ b/apps/render-farm/src/coordinator.test.ts
@@ -16,7 +16,7 @@ import * as recovery from "./recovery";
import { pickQueued } from "./scheduler";
import * as validate from "./validate";
-function harness() {
+function harness(env: Record<string, string> = {}) {
const objects = new Map<string, Uint8Array>();
const writes: string[] = [];
const timers: (() => void)[] = [];
@@ -93,6 +93,7 @@ function harness() {
RF_LOCAL_AUDIO_SLOTS: "0",
RF_HLS: "1",
RF_CALLBACK_HOSTS: "cap.test",
+ ...env,
},
},
Bun: {
@@ -580,6 +581,7 @@ describe("transcodes", () => {
test("are queued once, go to a video slot first and settle on the worker's report", async () => {
const h = harness();
+ h.objects.set(request.source, new Uint8Array(100));
const created = (await (await call(h, "/transcodes", request)).json()) as {
id: string;
status: string;
@@ -633,6 +635,7 @@ describe("transcodes", () => {
test("completion requires the active dispatch and matching stored bytes", async () => {
const h = harness();
+ h.objects.set(request.source, new Uint8Array(100));
const created = (await (await call(h, "/transcodes", request)).json()) as {
id: string;
};
@@ -669,6 +672,7 @@ describe("transcodes", () => {
test("a completion cannot settle a dispatch retired during object verification", async () => {
const h = harness();
+ h.objects.set(request.source, new Uint8Array(100));
const created = (await (await call(h, "/transcodes", request)).json()) as {
id: string;
};
@@ -698,6 +702,53 @@ describe("transcodes", () => {
});
});
+ test("standalone transcodes reject missing and oversized raw sources before dispatch", async () => {
+ const h = harness({ RF_MAX_SOURCE_BYTES: "1000" });
+ for (const size of [0, 1001]) {
+ if (size > 0) h.objects.set(request.source, new Uint8Array(size));
+ expect(
+ await (await call(h, "/transcodes", request)).json(),
+ ).toMatchObject({
+ status: "error",
+ });
+ }
+ h.objects.set(request.source, new Uint8Array(1000));
+ expect(await (await call(h, "/transcodes", request)).json()).toMatchObject({
+ status: "queued",
+ });
+ });
+
+ test("manifest quotas count every raw source before scheduling any transcode", async () => {
+ const h = harness({ RF_MAX_SOURCE_BYTES: "1000" });
+ const prefix = "owner/video/project";
+ const files = ["display", "camera"].map((name) => ({
+ path: `${name}.mp4`,
+ key: `${prefix}/${name}.mp4`,
+ transcodeFrom: `owner/video/${name}.webm`,
+ }));
+ for (const file of files) {
+ h.objects.set(file.transcodeFrom, new Uint8Array(501));
+ }
+ for (const size of [undefined, 1]) {
+ h.objects.set(
+ `${prefix}/manifest.json`,
+ new TextEncoder().encode(
+ JSON.stringify({ files: files.map((file) => ({ ...file, size })) }),
+ ),
+ );
+ await expect(h.sourceIndex(prefix, "owner/video/")).rejects.toThrow(
+ "1002 bytes (limit 1000)",
+ );
+ for (const file of files) {
+ const id = createHash("sha256")
+ .update(file.key)
+ .digest("hex")
+ .slice(0, 16);
+ expect((await call(h, `/transcodes/${id}`)).status).toBe(404);
+ }
+ }
+ });
+
test("must stay inside their source folder", async () => {
const h = harness();
for (const body of [
diff --git a/apps/render-farm/src/coordinator.ts b/apps/render-farm/src/coordinator.ts
index df630b2f9..27172c6b9 100644
--- a/apps/render-farm/src/coordinator.ts
+++ b/apps/render-farm/src/coordinator.ts
@@ -547,6 +547,20 @@ async function sourceIndex(
checkManifest(manifest, prefix, sourceRoot);
const keyOf = (file: { path: string; key?: string }) =>
file.key ?? `${prefix}/${file.path}`;
+ const sourceFiles = await Promise.all(
+ manifest.files.map(async (file) => ({
+ ...file,
+ size:
+ file.transcodeFrom === undefined
+ ? file.size
+ : await transcodeSourceSize(file.transcodeFrom),
+ })),
+ );
+ const sourceBounds = checkManifestBounds(
+ { files: sourceFiles },
+ SOURCE_LIMITS,
+ );
+ if (sourceBounds) throw new Error(sourceBounds);
await Promise.all(
manifest.files.map(async (file) => {
if (file.transcodeFrom === undefined) return;
@@ -1415,6 +1429,21 @@ type Transcode = {
/** By output key: one transcode per target, shared by every job needing it. */
const transcodes = new Map<string, Transcode>();
+async function transcodeSourceSize(source: string) {
+ const head = await s3.head(source);
+ if (!head || !Number.isSafeInteger(head.size) || head.size <= 0) {
+ throw new Error(
+ `transcode source ${source} is missing or has an invalid size`,
+ );
+ }
+ if (head.size > SOURCE_LIMITS.sourceBytes) {
+ throw new Error(
+ `transcode source ${source} is ${head.size} bytes (limit ${SOURCE_LIMITS.sourceBytes})`,
+ );
+ }
+ return head.size;
+}
+
async function ensureTranscode(source: string, output: string) {
const existing = transcodes.get(output);
if (existing && existing.state !== "error") return existing;
@@ -1435,13 +1464,19 @@ async function ensureTranscode(source: string, output: string) {
settle: resolve,
};
transcodes.set(output, transcode);
- // Made by an earlier export (or a previous coordinator): reuse it.
- const head = await s3.head(output).catch(() => null);
- if (head && head.size > 0) {
- settleTranscode(transcode, "ready", head.size);
- } else {
- transcode.state = "queued";
- dispatch();
+ try {
+ const head = await s3.head(output);
+ if (head && head.size > 0 && head.size <= SOURCE_LIMITS.sourceBytes) {
+ settleTranscode(transcode, "ready", head.size);
+ } else if (head) {
+ throw new Error("stored transcode output has an invalid size");
+ } else {
+ await transcodeSourceSize(source);
+ transcode.state = "queued";
+ dispatch();
+ }
+ } catch (error) {
+ settleTranscode(transcode, "error", String(error));
}
return transcode;
}