Skip to content

Commit 7617e91

Browse files
cropsggvincentkoc
andauthored
fix(gateway): reject blank session history keys (#85261)
Punchcard-Session: clear-orchard-lantern-bc Co-authored-by: Vincent Koc <[email protected]>
1 parent 05a03c6 commit 7617e91

2 files changed

Lines changed: 36 additions & 7 deletions

File tree

‎src/gateway/sessions-history-http.test.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,24 @@ describe("session history HTTP endpoints", () => {
763763
});
764764
});
765765

766+
test("claims invalid encoded session keys on a listening Gateway", async () => {
767+
await withGatewayHarness(async (harness) => {
768+
for (const encodedSessionKey of ["%20", "%zz"]) {
769+
const response = await fetch(
770+
`http://127.0.0.1:${harness.port}/sessions/${encodedSessionKey}/history`,
771+
);
772+
const body = await response.json();
773+
expect(response.status).toBe(400);
774+
expect(body).toEqual({
775+
error: {
776+
type: "invalid_request_error",
777+
message: "invalid session key",
778+
},
779+
});
780+
}
781+
});
782+
});
783+
766784
test("keeps standalone delivery-mirror rows in direct REST history", async () => {
767785
const { storePath } = await seedSession({ text: "visible history" });
768786
await appendTranscriptMessage({

‎src/gateway/sessions-history-http.ts‎

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,26 @@ const log = createSubsystemLogger("gateway/sessions-history-sse");
4747

4848
const MAX_SESSION_HISTORY_LIMIT = 1000;
4949

50-
function resolveSessionHistoryPath(req: IncomingMessage): string | null {
50+
// Route misses must remain distinct from matched-invalid keys so fallback
51+
// stages cannot claim malformed session-history requests.
52+
type SessionHistoryPathResolution =
53+
| { matched: false }
54+
| { error: "invalid-session-key"; matched: true }
55+
| { matched: true; sessionKey: string };
56+
57+
function resolveSessionHistoryPath(req: IncomingMessage): SessionHistoryPathResolution {
5158
const url = new URL(req.url ?? "/", "http://localhost");
5259
const match = url.pathname.match(/^\/sessions\/([^/]+)\/history$/);
5360
if (!match) {
54-
return null;
61+
return { matched: false };
5562
}
5663
try {
57-
return normalizeOptionalString(decodeURIComponent(match[1] ?? "")) ?? null;
64+
const sessionKey = normalizeOptionalString(decodeURIComponent(match[1] ?? ""));
65+
return sessionKey
66+
? { matched: true, sessionKey }
67+
: { error: "invalid-session-key", matched: true };
5868
} catch {
59-
return "";
69+
return { error: "invalid-session-key", matched: true };
6070
}
6171
}
6272

@@ -101,14 +111,15 @@ export async function handleSessionHistoryHttpRequest(
101111
rateLimiter?: AuthRateLimiter;
102112
},
103113
): Promise<boolean> {
104-
const sessionKey = resolveSessionHistoryPath(req);
105-
if (sessionKey === null) {
114+
const sessionKeyResolution = resolveSessionHistoryPath(req);
115+
if (!sessionKeyResolution.matched) {
106116
return false;
107117
}
108-
if (!sessionKey) {
118+
if ("error" in sessionKeyResolution) {
109119
sendInvalidRequest(res, "invalid session key");
110120
return true;
111121
}
122+
const { sessionKey } = sessionKeyResolution;
112123
if (req.method !== "GET") {
113124
sendMethodNotAllowed(res, "GET");
114125
return true;

0 commit comments

Comments
 (0)