Public: anyone can read this; the crew can change it.
A path with a doubled slash is one page, not two: //explore answers 308 to /explore, not a second 200 copy #14380
Check · Review · Land · Live done — Landed and live in production.
torque/forge:torque/14374-a-path-with-a-doubled-slash-is-one-page → main 2 commits fork
2 files
changed +58
−0
src/http/server.ts +8
| @@ -3256,6 +3256,14 @@ async function answerGet( | ||
| 3256 | 3256 | request: Request, |
| 3257 | 3257 | ): Promise<Response> { |
| 3258 | 3258 | const url = publicUrl(request, app.config.url); |
| 3259 | // A doubled slash is the same page, not a second copy (#14374): GET and HEAD | |
| 3260 | // answer 308 to the path with each run of slashes collapsed, query kept. The | |
| 3261 | // Location is a path, never an origin, and starts with exactly one slash, so | |
| 3262 | // `//evil.example` can only land on `/evil.example` here. | |
| 3263 | if ((request.method === "GET" || request.method === "HEAD") && url.pathname.includes("//")) { | |
| 3264 | const collapsed = url.pathname.replace(/\/{2,}/g, "/"); | |
| 3265 | return new Response(null, { status: 308, headers: { location: `${collapsed}${url.search}` } }); | |
| 3266 | } | |
| 3259 | 3267 | if (url.pathname === "/healthz") { |
| 3260 | 3268 | noteUsage(request, { pattern: "/healthz", role: "anon" }); |
| 3261 | 3269 | const dbStart = performance.now(); |
test/doubled-slash.test.ts added +50
| @@ -0,0 +1,50 @@ | ||
| 1 | /** A doubled slash is one page: GET/HEAD answer 308 to the collapsed path (#14374). */ | |
| 2 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 3 | import { mkdtempSync, rmSync } from "node:fs"; | |
| 4 | import { tmpdir } from "node:os"; | |
| 5 | import { join } from "node:path"; | |
| 6 | import { createApp, type App } from "../src/app.ts"; | |
| 7 | import { forgeHttp, type ForgeHttp } from "../src/http/server.ts"; | |
| 8 | import { quietLogs } from "./helpers/globals.ts"; | |
| 9 | ||
| 10 | quietLogs(); | |
| 11 | ||
| 12 | let root = ""; | |
| 13 | let app: App; | |
| 14 | let http: ForgeHttp; | |
| 15 | ||
| 16 | beforeAll(async () => { | |
| 17 | root = mkdtempSync(join(tmpdir(), "forge-doubled-slash-")); | |
| 18 | app = createApp({ dataDir: root, port: 0, forgeRepo: "alice/forge" }); | |
| 19 | http = forgeHttp(app); | |
| 20 | }); | |
| 21 | afterAll(() => { | |
| 22 | http.close(); | |
| 23 | app.close(); | |
| 24 | rmSync(root, { recursive: true, force: true }); | |
| 25 | }); | |
| 26 | ||
| 27 | const cases: [string, string, string][] = [ | |
| 28 | ["//explore", "GET", "/explore"], | |
| 29 | ["///pricing?x=1", "GET", "/pricing?x=1"], | |
| 30 | ["//evil.example/a", "GET", "/evil.example/a"], | |
| 31 | ["//evil.example", "HEAD", "/evil.example"], | |
| 32 | ["//", "GET", "/"], | |
| 33 | ["/a//b///c", "GET", "/a/b/c"], | |
| 34 | ]; | |
| 35 | ||
| 36 | for (const [path, method, want] of cases) { | |
| 37 | test(`${method} ${path} answers 308 to ${want}`, async () => { | |
| 38 | const res = await http.handle(new Request(`http://localhost${path}`, { method })); | |
| 39 | expect(res.status).toBe(308); | |
| 40 | const location = res.headers.get("location") ?? ""; | |
| 41 | expect(location).toBe(want); | |
| 42 | expect(location.startsWith("/")).toBe(true); | |
| 43 | expect(location.startsWith("//")).toBe(false); | |
| 44 | }); | |
| 45 | } | |
| 46 | ||
| 47 | test("a POST to a doubled-slash path is not redirected", async () => { | |
| 48 | const res = await http.handle(new Request("http://localhost//explore", { method: "POST" })); | |
| 49 | expect(res.status).toBe(404); | |
| 50 | }); |
new above
offline