Skip to content

Commit bb1aa2e

Browse files
committed
fix(ready): reject encoded /readyz variants and assert the JSON 404 contract
Compare the DECODED pathname for /readyz routing so an encoded variant like /readyz%2F (which decodes to /readyz/) cannot bypass the exact-path rejection and reach the GUI fallback (serveGuiFile decodes the pathname and would serve index.html with 200). GET, POST, and OPTIONS on encoded variants all answer the deterministic JSON 404. Strengthen the OPTIONS regression test to assert the JSON 404 body (content-type, error.type, and the path-specific error.message) and add encoded-path coverage for GET, POST, and OPTIONS.
1 parent 640488c commit bb1aa2e

3 files changed

Lines changed: 36 additions & 7 deletions

File tree

‎src/server/index.ts‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -391,8 +391,6 @@ export function consumeStartupCacheInvalidationWrite(): boolean {
391391
return wrote;
392392
}
393393

394-
export function startServer(port?: number, deps: StartServerDeps = {}) {
395-
const localAttestationSecret = deps.localAttestationSecret ?? createLocalAttestationSecret();
396394
export function startServer(port?: number, deps: StartServerDeps = {}): Server<WsData> {
397395
const localAttestationSecret = deps.localAttestationSecret ?? createLocalAttestationSecret();
398396
const config = runAlibabaRegionStartupMigration(runOpenAiTierStartupMigration(loadConfig()));
@@ -550,11 +548,23 @@ export function startServer(port?: number, deps: StartServerDeps = {}): Server<W
550548
const url = new URL(req.url);
551549
markActivity(`${req.method} ${url.pathname}`);
552550

551+
// Readiness is exact-GET on the literal /readyz path. Compare the DECODED
552+
// pathname so an encoded variant like /readyz%2F (which decodes to
553+
// /readyz/) cannot bypass the exact-path rejection and reach the GUI
554+
// fallback (serveGuiFile decodes the pathname and would serve index.html
555+
// with 200). Malformed percent-sequences fall back to the raw pathname,
556+
// which still cannot match the exact literal below.
557+
let readyzPath: string | undefined;
558+
try {
559+
const decoded = decodeURIComponent(url.pathname);
560+
if (decoded === "/readyz" || decoded === "/readyz/") readyzPath = decoded;
561+
} catch { /* malformed encoding — not a readiness path */ }
562+
553563
if (req.method === "OPTIONS") {
554564
// /readyz is exact-GET only; OPTIONS (like POST and the trailing-slash
555565
// path) must answer the deterministic JSON 404, never the generic 204
556566
// preflight response that the SPA fallback would otherwise allow.
557-
if (url.pathname === "/readyz" || url.pathname === "/readyz/") {
567+
if (readyzPath !== undefined) {
558568
return withCors(formatErrorResponse(404, "not_found", `Unknown endpoint: ${req.method} ${url.pathname}`), req, config);
559569
}
560570
const managementPreflight = url.pathname.startsWith("/api/");
@@ -619,8 +629,8 @@ export function startServer(port?: number, deps: StartServerDeps = {}): Server<W
619629
// POST or "/readyz/" must NOT match (exact pathname + GET method): answer them
620630
// with a JSON 404 here so they can never be silently accepted by the GUI SPA
621631
// fallback (which would serve index.html with HTTP 200 once gui/dist exists).
622-
if (url.pathname === "/readyz" || url.pathname === "/readyz/") {
623-
if (url.pathname !== "/readyz" || req.method !== "GET") {
632+
if (readyzPath !== undefined) {
633+
if (readyzPath !== "/readyz" || req.method !== "GET") {
624634
return withCors(formatErrorResponse(404, "not_found", `Unknown endpoint: ${req.method} ${url.pathname}`), req, config);
625635
}
626636
// A draining proxy must never advertise ready: every data-plane branch

‎tests/cli-ready.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -624,8 +624,8 @@ describe("handleStart readinessGate wiring (source-level)", () => {
624624
const createMatch = cliSource.match(/const\s+readinessGate\s*=\s*createReadinessGate\(\)/);
625625
expect(createMatch, "handleStart must create readinessGate via createReadinessGate()").not.toBeNull();
626626

627-
const startMatch = cliSource.match(/startServer\s*\(\s*port\s*,\s*\{\s*readinessGate\s*\}\s*\)/);
628-
expect(startMatch, "startServer must be called with { readinessGate } in the retry path").not.toBeNull();
627+
const startMatch = cliSource.match(/startServer\s*\(\s*port\s*,\s*\{\s*[^}]*readinessGate[^}]*\}\s*\)/);
628+
expect(startMatch, "startServer must be called with readinessGate among its deps in the retry path").not.toBeNull();
629629

630630
const syncMatch = cliSource.match(
631631
/syncCodexOnStartIfEnabled\s*\(\s*port\s*,\s*config\s*,\s*undefined\s*,\s*readinessGate\s*\)/,

‎tests/server-live.test.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -973,8 +973,27 @@ describe("GET /readyz", () => {
973973
// preflight (the generic OPTIONS branch would otherwise answer 204).
974974
const optionsRes = await fetch(new URL("/readyz", base), { method: "OPTIONS" });
975975
expect(optionsRes.status).toBe(404);
976+
expect(optionsRes.headers.get("content-type")).toContain("application/json");
977+
expect(((await optionsRes.json()) as { error?: { type?: string; message?: string } }).error).toMatchObject({
978+
type: "not_found",
979+
message: "Unknown endpoint: OPTIONS /readyz",
980+
});
976981
const optionsSlashRes = await fetch(new URL("/readyz/", base), { method: "OPTIONS" });
977982
expect(optionsSlashRes.status).toBe(404);
983+
expect(optionsSlashRes.headers.get("content-type")).toContain("application/json");
984+
expect(((await optionsSlashRes.json()) as { error?: { type?: string; message?: string } }).error).toMatchObject({
985+
type: "not_found",
986+
message: "Unknown endpoint: OPTIONS /readyz/",
987+
});
988+
// Encoded variants (e.g. /readyz%2F, which decodes to /readyz/) must NOT
989+
// bypass the exact-path rejection and reach the GUI fallback (which would
990+
// serve index.html with 200). GET, POST, and OPTIONS all answer the JSON 404.
991+
const encodedGetRes = await fetch(`${base}/readyz%2F`);
992+
expect(encodedGetRes.status).toBe(404);
993+
const encodedPostRes = await fetch(`${base}/readyz%2F`, { method: "POST" });
994+
expect(encodedPostRes.status).toBe(404);
995+
const encodedOptionsRes = await fetch(`${base}/readyz%2F`, { method: "OPTIONS" });
996+
expect(encodedOptionsRes.status).toBe(404);
978997
} finally {
979998
await server.stop(true);
980999
}

0 commit comments

Comments
 (0)