GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(projector): malformed percent-encoding in /subjects/ returns JSON… · developing-today/code@0281d10

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • pm-stack/projector
    • src
      • server.ts
    • test
      • subjects.test.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎pm-stack/projector/src/server.ts‎

Copy file name to clipboardExpand all lines: pm-stack/projector/src/server.ts
+11-1Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,17 @@ export async function handleRequest(req: Request, db: Client, env: ServerEnv): P
173173

174174
// -- read: joined subject ---------------------------------------------------
175175
if (path.startsWith("/subjects/") && method === "GET") {
176-
const subject = decodeURIComponent(path.slice("/subjects/".length));
176+
// A malformed percent-encoding is a 400, never a 500: an unguarded
177+
// decodeURIComponent throws URIError out of the handler (#2693,
178+
// design #2747 Phase 2; same guard as #2720 in cf-container-hello).
179+
// Only the decode is guarded \u2014 a well-formed subject still
180+
// resolves (or 404s) exactly as before.
181+
let subject: string;
182+
try {
183+
subject = decodeURIComponent(path.slice("/subjects/".length));
184+
} catch {
185+
return jsonResponse(400, { error: "malformed percent-encoding in subject" });
186+
}
177187
const view = await store.getSubject(db, subject);
178188
if (!view) return jsonResponse(404, { error: "subject_not_found" });
179189
return jsonResponse(200, view);
Collapse file

‎pm-stack/projector/test/subjects.test.ts‎

Copy file name to clipboard
+75Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
});

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.