Public: anyone can read this; the crew can change it.
A dropped file is always served as a download with nosniff, whatever its name or bytes #13413
Check · Review · Land · Live done — Landed and live in production.
mason/forge:mason/13392-a-dropped-file-is-always-served-as-a-dow → main 1 commit fork
4 files
changed +126
−16
src/actions/drop.ts +3 −3
| @@ -129,12 +129,12 @@ export const READ_WORDS: Record<ReadError, string> = { | ||
| 129 | 129 | binary: "this file is not text: read it by its path, with a tool that opens its kind", |
| 130 | 130 | }; |
| 131 | 131 | |
| 132 | /** The file a person downloads or an image is shown from: a path and the type it is served with. */ | |
| 133 | export function dropFile(app: App, actor: User | undefined, repo: Repo, name: string): Result<{ path: string; type: string }, "not_found"> { | |
| 132 | /** The file a person downloads, or the page's thumbnail is drawn from: its path on disk. */ | |
| 133 | export function dropFile(app: App, actor: User | undefined, repo: Repo, name: string): Result<{ path: string }, "not_found"> { | |
| 134 | 134 | if (!writer(app, actor, repo)) return err("not_found"); |
| 135 | 135 | const path = filePath(app.config.dataDir, repo.id, name); |
| 136 | 136 | if (path === undefined) return err("not_found"); |
| 137 | return ok({ path, type: IMAGE_TYPES[extOf(name)] ?? "application/octet-stream" }); | |
| 137 | return ok({ path }); | |
| 138 | 138 | } |
| 139 | 139 | |
| 140 | 140 | /** |
src/drop/store.ts +16
| @@ -53,6 +53,22 @@ export function cleanName(raw: string): string { | ||
| 53 | 53 | return name === "" ? "file" : name; |
| 54 | 54 | } |
| 55 | 55 | |
| 56 | /** | |
| 57 | * The headers every drop download carries, whatever its name or bytes: the drop holds untrusted | |
| 58 | * files (html, svg, polyglots) and none may run on the forge's origin. Always | |
| 59 | * `application/octet-stream`, always `attachment`, always `nosniff`. The name is the stored one, | |
| 60 | * already narrow, but is narrowed again here so a header can never carry a quote, a newline or a | |
| 61 | * byte outside ASCII: `filename` holds the safe set, `filename*` the same name percent-encoded. | |
| 62 | */ | |
| 63 | export function downloadHeaders(name: string): Record<string, string> { | |
| 64 | const safe = name.replace(/[^A-Za-z0-9._ +()-]/g, "_").slice(0, 100) || "file"; | |
| 65 | return { | |
| 66 | "content-type": "application/octet-stream", | |
| 67 | "content-disposition": `attachment; filename="${safe}"; filename*=UTF-8''${encodeURIComponent(safe).replace(/[()]/g, (c) => `%${c.charCodeAt(0).toString(16).toUpperCase()}`)}`, | |
| 68 | "x-content-type-options": "nosniff", | |
| 69 | }; | |
| 70 | } | |
| 71 | ||
| 56 | 72 | export const NAME_WORDS = "a file name is 1 to 100 letters, digits, spaces and . _ - + ( ), starting with a letter or digit"; |
| 57 | 73 | |
| 58 | 74 | export interface DropFile { |
src/http/api/drop.ts +8 −13
| @@ -1,13 +1,15 @@ | ||
| 1 | 1 | /** |
| 2 | 2 | * An app's drop over the API (`actions/drop.ts`): `GET …/drop` lists with each file's type, size |
| 3 | 3 | * and cheap extract, and the quota; `POST …/drop?name=<file>` keeps the request body as that file, |
| 4 | * streamed to disk, never held whole; `GET …/drop/<file>` is the file to a writer (an image is | |
| 5 | * shown as one, everything else downloads) or, with `?range=<start>-<end>`, up to 64 KiB of a text | |
| 6 | * file as JSON; `DELETE …/drop/<file>` removes it. Writers only — everyone else, anonymous | |
| 7 | * included, is a 404, and no response is cacheable or public. | |
| 4 | * streamed to disk, never held whole; `GET …/drop/<file>` is the file to a writer (always a | |
| 5 | * download, octet-stream and nosniff; the page's thumbnails rely on the browser's image decoder) | |
| 6 | * or, with `?range=<start>-<end>`, up to 64 KiB of a text file as JSON; `DELETE …/drop/<file>` | |
| 7 | * removes it. Writers only — everyone else, anonymous included, is a 404, and no response is | |
| 8 | * cacheable or public. | |
| 8 | 9 | */ |
| 9 | 10 | |
| 10 | 11 | import { dropFile, dropList, dropPut, dropRead, dropRemove, PUT_WORDS, READ_WORDS } from "../../actions/drop.ts"; |
| 12 | import { downloadHeaders } from "../../drop/store.ts"; | |
| 11 | 13 | import { loadRepo } from "../api.ts"; |
| 12 | 14 | import type { Handler } from "../context.ts"; |
| 13 | 15 | import { apiError, json, notFound, requireScope } from "../respond.ts"; |
| @@ -53,15 +55,8 @@ export function dropRoutes(router: Router<Handler>): void { | ||
| 53 | 55 | } |
| 54 | 56 | const file = dropFile(ctx.app, ctx.auth.user, repo, name); |
| 55 | 57 | if (!file.ok) return notFound(); |
| 56 | return new Response(Bun.file(file.value.path), { | |
| 57 | headers: { | |
| 58 | ...PRIVATE, | |
| 59 | "content-type": file.value.type, | |
| 60 | // A download, whatever it is: nothing dropped is ever rendered as a page. | |
| 61 | "content-disposition": `attachment; filename="${name.replace(/[^A-Za-z0-9._ +()-]/g, "_")}"`, | |
| 62 | "x-content-type-options": "nosniff", | |
| 63 | }, | |
| 64 | }); | |
| 58 | // A download, whatever it is: nothing dropped is ever rendered as a page. | |
| 59 | return new Response(Bun.file(file.value.path), { headers: { ...PRIVATE, ...downloadHeaders(name) } }); | |
| 65 | 60 | }); |
| 66 | 61 | |
| 67 | 62 | router.add("DELETE", "/api/v1/repos/:owner/:name/drop/:file", (ctx) => { |
test/drop-serve.test.ts added +99
| @@ -0,0 +1,99 @@ | ||
| 1 | /** | |
| 2 | * A dropped file is always served as a download (#13392): whatever its name or bytes — html, svg, a | |
| 3 | * polyglot that is both an image and a page — the response is `application/octet-stream`, | |
| 4 | * `attachment` and `nosniff`, so nothing dropped can run on the forge's origin. A name with quotes | |
| 5 | * or newlines never reaches the header as such: the upload refuses it, the page cleans it, and the | |
| 6 | * header narrows it again. | |
| 7 | */ | |
| 8 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | |
| 9 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 10 | import { tmpdir } from "node:os"; | |
| 11 | import { join } from "node:path"; | |
| 12 | import { createApp, type App } from "../src/app.ts"; | |
| 13 | import { createRepo } from "../src/actions/repos.ts"; | |
| 14 | import { createApp as insertApp } from "../src/core/apps.ts"; | |
| 15 | import { createToken } from "../src/core/tokens.ts"; | |
| 16 | import { createUser } from "../src/core/users.ts"; | |
| 17 | import { cleanName, downloadHeaders } from "../src/drop/store.ts"; | |
| 18 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 19 | import { promote } from "./helpers/members.ts"; | |
| 20 | ||
| 21 | const HTML = "<!doctype html><script>alert(document.cookie)</script>"; | |
| 22 | const SVG = `<svg xmlns="http://www.w3.org/2000/svg"><script>alert(1)</script></svg>`; | |
| 23 | /** A GIF header followed by a page: an image to a decoder, a page to a sniffer. */ | |
| 24 | const POLYGLOT = `GIF89a/*\x01\x00\x01\x00\x00\x00\x00*/=1;<html><script>alert(2)</script></html>`; | |
| 25 | ||
| 26 | describe("a dropped file is served as a download", () => { | |
| 27 | let dataDir: string; | |
| 28 | let app: App; | |
| 29 | let servers: Servers; | |
| 30 | let token: string; | |
| 31 | ||
| 32 | const put = (name: string, body: string) => | |
| 33 | fetch(`${servers.url}/api/v1/repos/cy/paper/drop?name=${encodeURIComponent(name)}`, { method: "POST", headers: { connection: "close", authorization: `Bearer ${token}` }, body }); | |
| 34 | const get = (name: string) => | |
| 35 | fetch(`${servers.url}/api/v1/repos/cy/paper/drop/${encodeURIComponent(name)}`, { headers: { connection: "close", authorization: `Bearer ${token}` } }); | |
| 36 | const expectDownload = (res: Response, filename: string) => { | |
| 37 | expect(res.status).toBe(200); | |
| 38 | expect(res.headers.get("content-type")).toBe("application/octet-stream"); | |
| 39 | expect(res.headers.get("x-content-type-options")).toBe("nosniff"); | |
| 40 | const disposition = res.headers.get("content-disposition") ?? ""; | |
| 41 | expect(disposition.startsWith("attachment;")).toBe(true); | |
| 42 | expect(disposition).toContain(`filename="${filename}"`); | |
| 43 | expect(res.headers.get("cache-control")).toContain("no-store"); | |
| 44 | }; | |
| 45 | ||
| 46 | beforeAll(async () => { | |
| 47 | dataDir = await mkdtemp(join(tmpdir(), "forge-drop-serve-")); | |
| 48 | app = createApp({ dataDir, port: 0, host: "forge.test" }); | |
| 49 | servers = await serve(app); | |
| 50 | const make = async (username: string, adminIfFirst = false) => { | |
| 51 | const made = await createUser(app.db, { origin: "seed", trusted: true, username, email: `${username}@forge.local`, password: "a-good-passphrase", adminIfFirst }); | |
| 52 | if (!made.ok) throw new Error(made.error); | |
| 53 | return made.value; | |
| 54 | }; | |
| 55 | await make("ada", true); | |
| 56 | const cy = await make("cy"); | |
| 57 | promote(app.db, cy); | |
| 58 | const repo = await createRepo(app, cy, { name: "paper", visibility: "private", description: "" }); | |
| 59 | if (!repo.ok) throw new Error(String(repo.error)); | |
| 60 | const row = insertApp(app.db, { repoId: repo.value.id, slug: "paper", access: "private" }); | |
| 61 | if (!row.ok) throw new Error(row.error); | |
| 62 | token = createToken(app.db, cy.id, "me", "rw").token; | |
| 63 | }, 60_000); | |
| 64 | ||
| 65 | afterAll(async () => { | |
| 66 | servers?.stop(); | |
| 67 | app?.close(); | |
| 68 | if (dataDir) await rm(dataDir, { recursive: true, force: true }); | |
| 69 | }); | |
| 70 | ||
| 71 | test("html, svg, a polyglot and an image all download as octet-stream with nosniff", async () => { | |
| 72 | for (const [name, body] of [["page.html", HTML], ["logo.svg", SVG], ["shot.gif", POLYGLOT], ["trick.png", HTML]] as const) { | |
| 73 | expect((await put(name, body)).status).toBe(201); | |
| 74 | const res = await get(name); | |
| 75 | expectDownload(res, name); | |
| 76 | expect(await res.text()).toBe(body); | |
| 77 | } | |
| 78 | }); | |
| 79 | ||
| 80 | test("a name with quotes or newlines is refused by the upload; the page's cleaned name downloads intact", async () => { | |
| 81 | expect((await put('evil".html', HTML)).status).toBe(422); | |
| 82 | expect((await put("two\nlines.html", HTML)).status).toBe(422); | |
| 83 | const cleaned = cleanName('my "report"\r\nSet-Cookie: x=1.html'); | |
| 84 | expect(cleaned).toBe("my _report___Set-Cookie_ x_1.html"); | |
| 85 | expect((await put(cleaned, HTML)).status).toBe(201); | |
| 86 | const res = await get(cleaned); | |
| 87 | expectDownload(res, cleaned); | |
| 88 | expect(res.headers.get("set-cookie")).toBeNull(); | |
| 89 | expect(res.headers.get("content-disposition")).toContain("filename*=UTF-8''my%20_report___Set-Cookie_%20x_1.html"); | |
| 90 | }); | |
| 91 | ||
| 92 | test("the header narrows any name: no quote, newline or non-ASCII byte reaches it", () => { | |
| 93 | const h = downloadHeaders('a"b\r\ncé(1)+.svg'); | |
| 94 | expect(h["content-type"]).toBe("application/octet-stream"); | |
| 95 | expect(h["x-content-type-options"]).toBe("nosniff"); | |
| 96 | expect(h["content-disposition"]).toBe(`attachment; filename="a_b__c__(1)+.svg"; filename*=UTF-8''a_b__c__%281%29%2B.svg`); | |
| 97 | expect(downloadHeaders("")["content-disposition"]).toBe(`attachment; filename="file"; filename*=UTF-8''file`); | |
| 98 | }); | |
| 99 | }); |
new above
offline