diff --git a/pkgs/lab-ops-hub/src/index.ts b/pkgs/lab-ops-hub/src/index.ts index 2e457fe64f..3c23f0794d 100644 --- a/pkgs/lab-ops-hub/src/index.ts +++ b/pkgs/lab-ops-hub/src/index.ts @@ -14,6 +14,7 @@ import { validateMachineBody, validateNoteBody, validateTaskBody, + fileResponseForKey, } from "./lib"; // --------------------------------------------------------------------------- @@ -388,11 +389,7 @@ export default { } const fileMatch = p.match(/^\/api\/files\/(.+)$/); if (fileMatch && req.method === "GET") { - const obj = await env.FILES.get(decodeURIComponent(fileMatch[1])); - if (!obj) return json({ error: "not found in R2" }, 404); - return new Response(obj.body, { - headers: { "content-type": obj.httpMetadata?.contentType ?? "application/octet-stream" }, - }); + return fileResponseForKey(fileMatch[1], (key) => env.FILES.get(key)); } // Live board (Durable Object, one global room) diff --git a/pkgs/lab-ops-hub/src/lib.ts b/pkgs/lab-ops-hub/src/lib.ts index bc58f02ba3..77bb09f0e0 100644 --- a/pkgs/lab-ops-hub/src/lib.ts +++ b/pkgs/lab-ops-hub/src/lib.ts @@ -469,6 +469,38 @@ export function uploadKey(name: string, now: number, rand: string = crypto.rando return `uploads/${now}-${rand}-${name.replace(/[^\w.\-]+/g, "_")}`; } +// Decode the path suffix before looking up an uploaded file. Keep this +// route-specific response logic pure so malformed keys and the 404 control +// can be covered without constructing a Worker environment. +export async function fileResponseForKey( + encodedKey: string, + getFile: (key: string) => Promise<{ + body: ReadableStream | null; + httpMetadata?: { contentType?: string }; + } | null>, +): Promise { + let key: string; + try { + key = decodeURIComponent(encodedKey); + } catch { + return new Response(JSON.stringify({ error: "bad file key encoding" }, null, 2), { + status: 400, + headers: { "content-type": "application/json" }, + }); + } + + const obj = await getFile(key); + if (!obj) { + return new Response(JSON.stringify({ error: "not found in R2" }, null, 2), { + status: 404, + headers: { "content-type": "application/json" }, + }); + } + return new Response(obj.body, { + headers: { "content-type": obj.httpMetadata?.contentType ?? "application/octet-stream" }, + }); +} + // --- HTML extraction (#569) ------------------------------------------------- // extractHtml mirrors the container crawler's Extractor (container/app.py, // an html.parser.HTMLParser) so the Worker fallback in index.ts and the diff --git a/pkgs/lab-ops-hub/test/file-response.test.ts b/pkgs/lab-ops-hub/test/file-response.test.ts new file mode 100644 index 0000000000..fe848414c3 --- /dev/null +++ b/pkgs/lab-ops-hub/test/file-response.test.ts @@ -0,0 +1,53 @@ +import assert from "node:assert/strict"; +import { test } from "node:test"; +import { fileResponseForKey } from "../src/lib.ts"; + +test("fileResponseForKey rejects malformed percent-encoded keys with a safe 400", async () => { + for (const encodedKey of ["%", "%zz", "uploads%2", "%E0%A4%A"]) { + let lookedUp = false; + const response = await fileResponseForKey(encodedKey, async () => { + lookedUp = true; + return null; + }); + + assert.equal(response.status, 400, encodedKey); + assert.match(response.headers.get("content-type") ?? "", /application\/json/, encodedKey); + assert.deepEqual(await response.json(), { error: "bad file key encoding" }, encodedKey); + assert.equal(lookedUp, false, encodedKey); + } +}); + +test("fileResponseForKey keeps valid encoded keys and downloads unchanged", async () => { + let requestedKey = ""; + const response = await fileResponseForKey("uploads%2Freport.txt", async (key) => { + requestedKey = key; + return { + body: new Response("file contents").body, + httpMetadata: { contentType: "text/plain" }, + }; + }); + + assert.equal(requestedKey, "uploads/report.txt"); + assert.equal(response.status, 200); + assert.equal(response.headers.get("content-type"), "text/plain"); + assert.equal(await response.text(), "file contents"); +}); + +test("fileResponseForKey keeps the missing-object 404 response", async () => { + const response = await fileResponseForKey("uploads%2Fmissing.txt", async (key) => { + assert.equal(key, "uploads/missing.txt"); + return null; + }); + + assert.equal(response.status, 404); + assert.deepEqual(await response.json(), { error: "not found in R2" }); +}); + +test("fileResponseForKey leaves R2 failures for the worker's 500 handler", async () => { + await assert.rejects( + fileResponseForKey("uploads%2Freport.txt", async () => { + throw new Error("R2 unavailable"); + }), + /R2 unavailable/, + ); +});