-
-
Notifications
You must be signed in to change notification settings - Fork 181
feat(codex): implement session isolation for concurrent Codex requests (#430) #434
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
148 changes: 148 additions & 0 deletions
148
src/app/v1/_lib/codex/__tests__/session-extractor.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| import { describe, expect, test } from "vitest"; | ||
| import { extractCodexSessionId, isCodexClient } from "../session-extractor"; | ||
|
|
||
| describe("Codex session extractor", () => { | ||
| test("extracts from header session_id", () => { | ||
| const headerSessionId = "sess_123456789012345678901"; | ||
| const result = extractCodexSessionId( | ||
| new Headers({ session_id: headerSessionId }), | ||
| { | ||
| metadata: { session_id: "sess_aaaaaaaaaaaaaaaaaaaaa" }, | ||
| previous_response_id: "resp_123456789012345678901", | ||
| }, | ||
| "codex_cli_rs/0.50.0 (Mac OS 26.0.1; arm64)" | ||
| ); | ||
|
|
||
| expect(result.sessionId).toBe(headerSessionId); | ||
| expect(result.source).toBe("header_session_id"); | ||
| }); | ||
|
|
||
| test("extracts from header x-session-id", () => { | ||
| const headerSessionId = "sess_123456789012345678902"; | ||
| const result = extractCodexSessionId( | ||
| new Headers({ "x-session-id": headerSessionId }), | ||
| { | ||
| metadata: { session_id: "sess_aaaaaaaaaaaaaaaaaaaaa" }, | ||
| previous_response_id: "resp_123456789012345678901", | ||
| }, | ||
| "codex_cli_rs/0.50.0 (Mac OS 26.0.1; arm64)" | ||
| ); | ||
|
|
||
| expect(result.sessionId).toBe(headerSessionId); | ||
| expect(result.source).toBe("header_x_session_id"); | ||
| }); | ||
|
|
||
| test("extracts from body metadata.session_id", () => { | ||
| const bodySessionId = "sess_123456789012345678903"; | ||
| const result = extractCodexSessionId( | ||
| new Headers(), | ||
| { metadata: { session_id: bodySessionId } }, | ||
| "codex_cli_rs/0.50.0 (Mac OS 26.0.1; arm64)" | ||
| ); | ||
|
|
||
| expect(result.sessionId).toBe(bodySessionId); | ||
| expect(result.source).toBe("body_metadata_session_id"); | ||
| }); | ||
|
|
||
| test("falls back to previous_response_id", () => { | ||
| const previousResponseId = "resp_123456789012345678901"; | ||
| const result = extractCodexSessionId( | ||
| new Headers(), | ||
| { previous_response_id: previousResponseId }, | ||
| "codex_cli_rs/0.50.0 (Mac OS 26.0.1; arm64)" | ||
| ); | ||
|
|
||
| expect(result.sessionId).toBe(`codex_prev_${previousResponseId}`); | ||
| expect(result.source).toBe("body_previous_response_id"); | ||
| }); | ||
|
|
||
| test("rejects previous_response_id that would exceed 256 after prefix", () => { | ||
| const longId = "a".repeat(250); // 250 + 11 (prefix) = 261 > 256 | ||
| const result = extractCodexSessionId(new Headers(), { previous_response_id: longId }, null); | ||
| expect(result.sessionId).toBe(null); | ||
| expect(result.source).toBe(null); | ||
| }); | ||
|
|
||
| test("respects extraction priority", () => { | ||
| const sessionIdFromHeader = "sess_123456789012345678904"; | ||
| const xSessionIdFromHeader = "sess_123456789012345678905"; | ||
| const sessionIdFromBody = "sess_123456789012345678906"; | ||
| const previousResponseId = "resp_123456789012345678901"; | ||
|
|
||
| const result = extractCodexSessionId( | ||
| new Headers({ | ||
| session_id: sessionIdFromHeader, | ||
| "x-session-id": xSessionIdFromHeader, | ||
| }), | ||
| { | ||
| metadata: { session_id: sessionIdFromBody }, | ||
| previous_response_id: previousResponseId, | ||
| }, | ||
| "codex_cli_rs/0.50.0 (Mac OS 26.0.1; arm64)" | ||
| ); | ||
|
|
||
| expect(result.sessionId).toBe(sessionIdFromHeader); | ||
| expect(result.source).toBe("header_session_id"); | ||
| }); | ||
|
|
||
| test("detects Codex client User-Agent", () => { | ||
| expect(isCodexClient("codex_cli_rs/0.50.0 (Mac OS 26.0.1; arm64)")).toBe(true); | ||
| expect(isCodexClient("codex_vscode/0.35.0 (Windows 10.0.26100; x86_64)")).toBe(true); | ||
| expect(isCodexClient("Mozilla/5.0")).toBe(false); | ||
| expect(isCodexClient(null)).toBe(false); | ||
| }); | ||
|
|
||
| test("rejects session_id shorter than 21 characters", () => { | ||
| const result = extractCodexSessionId( | ||
| new Headers({ session_id: "short_id_12345" }), // 14 chars | ||
| {}, | ||
| null | ||
| ); | ||
| expect(result.sessionId).toBe(null); | ||
| expect(result.source).toBe(null); | ||
| }); | ||
|
|
||
| test("accepts session_id with exactly 21 characters (minimum)", () => { | ||
| const minId = "a".repeat(21); | ||
| const result = extractCodexSessionId(new Headers({ session_id: minId }), {}, null); | ||
| expect(result.sessionId).toBe(minId); | ||
| expect(result.source).toBe("header_session_id"); | ||
| }); | ||
|
|
||
| test("accepts session_id with exactly 256 characters (maximum)", () => { | ||
| const maxId = "a".repeat(256); | ||
| const result = extractCodexSessionId(new Headers({ session_id: maxId }), {}, null); | ||
| expect(result.sessionId).toBe(maxId); | ||
| expect(result.source).toBe("header_session_id"); | ||
| }); | ||
|
|
||
| test("rejects session_id longer than 256 characters", () => { | ||
| const longId = "a".repeat(300); | ||
| const result = extractCodexSessionId(new Headers({ session_id: longId }), {}, null); | ||
| expect(result.sessionId).toBe(null); | ||
| expect(result.source).toBe(null); | ||
| }); | ||
|
|
||
| test("rejects session_id with invalid characters", () => { | ||
| // Test with body metadata to avoid Headers normalization | ||
| const result = extractCodexSessionId( | ||
| new Headers(), | ||
| { metadata: { session_id: "sess_123456789@#$%^&*()!" } }, | ||
| null | ||
| ); | ||
| expect(result.sessionId).toBe(null); | ||
| }); | ||
|
|
||
| test("accepts session_id with allowed special characters", () => { | ||
| const validId = "sess-123_456.789:abc012345"; | ||
| const result = extractCodexSessionId(new Headers({ session_id: validId }), {}, null); | ||
| expect(result.sessionId).toBe(validId); | ||
| }); | ||
|
|
||
| test("returns null when no valid session_id found", () => { | ||
| const result = extractCodexSessionId(new Headers(), {}, "codex_cli_rs/0.50.0"); | ||
| expect(result.sessionId).toBe(null); | ||
| expect(result.source).toBe(null); | ||
| expect(result.isCodexClient).toBe(true); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| import "server-only"; | ||
|
|
||
| export type CodexSessionIdSource = | ||
| | "header_session_id" | ||
| | "header_x_session_id" | ||
| | "body_metadata_session_id" | ||
| | "body_previous_response_id" | ||
| | null; | ||
|
|
||
| export interface CodexSessionExtractionResult { | ||
| sessionId: string | null; | ||
| source: CodexSessionIdSource; | ||
| isCodexClient: boolean; | ||
| } | ||
|
|
||
| // Session ID validation constants | ||
| const CODEX_SESSION_ID_MIN_LENGTH = 21; // Codex session_id typically > 20 chars (UUID-like) | ||
| const CODEX_SESSION_ID_MAX_LENGTH = 256; // Prevent Redis key bloat from malicious input | ||
| const SESSION_ID_PATTERN = /^[\w\-.:]+$/; // Alphanumeric, dash, dot, colon only | ||
|
|
||
| // Codex CLI User-Agent pattern (pre-compiled for performance) | ||
| const CODEX_CLI_PATTERN = /^(codex_vscode|codex_cli_rs)\/[\d.]+/i; | ||
|
|
||
| export function normalizeCodexSessionId(value: unknown): string | null { | ||
| if (typeof value !== "string") return null; | ||
|
|
||
| const trimmed = value.trim(); | ||
| if (!trimmed) return null; | ||
|
|
||
| if (trimmed.length < CODEX_SESSION_ID_MIN_LENGTH) return null; | ||
| if (trimmed.length > CODEX_SESSION_ID_MAX_LENGTH) return null; | ||
| if (!SESSION_ID_PATTERN.test(trimmed)) return null; | ||
|
|
||
| return trimmed; | ||
| } | ||
|
|
||
| function parseMetadata(requestBody: Record<string, unknown>): Record<string, unknown> | null { | ||
| const metadata = requestBody.metadata; | ||
| if (!metadata || typeof metadata !== "object" || Array.isArray(metadata)) return null; | ||
| return metadata as Record<string, unknown>; | ||
| } | ||
|
|
||
| /** | ||
| * Detect official Codex CLI clients by User-Agent. | ||
| * | ||
| * Examples: | ||
| * - codex_vscode/0.35.0 (...) | ||
| * - codex_cli_rs/0.50.0 (...) | ||
| */ | ||
| export function isCodexClient(userAgent: string | null): boolean { | ||
| if (!userAgent) return false; | ||
| return CODEX_CLI_PATTERN.test(userAgent); | ||
| } | ||
|
|
||
| /** | ||
| * Extract Codex session id from headers/body with priority: | ||
| * 1) headers["session_id"] | ||
| * 2) headers["x-session-id"] | ||
| * 3) body.metadata.session_id | ||
| * 4) body.previous_response_id (fallback, prefixed with "codex_prev_") | ||
| * | ||
| * Only accept session ids with length > 20. | ||
| */ | ||
| export function extractCodexSessionId( | ||
| headers: Headers, | ||
| requestBody: Record<string, unknown>, | ||
| userAgent: string | null | ||
| ): CodexSessionExtractionResult { | ||
| const officialClient = isCodexClient(userAgent); | ||
|
|
||
| const headerSessionId = normalizeCodexSessionId(headers.get("session_id")); | ||
| if (headerSessionId) { | ||
| return { | ||
| sessionId: headerSessionId, | ||
| source: "header_session_id", | ||
| isCodexClient: officialClient, | ||
| }; | ||
| } | ||
|
|
||
| const headerXSessionId = normalizeCodexSessionId(headers.get("x-session-id")); | ||
| if (headerXSessionId) { | ||
| return { | ||
| sessionId: headerXSessionId, | ||
| source: "header_x_session_id", | ||
| isCodexClient: officialClient, | ||
| }; | ||
| } | ||
|
|
||
| const metadata = parseMetadata(requestBody); | ||
| const bodyMetadataSessionId = metadata ? normalizeCodexSessionId(metadata.session_id) : null; | ||
| if (bodyMetadataSessionId) { | ||
| return { | ||
| sessionId: bodyMetadataSessionId, | ||
| source: "body_metadata_session_id", | ||
| isCodexClient: officialClient, | ||
| }; | ||
| } | ||
|
|
||
| const prevResponseId = normalizeCodexSessionId(requestBody.previous_response_id); | ||
| if (prevResponseId) { | ||
| const sessionId = `codex_prev_${prevResponseId}`; | ||
| if (sessionId.length <= CODEX_SESSION_ID_MAX_LENGTH) { | ||
| return { | ||
| sessionId, | ||
| source: "body_previous_response_id", | ||
| isCodexClient: officialClient, | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| return { sessionId: null, source: null, isCodexClient: officialClient }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Critical [SECURITY-VULNERABILITY] Codex session_id validation can be bypassed via fallback
Why this is a problem: At
src/lib/session-manager.ts:92,extractCodexSessionId(...)rejects too-short/too-long/invalid-character IDs, but this block falls through whenresult.sessionIdis null and the function can later returnmetadata.user_id/metadata.session_idwithout those validations. A craftedmetadata.session_id(e.g. extremely long or containing disallowed characters) would then be used as a Redis key component viagetOrCreateSessionId(...), defeating the security hardening intent.Suggested fix: