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

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

scout/forge:scout/14490-a-file-page-s-raw-route-sends-nosniff-an → main 3 commits fork

  • Merged. scout/14490-a-file-page-s-raw-route-sends-nosniff-an was merged into main as c3bbb69.

Review

Sign in to review this change.
3 files changed +48 −1
src/web/pages/browse.ts +1 −1
@@ -538,7 +538,7 @@ export async function browseRaw(ctx: Ctx): Promise<Response> {
538538 if (!isSafeTreePath(path)) return notFoundPage(ctx);
539539 const blob = await scope.source.blob(ref, path);
540540 if (!blob.ok || blob.value.kind === "too_large") return notFoundPage(ctx);
541 return rawResponse(blob.value);
541 return rawResponse(blob.value, { name: path });
542542 }
543543
544544 // GET /origins/:id/r/:owner/:name/commits
src/web/pages/repo.ts +8
@@ -8,6 +8,7 @@
88 * path segment and can never smuggle a path of its own.
99 */
1010
11 import { downloadHeaders } from "../../drop/store.ts";
1112 import { WAVE_CAP } from "../render/waveform.ts";
1213 import { repoLine } from "../describe.ts";
1314 import { recordLook } from "../../flow/valve_store.ts";
@@ -1373,6 +1374,8 @@ export function rawResponse(
13731374 immutable?: boolean;
13741375 public?: boolean;
13751376 request?: Request;
1377 /** The file's path or name: an opaque stream is offered as a download under it. */
1378 name?: string;
13761379 } = {},
13771380 ): Response {
13781381 if (blob.kind === "too_large") return new Response("", { status: 404 });
@@ -1394,6 +1397,10 @@ export function rawResponse(
13941397 : "application/octet-stream",
13951398 "x-content-type-options": "nosniff",
13961399 "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"] ?? "" }),
13971404 "cache-control": control,
13981405 ...(cache.immutable && etag && cache.public ? { vary: EDGE_VARY } : {}),
13991406 };
@@ -2090,6 +2097,7 @@ export async function rawBlob(ctx: Ctx): Promise<Response> {
20902097 immutable: FULL_OID.test(ref),
20912098 public: mayShare(ctx.request, ctx.auth.user === undefined, repo, ref),
20922099 request: ctx.request,
2100 name: path,
20932101 });
20942102 }
20952103
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 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs