|
| 1 | +// Regression tests for #2693 — projector /subjects/ decode guard. |
| 2 | +// |
| 3 | +// The /subjects/<segment> branch passed its path segment through an |
| 4 | +// unguarded decodeURIComponent, so a malformed percent-encoding |
| 5 | +// (%E0%A4%A, %zz, a trailing %) threw URIError out of handleRequest |
| 6 | +// and the request died as a non-JSON 500. The fix guards just the |
| 7 | +// decode: the URIError becomes a JSON 400. These tests pin the |
| 8 | +// malformed forms alongside the well-formed controls (existing |
| 9 | +// subject -> 200, unknown subject -> 404 subject_not_found). |
| 10 | +// |
| 11 | +// Run: bun test test/subjects.test.ts |
| 12 | +import { describe, test, expect, beforeAll, afterAll } from "bun:test"; |
| 13 | +import { createClient, type Client } from "@libsql/client"; |
| 14 | +import { mkdtempSync, rmSync } from "node:fs"; |
| 15 | +import { tmpdir } from "node:os"; |
| 16 | +import { join } from "node:path"; |
| 17 | +import { migrate, upsertLink } from "../src/store"; |
| 18 | +import { handleRequest, type ServerEnv } from "../src/server"; |
| 19 | + |
| 20 | +const env: ServerEnv = {}; |
| 21 | +let db: Client; |
| 22 | +let dir: string; |
| 23 | + |
| 24 | +function get(path: string): Promise<Response> { |
| 25 | + return handleRequest(new Request(`http://127.0.0.1${path}`), db, env); |
| 26 | +} |
| 27 | + |
| 28 | +beforeAll(async () => { |
| 29 | + dir = mkdtempSync(join(tmpdir(), "projector-subjects-")); |
| 30 | + db = createClient({ url: `file:${join(dir, "subjects.db")}` }); |
| 31 | + await migrate(db); |
| 32 | + await upsertLink(db, { |
| 33 | + subject: "gh:owner/repo#1", |
| 34 | + ref: "owner/repo#1", |
| 35 | + relation: "relates", |
| 36 | + }); |
| 37 | +}); |
| 38 | + |
| 39 | +afterAll(() => { |
| 40 | + db.close(); |
| 41 | + rmSync(dir, { recursive: true, force: true }); |
| 42 | +}); |
| 43 | + |
| 44 | +describe("GET /subjects/<malformed percent-encoding>", () => { |
| 45 | + for (const bad of ["%E0%A4%A", "%zz", "trailing%", "%2"]) { |
| 46 | + test(`${bad} returns a JSON 400 and never throws`, async () => { |
| 47 | + const res = await get(`/subjects/${bad}`); |
| 48 | + expect(res.status).toBe(400); |
| 49 | + expect(await res.json()).toEqual({ |
| 50 | + error: "malformed percent-encoding in subject", |
| 51 | + }); |
| 52 | + }); |
| 53 | + } |
| 54 | +}); |
| 55 | + |
| 56 | +describe("GET /subjects/<well-formed> controls", () => { |
| 57 | + test("existing subject returns 200 with its view", async () => { |
| 58 | + const res = await get(`/subjects/${encodeURIComponent("gh:owner/repo#1")}`); |
| 59 | + expect(res.status).toBe(200); |
| 60 | + const view = (await res.json()) as { subject: string }; |
| 61 | + expect(view.subject).toBe("gh:owner/repo#1"); |
| 62 | + }); |
| 63 | + |
| 64 | + test("unknown subject returns 404 subject_not_found", async () => { |
| 65 | + const res = await get("/subjects/plain"); |
| 66 | + expect(res.status).toBe(404); |
| 67 | + expect(await res.json()).toEqual({ error: "subject_not_found" }); |
| 68 | + }); |
| 69 | + |
| 70 | + test("well-formed encoding of an unknown subject 404s cleanly", async () => { |
| 71 | + const res = await get("/subjects/valid%20name"); |
| 72 | + expect(res.status).toBe(404); |
| 73 | + expect(await res.json()).toEqual({ error: "subject_not_found" }); |
| 74 | + }); |
| 75 | +}); |