OpenAI's Codex Security CLI and TypeScript SDK for finding, validating, and fixing security vulnerabilities. npm: https://www.npmjs.com/package/@openai/codex-security
TypeScript ai-securityapplication-securityclicode-scanningcodexcodex-securitycybersecuritydevsecopsnodejsnpmopenaisdksecuritytypescriptvulnerability-scanning
fix(sdk): read compressed Codex session logs (#1428)
12cdd1fd104df3
diff --git a/sdk/typescript/README.md b/sdk/typescript/README.mdindex c14e25f77..a2b3dd67d 100644--- a/sdk/typescript/README.md+++ b/sdk/typescript/README.md@@ -33,6 +33,12 @@ Supported runtimes:findings. Python 3.10 also needs `tomli`. The findings server (`serve`) usesNode’s built-in SQLite and does not require Python.+Reading compressed Codex session logs (`.jsonl.zst`) requires Node.js 22.15.0++within 22.x, or Node.js 24.x or 26.x. On Node.js 22.13–22.14, compressed sessions+are unavailable when reading saved activity or attaching scan logs to feedback, and+scans whose original session is compressed cannot resume. Plain `.jsonl` logs+work on all supported runtimes.+## AuthenticationSign in with ChatGPT:diff --git a/sdk/typescript/docs/cli.md b/sdk/typescript/docs/cli.mdindex d0b8b5a6c..e2a53b313 100644--- a/sdk/typescript/docs/cli.md+++ b/sdk/typescript/docs/cli.md@@ -1022,6 +1022,12 @@ History lives in `$CODEX_SECURITY_STATE_DIR/workbench.sqlite3`, orwritable, and outside the target repository. Session logs may contain sensitivedata even though scan recipes do not store credentials.+Codex may compress saved session logs to `.jsonl.zst`. Reading those logs requires+Node.js 22.15.0+ within 22.x, or Node.js 24.x or 26.x. On Node.js 22.13–22.14,+compressed sessions are unavailable to `scans logs`, feedback attachments, and+`scans resume`. Upgrade Node.js to read or resume these sessions. Plain `.jsonl`+logs work on all supported runtimes.+### Resuming an interrupted Deep Scan```bashdiff --git a/sdk/typescript/src/cost.ts b/sdk/typescript/src/cost.tsindex 4aa0c12c3..c7cb785bd 100644--- a/sdk/typescript/src/cost.ts+++ b/sdk/typescript/src/cost.ts@@ -389,7 +389,10 @@ export class ScanCostTracker {}}-export async function* sessionFiles(directory: string): AsyncGenerator<string> {+export async function* sessionFiles(+ directory: string,+ compressed = false,+): AsyncGenerator<string> {let entries;try {entries = await readdir(directory, { withFileTypes: true });@@ -400,8 +403,12 @@ export async function* sessionFiles(directory: string): AsyncGenerator<string> {for (const entry of entries) {const path = join(directory, entry.name);if (entry.isDirectory()) {- yield* sessionFiles(path);- } else if (entry.isFile() && entry.name.endsWith(".jsonl")) {+ yield* sessionFiles(path, compressed);+ } else if (+ entry.isFile() &&+ (entry.name.endsWith(".jsonl") ||+ (compressed && entry.name.endsWith(".jsonl.zst")))+ ) {yield path;}}diff --git a/sdk/typescript/src/scan-logs.ts b/sdk/typescript/src/scan-logs.tsindex c3c805ceb..26cda210d 100644--- a/sdk/typescript/src/scan-logs.ts+++ b/sdk/typescript/src/scan-logs.ts@@ -1,7 +1,9 @@import { createReadStream } from "node:fs";import { basename, dirname, join, relative } from "node:path";import { createInterface } from "node:readline";+import { pipeline } from "node:stream";import { isDeepStrictEqual } from "node:util";+import zlib from "node:zlib";import { isRecord } from "./record.js";import { sessionFiles } from "./cost.js";import { CodexSecurityError } from "./errors.js";@@ -64,6 +66,10 @@ export function readSavedScanLogs(});}+// Codex can compress cold rollouts to `.jsonl.zst`. Node 22.13 and 22.14 lack+// zstd, so they still read plain rollouts only.+const readsCompressedSessions = typeof zlib.createZstdDecompress === "function";+interface SessionLog {threadId: string;parentThreadId: string | null;@@ -76,7 +82,10 @@ async function* scanSessions(codexHome: string,directory = "sessions",): AsyncGenerator<SessionLog> {- for await (const path of sessionFiles(join(codexHome, directory))) {+ for await (const path of sessionFiles(+ join(codexHome, directory),+ readsCompressedSessions,+ )) {for await (const first of sessionEvents(path)) {if (first["type"] !== "session_meta" || !isRecord(first["payload"])) {break;@@ -265,7 +274,10 @@ async function extendsSessionLog(async function* sessionEvents(path: string,): AsyncGenerator<Record<string, unknown>> {- const stream = createReadStream(path, { encoding: "utf8" });+ const file = createReadStream(path);+ const stream = path.endsWith(".zst")+ ? pipeline(file, zlib.createZstdDecompress(), () => {})+ : file;const lines = createInterface({ input: stream, crlfDelay: Infinity });try {for await (const line of lines) {diff --git a/sdk/typescript/tests-ts/scan-logs-node.test.ts b/sdk/typescript/tests-ts/scan-logs-node.test.tsnew file mode 100644index 000000000..7cc91ce75--- /dev/null+++ b/sdk/typescript/tests-ts/scan-logs-node.test.ts@@ -0,0 +1,95 @@+import { execFile } from "node:child_process";+import { mkdir, writeFile } from "node:fs/promises";+import { join } from "node:path";+import { fileURLToPath, pathToFileURL } from "node:url";+import { promisify } from "node:util";+import { zstdCompressSync } from "node:zlib";+import { afterEach, expect, test } from "bun:test";+import { createApiTestFixtures } from "./support/temporary-directories.js";++const { temporaryDirectory, cleanup } = createApiTestFixtures(+ "codex-security-scan-logs-node-",+);++afterEach(cleanup);++test.each(["native", "without zstd"])(+ "reads saved logs with Node's %s capabilities",+ async (capabilities) => {+ const root = await temporaryDirectory();+ const home = join(root, "home");+ const sessions = join(home, "sessions");+ await mkdir(sessions, { recursive: true });+ const parent = {+ type: "session_meta",+ payload: { id: "parent", cwd: root },+ };+ const worker = {+ type: "session_meta",+ payload: { id: "worker", parent_thread_id: "parent", cwd: root },+ };+ await writeFile(+ join(sessions, "rollout-parent.jsonl"),+ `${JSON.stringify(parent)}\n`,+ );+ await writeFile(+ join(sessions, "rollout-worker.jsonl.zst"),+ zstdCompressSync(`${JSON.stringify(worker)}\n`),+ );++ const built = await Bun.build({+ entrypoints: [+ fileURLToPath(new URL("../src/scan-logs.ts", import.meta.url)),+ ],+ target: "node",+ format: "esm",+ });+ expect(built.success).toBe(true);+ const module = join(root, "scan-logs.mjs");+ await writeFile(module, await built.outputs[0]!.text());+ const { stdout } = await promisify(execFile)(+ "node",+ [+ "--input-type=module",+ "--eval",+ `+ import zlib from "node:zlib";+ import { syncBuiltinESMExports } from "node:module";+ if (${JSON.stringify(capabilities)} === "without zstd") {+ delete zlib.createZstdDecompress;+ syncBuiltinESMExports();+ }+ const { findScanSession, readScanLogs } = await import(${JSON.stringify(pathToFileURL(module).href)});+ const home = ${JSON.stringify(home)};+ console.log(JSON.stringify({+ compressed: typeof zlib.createZstdDecompress === "function",+ parent: await findScanSession(home, "parent"),+ worker: await findScanSession(home, "worker"),+ logs: await readScanLogs({ scanId: "scan-1", threadId: "parent", codexHome: home }),+ }));+ `,+ ],+ { encoding: "utf8" },+ );+ const result = JSON.parse(stdout);+ if (capabilities === "without zstd") {+ expect(result.compressed).toBe(false);+ }+ expect(result.parent).toMatchObject({+ threadId: "parent",+ workingDirectory: root,+ });+ expect(result.worker).toEqual(+ result.compressed+ ? expect.objectContaining({+ threadId: "worker",+ workingDirectory: root,+ })+ : null,+ );+ expect(result.logs.events).toEqual([+ { threadId: "parent", event: parent },+ ...(result.compressed ? [{ threadId: "worker", event: worker }] : []),+ ]);+ },+);diff --git a/sdk/typescript/tests-ts/scan-logs.test.ts b/sdk/typescript/tests-ts/scan-logs.test.tsindex e95366f43..ac076bbc7 100644--- a/sdk/typescript/tests-ts/scan-logs.test.ts+++ b/sdk/typescript/tests-ts/scan-logs.test.ts@@ -2,8 +2,13 @@ import { writeJsonLines } from "./support/json.js";import { mkdir, readFile, rename, writeFile } from "node:fs/promises";import { unlinkSync } from "node:fs";import { join } from "node:path";+import { zstdCompressSync } from "node:zlib";import { afterEach, describe, expect, spyOn, test } from "bun:test";-import { readSavedScanLogs, readScanLogs } from "../src/scan-logs.js";+import {+ findScanSession,+ readSavedScanLogs,+ readScanLogs,+} from "../src/scan-logs.js";import { createApiTestFixtures } from "./support/temporary-directories.js";const { temporaryDirectory: temporaryHome, cleanup } = createApiTestFixtures(@@ -59,19 +64,24 @@ function commandEvent(command: string, id: string, timestamp?: string) {describe("saved scan logs", () => {test.each([- ["prefix first", [0], [0, 1, 1, 2], 1, false],- ["complete first", [0, 1, 1, 2], [0], 0, false],- ["identical copies", [0, 1, 1, 2], [0, 1, 1, 2], 0, false],- ["longer divergent copy", [0, 2], [0, 1, 1, 2], 0, false],- ["shorter divergent copy", [0, 1, 1, 2], [0, 2], 0, false],- ["equal-length divergent copy", [0, 1], [0, 2], 0, false],- ["complete archived copy", [0], [0, 1, 1, 2], 1, true],- ["prefix archived copy", [0, 1, 1, 2], [0], 0, true],- ["identical archived copy", [0, 1, 1, 2], [0, 1, 1, 2], 0, true],- ["divergent archived copy", [0, 2], [0, 1, 1, 2], 0, true],+ ["prefix first", [0], [0, 1, 1, 2], 1, false, false],+ ["complete first", [0, 1, 1, 2], [0], 0, false, false],+ ["identical copies", [0, 1, 1, 2], [0, 1, 1, 2], 0, false, false],+ ["longer divergent copy", [0, 2], [0, 1, 1, 2], 0, false, false],+ ["shorter divergent copy", [0, 1, 1, 2], [0, 2], 0, false, false],+ ["equal-length divergent copy", [0, 1], [0, 2], 0, false, false],+ ["complete archived copy", [0], [0, 1, 1, 2], 1, true, false],+ ["prefix archived copy", [0, 1, 1, 2], [0], 0, true, false],+ ["identical archived copy", [0, 1, 1, 2], [0, 1, 1, 2], 0, true, false],+ ["divergent archived copy", [0, 2], [0, 1, 1, 2], 0, true, false],+ ["complete compressed copy", [0], [0, 1, 1, 2], 1, false, true],+ ["prefix compressed copy", [0, 1, 1, 2], [0], 0, false, true],+ ["identical compressed copy", [0, 1, 1, 2], [0, 1, 1, 2], 0, false, true],+ ["divergent compressed copy", [0, 2], [0, 1, 1, 2], 0, false, true],+ ["complete compressed archived copy", [0], [0, 1, 1, 2], 1, true, true],] as const)("retains complete copied rollout events and precedence: %s",- async (_label, first, second, selected, archived) => {+ async (_label, first, second, selected, archived, compressed) => {const homes = [await temporaryHome(), await temporaryHome()];const activity = [commandEvent("first", "first-call", "2026-08-11T12:00:03Z"),@@ -88,6 +98,18 @@ describe("saved scan logs", () => {"parent",);}+ if (compressed) {+ const path = join(+ homes[1]!,+ "sessions",+ "2026",+ "08",+ "11",+ "rollout-worker.jsonl",+ );+ await writeFile(`${path}.zst`, zstdCompressSync(await readFile(path)));+ unlinkSync(path);+ }if (archived) {await rename(join(homes[1]!, "sessions"),@@ -113,7 +135,7 @@ describe("saved scan logs", () => {"2026","08","11",- "rollout-worker.jsonl",+ `rollout-worker.jsonl${compressed && selected === 1 ? ".zst" : ""}`,),);expect(@@ -366,6 +388,52 @@ describe("saved scan logs", () => {expect(JSON.stringify(result)).not.toContain("private unrelated scan");});+ test("reads sessions that Codex compressed", async () => {+ const home = await temporaryHome();+ await writeSession(+ home,+ "parent",+ [commandEvent("parent work", "parent-call")],+ undefined,+ undefined,+ "/scan",+ );+ await writeSession(+ home,+ "worker",+ [commandEvent("worker work", "worker-call")],+ "parent",+ );+ const directory = join(home, "sessions", "2026", "08", "11");+ for (const threadId of ["parent", "worker"]) {+ const path = join(directory, `rollout-${threadId}.jsonl`);+ await writeFile(`${path}.zst`, zstdCompressSync(await readFile(path)));+ unlinkSync(path);+ }++ expect(await findScanSession(home, "parent")).toMatchObject({+ workingDirectory: "/scan",+ path: join(directory, "rollout-parent.jsonl.zst"),+ });+ const result = await readScanLogs({+ scanId: "scan-1",+ threadId: "parent",+ codexHome: home,+ });+ expect(result.events).toEqual([+ expect.objectContaining({ threadId: "parent" }),+ {+ threadId: "parent",+ event: commandEvent("parent work", "parent-call"),+ },+ expect.objectContaining({ threadId: "worker" }),+ {+ threadId: "worker",+ event: commandEvent("worker work", "worker-call"),+ },+ ]);+ });+test("excludes inherited parent history from worker logs", async () => {const home = await temporaryHome();await writeSession(home, "parent", []);