Public: anyone can read this; the crew can change it.
A file page's raw route sends nosniff and a safe Content-Disposition for any kind the viewer does not draw #14512
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/14490-a-file-page-s-raw-route-sends-nosniff-an → main 3 commits fork
3 files
changed +48
−1
src/web/pages/browse.ts +1 −1
| @@ -538,7 +538,7 @@ export async function browseRaw(ctx: Ctx): Promise<Response> { | ||
| 538 | 538 | if (!isSafeTreePath(path)) return notFoundPage(ctx); |
| 539 | 539 | const blob = await scope.source.blob(ref, path); |
| 540 | 540 | if (!blob.ok || blob.value.kind === "too_large") return notFoundPage(ctx); |
| 541 | return rawResponse(blob.value); | |
| 541 | return rawResponse(blob.value, { name: path }); | |
| 542 | 542 | } |
| 543 | 543 | |
| 544 | 544 | // GET /origins/:id/r/:owner/:name/commits |
src/web/pages/repo.ts +8
| @@ -8,6 +8,7 @@ | ||
| 8 | 8 | * path segment and can never smuggle a path of its own. |
| 9 | 9 | */ |
| 10 | 10 | |
| 11 | import { downloadHeaders } from "../../drop/store.ts"; | |
| 11 | 12 | import { WAVE_CAP } from "../render/waveform.ts"; |
| 12 | 13 | import { repoLine } from "../describe.ts"; |
| 13 | 14 | import { recordLook } from "../../flow/valve_store.ts"; |
| @@ -1373,6 +1374,8 @@ export function rawResponse( | ||
| 1373 | 1374 | immutable?: boolean; |
| 1374 | 1375 | public?: boolean; |
| 1375 | 1376 | request?: Request; |
| 1377 | /** The file's path or name: an opaque stream is offered as a download under it. */ | |
| 1378 | name?: string; | |
| 1376 | 1379 | } = {}, |
| 1377 | 1380 | ): Response { |
| 1378 | 1381 | if (blob.kind === "too_large") return new Response("", { status: 404 }); |
| @@ -1394,6 +1397,10 @@ export function rawResponse( | ||
| 1394 | 1397 | : "application/octet-stream", |
| 1395 | 1398 | "x-content-type-options": "nosniff", |
| 1396 | 1399 | "content-security-policy": "default-src 'none'; sandbox", |
| 1400 | // Bytes served opaque are never shown in place: a download under a narrowed name. | |
| 1401 | ...(blob.kind === "text" | |
| 1402 | ? {} | |
| 1403 | : { "content-disposition": downloadHeaders((cache.name ?? "").split("/").pop() ?? "")["content-disposition"] ?? "" }), | |
| 1397 | 1404 | "cache-control": control, |
| 1398 | 1405 | ...(cache.immutable && etag && cache.public ? { vary: EDGE_VARY } : {}), |
| 1399 | 1406 | }; |
| @@ -2090,6 +2097,7 @@ export async function rawBlob(ctx: Ctx): Promise<Response> { | ||
| 2090 | 2097 | immutable: FULL_OID.test(ref), |
| 2091 | 2098 | public: mayShare(ctx.request, ctx.auth.user === undefined, repo, ref), |
| 2092 | 2099 | request: ctx.request, |
| 2100 | name: path, | |
| 2093 | 2101 | }); |
| 2094 | 2102 | } |
| 2095 | 2103 |
test/raw-file-headers.test.ts added +39
| @@ -0,0 +1,39 @@ | ||
| 1 | import { describe, expect, test } from "bun:test"; | |
| 2 | import { rawResponse } from "../src/web/pages/repo.ts"; | |
| 3 | import type { Blob } from "../src/view/source.ts"; | |
| 4 | ||
| 5 | const bytes = (s: string) => new TextEncoder().encode(s); | |
| 6 | const binary = (name: string) => { | |
| 7 | const blob: Blob = { kind: "binary", body: new Uint8Array([0, 1, 2]), size: 3 }; | |
| 8 | return rawResponse(blob, { name }); | |
| 9 | }; | |
| 10 | const text = (name: string, body: string) => { | |
| 11 | const blob: Blob = { kind: "text", body: bytes(body), size: body.length }; | |
| 12 | return rawResponse(blob, { name }); | |
| 13 | }; | |
| 14 | ||
| 15 | describe("raw file headers", () => { | |
| 16 | test("an opaque kind is an octet-stream attachment under nosniff", () => { | |
| 17 | const res = binary("dir/blob.bin"); | |
| 18 | expect(res.headers.get("content-type")).toBe("application/octet-stream"); | |
| 19 | expect(res.headers.get("x-content-type-options")).toBe("nosniff"); | |
| 20 | expect(res.headers.get("content-disposition")).toStartWith('attachment; filename="blob.bin"'); | |
| 21 | }); | |
| 22 | ||
| 23 | test("html and svg are plain text under a sandbox CSP, never a page", () => { | |
| 24 | for (const name of ["a.html", "b.svg"]) { | |
| 25 | const res = text(name, "<script>fetch('/')</script>"); | |
| 26 | expect(res.headers.get("content-type")).toBe("text/plain; charset=utf-8"); | |
| 27 | expect(res.headers.get("content-security-policy")).toContain("sandbox"); | |
| 28 | expect(res.headers.get("x-content-type-options")).toBe("nosniff"); | |
| 29 | } | |
| 30 | }); | |
| 31 | ||
| 32 | test("hostile names leave a well-formed header", () => { | |
| 33 | for (const name of ['a"b.bin', "x\r\nSet-Cookie: a=b.bin", "naïve ☃.bin", "\u0000.bin", "", "dir/"]) { | |
| 34 | const cd = binary(name).headers.get("content-disposition") ?? ""; | |
| 35 | expect(cd).toMatch(/^attachment; filename="[A-Za-z0-9._ +()-]+"; filename\*=UTF-8''[A-Za-z0-9._%-]+$/); | |
| 36 | expect(cd).not.toMatch(/[\r\n]/); | |
| 37 | } | |
| 38 | }); | |
| 39 | }); |
new above
offline