Public: anyone can read this; the crew can change it.
A signed-in source archive is never marked shared-cacheable #15014
Check · Review · Land · Live done — Landed and live in production.
torque/forge:torque/14948-a-signed-in-source-archive-is-never-mark → main 2 commits fork
4 files
changed +141
−9
src/http/archive.ts +9 −3
| @@ -2,7 +2,9 @@ | ||
| 2 | 2 | * `GET /:owner/:name/archive/<ref>.tar.gz`: the source at a commit, for an |
| 3 | 3 | * agent that has HTTPS and no git. Anyone who may read the repository may |
| 4 | 4 | * take it; a private one answers 404 like every other page. At a full commit |
| 5 | * id the bytes never change and are cached as such. | |
| 5 | * id the bytes never change and are cached as such; only an anonymous | |
| 6 | * request's copy of a public repository may sit in a shared cache | |
| 7 | * (`mayShare`, as `rawBlob` does), a signed-in one stays the browser's. | |
| 6 | 8 | */ |
| 7 | 9 | |
| 8 | 10 | import { getRepo, canRead } from "../core/repos.ts"; |
| @@ -11,6 +13,8 @@ import { ARCHIVE_HTTP_BYTES, takeArchive } from "./archive_limit.ts"; | ||
| 11 | 13 | import { isSafeRef, isSafeTreePath } from "../git/guard.ts"; |
| 12 | 14 | import { repoDir } from "../git/repos.ts"; |
| 13 | 15 | import type { Handler } from "./context.ts"; |
| 16 | import { PINNED_PUBLIC, mayShare } from "./addressed.ts"; | |
| 17 | import { EDGE_VARY } from "./edge_cache.ts"; | |
| 14 | 18 | import { securityFloor } from "./respond.ts"; |
| 15 | 19 | |
| 16 | 20 | const FULL_OID = /^[0-9a-f]{40}$/; |
| @@ -43,14 +47,16 @@ export const sourceArchive: Handler = async (ctx) => { | ||
| 43 | 47 | headers: { "content-type": "text/plain; charset=utf-8" }, |
| 44 | 48 | }), |
| 45 | 49 | ); |
| 46 | const pinned = FULL_OID.test(ref) && repo.visibility === "public"; | |
| 50 | const pinned = FULL_OID.test(ref); | |
| 51 | const shared = pinned && mayShare(ctx.request, ctx.auth.user === undefined, repo, ref); | |
| 47 | 52 | return securityFloor( |
| 48 | 53 | new Response(made.value.bytes, { |
| 49 | 54 | headers: { |
| 50 | 55 | "content-type": "application/gzip", |
| 51 | 56 | "content-disposition": `attachment; filename="${repo.name}-${made.value.oid.slice(0, 12)}.tar.gz"`, |
| 52 | 57 | "x-forge-commit": made.value.oid, |
| 53 | "cache-control": pinned ? "public, max-age=31536000, immutable" : "no-cache", | |
| 58 | "cache-control": shared ? PINNED_PUBLIC : pinned ? "private, max-age=31536000, immutable" : "no-cache", | |
| 59 | ...(shared ? { vary: EDGE_VARY } : {}), | |
| 54 | 60 | etag: `"${made.value.oid}"`, |
| 55 | 61 | }, |
| 56 | 62 | }), |
src/http/respond.ts +13 −4
| @@ -418,18 +418,27 @@ export function securityFloor(response: Response): Response { | ||
| 418 | 418 | return response; |
| 419 | 419 | } |
| 420 | 420 | |
| 421 | /** | |
| 422 | * Paths whose answer depends on no identity: hashed assets, an app host's hashed toolbar scripts | |
| 423 | * and the fixed icons (`web/standards.ts`). | |
| 424 | */ | |
| 425 | const STATIC_ASSET_PATH = /^\/(?:assets\/[^/]+|__forge\/toolbar-(?:tour|comment)-[0-9a-f]{16}\.js|icons\/[^/]+\.png|favicon\.ico|apple-touch-icon(?:-precomposed)?\.png)$/; | |
| 426 | ||
| 421 | 427 | /** |
| 422 | 428 | * An answer to a request that carried a session cookie or an Authorization |
| 423 | 429 | * header is one person's: a shared cache must not keep it. It leaves |
| 424 | * `private`, with any `public` or `s-maxage` taken out. Content-addressed | |
| 425 | * assets (`immutable`) are the same bytes for everyone and keep their year, | |
| 426 | * though a browser sends its cookie for them too. | |
| 430 | * `private`, with any `public` or `s-maxage` taken out. Only the static | |
| 431 | * assets (`STATIC_ASSET_PATH`: hashed files under `/assets/`, the icons) are | |
| 432 | * the same bytes for everyone and keep their public year, though a browser | |
| 433 | * sends its cookie for them too. An `immutable` header alone is not enough: | |
| 434 | * a repository's archive or raw file at a commit is immutable too, yet what a | |
| 435 | * credential may read is that person's (#14948). | |
| 427 | 436 | */ |
| 428 | 437 | export function privateWhenSignedIn(request: Request, response: Response): Response { |
| 429 | 438 | if (!request.headers.has("cookie") && !request.headers.has("authorization")) return response; |
| 439 | if (STATIC_ASSET_PATH.test(new URL(request.url).pathname)) return response; | |
| 430 | 440 | try { |
| 431 | 441 | const control = response.headers.get("cache-control") ?? ""; |
| 432 | if (/\bimmutable\b/i.test(control)) return response; | |
| 433 | 442 | const kept = control |
| 434 | 443 | .split(",") |
| 435 | 444 | .map((part) => part.trim()) |
test/archive-cache.test.ts added +113
| @@ -0,0 +1,113 @@ | ||
| 1 | /** | |
| 2 | * A source archive at a full commit id is immutable, but only an anonymous | |
| 3 | * reader's copy of a public repository may sit in a shared cache (#14948): | |
| 4 | * the archive answers through `mayShare`, as a raw blob does, and | |
| 5 | * `privateWhenSignedIn` no longer spares a credentialed answer just because | |
| 6 | * it says `immutable`. Static assets keep their public year either way. | |
| 7 | */ | |
| 8 | ||
| 9 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | |
| 10 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 11 | import { tmpdir } from "node:os"; | |
| 12 | import { join } from "node:path"; | |
| 13 | import { commitToBranch, createRepo } from "../src/actions/repos.ts"; | |
| 14 | import { createApp, type App } from "../src/app.ts"; | |
| 15 | import { getUserByName } from "../src/core/users.ts"; | |
| 16 | import { createToken } from "../src/core/tokens.ts"; | |
| 17 | import { runGit } from "../src/git/env.ts"; | |
| 18 | import { repoDir } from "../src/git/repos.ts"; | |
| 19 | import { PINNED_PUBLIC } from "../src/http/addressed.ts"; | |
| 20 | import { forgetArchives } from "../src/http/archive_limit.ts"; | |
| 21 | import { EDGE_VARY } from "../src/http/edge_cache.ts"; | |
| 22 | import { privateWhenSignedIn } from "../src/http/respond.ts"; | |
| 23 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 24 | import { shellAsset } from "../src/web/asset.ts"; | |
| 25 | import { adminCookie } from "./helpers/admin.ts"; | |
| 26 | import { quietLogs } from "./helpers/globals.ts"; | |
| 27 | ||
| 28 | quietLogs(); | |
| 29 | ||
| 30 | describe("a source archive's cache headers", () => { | |
| 31 | let dataDir = ""; | |
| 32 | let app: App; | |
| 33 | let servers: Servers; | |
| 34 | let cookie = ""; | |
| 35 | let token = ""; | |
| 36 | let oid = ""; | |
| 37 | ||
| 38 | beforeAll(async () => { | |
| 39 | dataDir = await mkdtemp(join(tmpdir(), "forge-archive-cache-")); | |
| 40 | app = createApp({ dataDir, port: 0 }); | |
| 41 | servers = await serve(app); | |
| 42 | cookie = await adminCookie(app, { username: "ada", email: "[email protected]", password: "analytical-engine" }); | |
| 43 | const ada = getUserByName(app.db, "ada")!; | |
| 44 | token = createToken(app.db, ada.id, "t", "ro").token; | |
| 45 | const made = await createRepo(app, ada, { name: "lib", visibility: "public" }); | |
| 46 | if (!made.ok) throw new Error(String(made.error)); | |
| 47 | await commitToBranch(app, ada, made.value, { message: "init", files: [{ path: "README.md", content: "hi\n" }] }); | |
| 48 | const rev = await runGit(["rev-parse", "main"], { cwd: repoDir(app.reposRoot, "ada", "lib") }); | |
| 49 | if (!rev.ok) throw new Error("no main"); | |
| 50 | oid = rev.value.trim(); | |
| 51 | }); | |
| 52 | ||
| 53 | afterAll(async () => { | |
| 54 | await servers.stop(); | |
| 55 | await rm(dataDir, { recursive: true, force: true }); | |
| 56 | }); | |
| 57 | ||
| 58 | const archive = () => `${servers.url}/ada/lib/archive/${oid}.tar.gz`; | |
| 59 | ||
| 60 | test("anonymous, a public repository's archive at a full id is shared-cacheable and varies on identity", async () => { | |
| 61 | forgetArchives(); | |
| 62 | const res = await fetch(archive()); | |
| 63 | expect(res.status).toBe(200); | |
| 64 | expect(res.headers.get("cache-control")).toBe(PINNED_PUBLIC); | |
| 65 | expect(res.headers.get("vary")).toBe(EDGE_VARY); | |
| 66 | }); | |
| 67 | ||
| 68 | for (const [how, headers] of [ | |
| 69 | ["a session cookie", () => ({ cookie })], | |
| 70 | ["a bearer token", () => ({ authorization: `Bearer ${token}` })], | |
| 71 | ] as const) { | |
| 72 | test(`signed in with ${how}, the archive is private and never shared-cacheable`, async () => { | |
| 73 | forgetArchives(); | |
| 74 | const res = await fetch(archive(), { headers: headers() }); | |
| 75 | expect(res.status).toBe(200); | |
| 76 | const control = (res.headers.get("cache-control") ?? "").toLowerCase(); | |
| 77 | expect(control).not.toMatch(/\bpublic\b/); | |
| 78 | expect(control).not.toContain("s-maxage"); | |
| 79 | expect(control).toContain("private"); | |
| 80 | // The bytes at a commit still never change: the browser keeps them. | |
| 81 | expect(control).toContain("immutable"); | |
| 82 | }); | |
| 83 | } | |
| 84 | ||
| 85 | test("a branch archive revalidates for everyone", async () => { | |
| 86 | forgetArchives(); | |
| 87 | const res = await fetch(`${servers.url}/ada/lib/archive/main.tar.gz`); | |
| 88 | expect(res.status).toBe(200); | |
| 89 | expect(res.headers.get("cache-control")).toBe("no-cache"); | |
| 90 | }); | |
| 91 | ||
| 92 | test("signed in, a hashed static asset keeps its public year", async () => { | |
| 93 | const res = await fetch(servers.url + shellAsset().path, { headers: { cookie } }); | |
| 94 | expect(res.status).toBe(200); | |
| 95 | expect(res.headers.get("cache-control")).toBe("public, max-age=31536000, immutable"); | |
| 96 | }); | |
| 97 | ||
| 98 | test("privateWhenSignedIn strips public from an immutable answer off the static paths", () => { | |
| 99 | const signed = (path: string) => new Request(`http://x${path}`, { headers: { cookie: "a=b" } }); | |
| 100 | const pinned = () => new Response("", { headers: { "cache-control": "public, max-age=31536000, immutable" } }); | |
| 101 | expect(privateWhenSignedIn(signed("/a/b/archive/x.tar.gz"), pinned()).headers.get("cache-control")).toBe( | |
| 102 | "private, max-age=31536000, immutable", | |
| 103 | ); | |
| 104 | expect(privateWhenSignedIn(signed("/assets/forge-abc.js"), pinned()).headers.get("cache-control")).toBe( | |
| 105 | "public, max-age=31536000, immutable", | |
| 106 | ); | |
| 107 | expect(privateWhenSignedIn(signed("/favicon.ico"), pinned()).headers.get("cache-control")).toBe( | |
| 108 | "public, max-age=31536000, immutable", | |
| 109 | ); | |
| 110 | // A path that merely starts like an asset is not one. | |
| 111 | expect(privateWhenSignedIn(signed("/assets/x/../../a/b/raw"), pinned()).headers.get("cache-control")).toContain("private"); | |
| 112 | }); | |
| 113 | }); |
test/cache-private.test.ts +6 −2
| @@ -93,7 +93,7 @@ describe("cache headers", () => { | ||
| 93 | 93 | expect(res.headers.get("cache-control")).toBe("public, max-age=3600"); |
| 94 | 94 | }); |
| 95 | 95 | |
| 96 | test("a credential strips public and s-maxage from what a route wrote, and spares immutable assets", () => { | |
| 96 | test("a credential strips public and s-maxage from what a route wrote, and spares only static assets", () => { | |
| 97 | 97 | const sign = new Request("http://x/", { headers: { cookie: "a=b" } }); |
| 98 | 98 | const made = privateWhenSignedIn(sign, new Response("", { headers: { "cache-control": "public, s-maxage=60, max-age=5" } })); |
| 99 | 99 | expect(made.headers.get("cache-control")).toBe("private, max-age=5"); |
| @@ -101,7 +101,11 @@ describe("cache headers", () => { | ||
| 101 | 101 | expect(bare.headers.get("cache-control")).toBe("private"); |
| 102 | 102 | const anon = privateWhenSignedIn(new Request("http://x/"), new Response("", { headers: { "cache-control": "public, max-age=5" } })); |
| 103 | 103 | expect(anon.headers.get("cache-control")).toBe("public, max-age=5"); |
| 104 | const asset = privateWhenSignedIn(sign, new Response("", { headers: { "cache-control": "public, max-age=31536000, immutable" } })); | |
| 104 | const pinned = () => new Response("", { headers: { "cache-control": "public, max-age=31536000, immutable" } }); | |
| 105 | const asset = privateWhenSignedIn(new Request("http://x/assets/forge-abc.js", { headers: { cookie: "a=b" } }), pinned()); | |
| 105 | 106 | expect(asset.headers.get("cache-control")).toBe("public, max-age=31536000, immutable"); |
| 107 | // `immutable` alone spares nothing: a commit-addressed page is one person's when a credential | |
| 108 | // read it (#14948). | |
| 109 | expect(privateWhenSignedIn(sign, pinned()).headers.get("cache-control")).toBe("private, max-age=31536000, immutable"); | |
| 106 | 110 | }); |
| 107 | 111 | }); |
new above
offline