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: fail stale-target scans closed in CI (#273)
mldangelo-oai343be87be72f
diff --git a/sdk/typescript/_bundled_plugin/scripts/workbench_db.py b/sdk/typescript/_bundled_plugin/scripts/workbench_db.pyindex 1593dc6a3..ad055c78a 100644--- a/sdk/typescript/_bundled_plugin/scripts/workbench_db.py+++ b/sdk/typescript/_bundled_plugin/scripts/workbench_db.py@@ -1412,9 +1412,12 @@ def complete_scan_locked(if scan["recipe_json"] is None:deep_scan.require_deep_scan_ready_for_parent_completion(connection, scan)warnings = json.loads(scan["completion_warnings_json"])+ target_warnings: list[str] = []warning = scan_target_warning(scan)- if warning is not None and warning not in warnings:- warnings.append(warning)+ if warning is not None:+ target_warnings.append(warning)+ if warning not in warnings:+ warnings.append(warning)scan_dir = require_canonical_scan_directory(Path(scan["scan_dir"]))completion_timestamp = now()completion_binding = workbench_completion_binding(scan, completion_timestamp)@@ -1453,8 +1456,11 @@ def complete_scan_locked(completion_warnings=warnings,)warning = scan_target_warning(scan)- if warning is not None and warning not in warnings:- warnings.append(warning)+ if warning is not None:+ if warning not in target_warnings:+ target_warnings.append(warning)+ if warning not in warnings:+ warnings.append(warning)manifest, findings, _ = _write_prepared_scan_finalization(prepared)except ContractError as exc:raise SystemExit(str(exc)) from exc@@ -1476,7 +1482,9 @@ def complete_scan_locked(except BaseException:connection.rollback()raise- return scan_context(connection, scan["id"])+ context = scan_context(connection, scan["id"])+ context["targetWarnings"] = target_warnings+ return contextif cost_json is None:measured_usage = scan_usage.collect_scan_usage(@@ -1544,7 +1552,9 @@ def complete_scan_locked(except BaseException:connection.rollback()raise- return scan_context(connection, scan["id"])+ context = scan_context(connection, scan["id"])+ context["targetWarnings"] = target_warnings+ return contextdef register_cli_scan(connection: sqlite3.Connection, args: argparse.Namespace) -> dict[str, Any]:@@ -3460,7 +3470,7 @@ def available_artifact_path(scan_dir: Path, candidate: Path) -> Path | None:resolved.relative_to(resolved_scan_dir)except (FileNotFoundError, RuntimeError, SystemExit, ValueError):return None- if resolved != candidate or not candidate.is_file():+ if os.path.normcase(resolved) != os.path.normcase(candidate) or not candidate.is_file():return Nonereturn resolveddiff --git a/sdk/typescript/src/api.ts b/sdk/typescript/src/api.tsindex f4033d685..4d3a0e20a 100644--- a/sdk/typescript/src/api.ts+++ b/sdk/typescript/src/api.ts@@ -163,7 +163,7 @@ export interface ScanOptions extends DeepScanOptions {details?: ScanReconnectDetails,) => void;onWorkerStatus?: (status: ScanWorkerStatus) => void;- onWarning?: (warning: string) => void;+ onWarning?: (warning: string, details?: ScanWarningDetails) => void;onObserverError?: (observer: ScanObserverName, error: unknown) => void;signal?: AbortSignal;}@@ -190,6 +190,10 @@ export interface ScanReconnectDetails {retryAfterSeconds?: number;}+export interface ScanWarningDetails {+ kind: "target_changed";+}+type ScanObserverName =| "onAuthentication"| "onCost"@@ -352,6 +356,7 @@ export class CodexSecurity {let releaseCredentialHome: (() => Promise<void>) | null = null;let scanFailure = false;let completionCost: ScanCost | null = null;+ let preparedTargetWarnings: string[] = [];let activeScan: {id: string;options: WorkbenchCommandOptions;@@ -874,11 +879,16 @@ export class CodexSecurity {);}completionCost = snapshot.cost;- await workbench(workbenchOptions, [+ const preparation = await workbench(workbenchOptions, ["prepare-scan-completion","--scan-id",scanId,]);+ preparedTargetWarnings = Array.isArray(preparation["targetWarnings"])+ ? preparation["targetWarnings"].filter(+ (warning): warning is string => typeof warning === "string",+ )+ : [];return snapshot.usage;},onScanStarted: options.onScanStarted,@@ -898,6 +908,14 @@ export class CodexSecurity {activeScan = null;const completedScan = completion["scan"];if (isRecord(completedScan) && Array.isArray(completedScan["warnings"])) {+ const targetWarnings = new Set([+ ...preparedTargetWarnings,+ ...(Array.isArray(completion["targetWarnings"])+ ? completion["targetWarnings"].filter(+ (warning): warning is string => typeof warning === "string",+ )+ : []),+ ]);for (const warning of completedScan["warnings"]) {if (typeof warning === "string") {notifyObserver(@@ -905,6 +923,9 @@ export class CodexSecurity {options.onWarning,options.onObserverError,warning,+ targetWarnings.has(warning)+ ? { kind: "target_changed" }+ : undefined,);}}diff --git a/sdk/typescript/src/cli.ts b/sdk/typescript/src/cli.tsindex 1311875d1..6608e6f41 100644--- a/sdk/typescript/src/cli.ts+++ b/sdk/typescript/src/cli.ts@@ -2614,6 +2614,7 @@ async function runScan(let progress: Progress | null = null;let lastWorkerUpdate = "";let phase: string | null = null;+ const targetWarnings: string[] = [];const configuredLogLevel =dependencies.environment["CODEX_SECURITY_LOG_LEVEL"]?.trim() ||dependencies.environment["LOG_LEVEL"]?.trim();@@ -2919,9 +2920,12 @@ async function runScan(progress.stage(message);progress.startTimer(runningMessage());},- onWarning: (warning) => {+ onWarning: (warning, details) => {+ const message = sanitizeDiagnosticValue(warning);+ if (details?.kind === "target_changed") {+ targetWarnings.push(message);+ }writeAboveProgress(() => {- const message = sanitizeDiagnosticValue(warning);diagnostic("scan.warning", { message });errorOutput.write(`codex-security: warning: ${message}\n`);});@@ -3049,6 +3053,10 @@ async function runScan(const blockingCount = result.findings.findings.filter(({ severity }) =>blockingSeverities.has(severity.level),).length;+ const scanData =+ targetWarnings.length === 0+ ? result.toJSON()+ : { ...result.toJSON(), warnings: targetWarnings };const incomplete = result.coverage.completeness !== "complete";progress?.stage("Scan complete");printScanSummary(@@ -3064,17 +3072,24 @@ async function runScan(findings: result.findings.findings.length,scan_id: result.manifest.scan.id,estimated_usd: result.cost?.estimatedUsd,- exit_code: incomplete ? 2 : blockingCount > 0 ? 1 : 0,+ exit_code:+ targetWarnings.length > 0 || incomplete ? 2 : blockingCount > 0 ? 1 : 0,});+ if (targetWarnings.length > 0) {+ errorOutput.write(+ "codex-security: Scan target changed during execution; results do not represent the current checkout.\n",+ );+ return { exitCode: 2, data: scanData };+ }if (incomplete) {errorOutput.write(threshold === undefined? `codex-security: Scan coverage is ${result.coverage.completeness}; results may be incomplete.\n`: `codex-security: Cannot evaluate the failure policy: coverage is ${result.coverage.completeness}.\n`,);- return { exitCode: 2, data: result.toJSON() };+ return { exitCode: 2, data: scanData };}- return { exitCode: blockingCount > 0 ? 1 : 0, data: result.toJSON() };+ return { exitCode: blockingCount > 0 ? 1 : 0, data: scanData };}// Filesystem and OS syscall failures cannot originate from the model transport,diff --git a/sdk/typescript/src/index.ts b/sdk/typescript/src/index.tsindex 2ef1c60e0..747aa260e 100644--- a/sdk/typescript/src/index.ts+++ b/sdk/typescript/src/index.ts@@ -9,6 +9,7 @@ export type {ScanOptions,ScanPreflight,ScanReconnectDetails,+ ScanWarningDetails,} from "./api.js";export type { ScanWorkerPhase, ScanWorkerStatus } from "./worker-progress.js";export { CodexLoginHandle } from "./auth.js";diff --git a/sdk/typescript/tests-ts/api.test.ts b/sdk/typescript/tests-ts/api.test.tsindex 4b7f68824..9ec2a331f 100644--- a/sdk/typescript/tests-ts/api.test.ts+++ b/sdk/typescript/tests-ts/api.test.ts@@ -1717,10 +1717,13 @@ describe("CodexSecurity orchestration", () => {let prompt = "";let scanStarted = false;const warnings: string[] = [];+ const warningDetails: Array<{ kind: "target_changed" } | undefined> = [];const reconnects: Array<[number, number]> = [];const commands: Array<readonly string[]> = [];const completionWarning ="Repository HEAD changed while the scan was running; results were saved for the original revision.";+ const recoveryWarning =+ "Recovered finding: normalized its semantic anchor.";const client = new TestClient({ codexOverrides: { model: "replay-model" } },@@ -1760,8 +1763,14 @@ describe("CodexSecurity orchestration", () => {falsePositives: [],};}+ if (args[0] === "prepare-scan-completion") {+ return { targetWarnings: [completionWarning] };+ }if (args[0] === "complete-scan") {- return { scan: { warnings: [completionWarning] } };+ return {+ scan: { warnings: [completionWarning, recoveryWarning] },+ targetWarnings: [],+ };}return {};},@@ -1794,8 +1803,9 @@ describe("CodexSecurity orchestration", () => {onScanStarted: () => {scanStarted = true;},- onWarning: (warning) => {+ onWarning: (warning, details) => {warnings.push(warning);+ warningDetails.push(details);},onReconnect: (attempt, maxAttempts) => {reconnects.push([attempt, maxAttempts]);@@ -1803,7 +1813,8 @@ describe("CodexSecurity orchestration", () => {});expect(result.threadId).toBe("thread-1");expect(scanStarted).toBe(true);- expect(warnings).toEqual([completionWarning]);+ expect(warnings).toEqual([completionWarning, recoveryWarning]);+ expect(warningDetails).toEqual([{ kind: "target_changed" }, undefined]);expect(reconnects).toEqual([[2, 5]]);const startedAt = (codexOptions as CodexOptions | null)?.env?.["CODEX_SECURITY_STARTED_AT"diff --git a/sdk/typescript/tests-ts/cli.test.ts b/sdk/typescript/tests-ts/cli.test.tsindex 0d6ca9fb6..3c1b499c8 100644--- a/sdk/typescript/tests-ts/cli.test.ts+++ b/sdk/typescript/tests-ts/cli.test.ts@@ -3637,28 +3637,65 @@ describe("CLI", () => {}});- test("prints scan completion warnings without failing the scan", async () => {- const stdout = capture();- const stderr = capture();- const deps = dependencies();- deps.createSecurity = () => ({- run: async (_repository, options) => {- options?.onWarning?.(- "Repository HEAD changed while the scan was running; results were saved for the original revision.",- );- return fakeResult();- },- close: async () => {},- preflight: async () => fakePreflight(),- });+ test("fails stale scans and includes target warnings in machine-readable results", async () => {+ for (const warning of [+ "Repository HEAD changed while the scan was running; results were saved for the original revision.",+ "Directory contents changed while the scan was running; results were saved for the original snapshot.",+ "Working-tree contents changed while the scan was running; results were saved for the original snapshot.",+ "The scanned Git repository became unavailable while the scan was running; results were saved for the original revision.",+ "The scan target became unavailable while the scan was running; results were saved for the original revision or snapshot.",+ "Repository HEAD changed while the scan was running; findings belong to the previous checkout.",+ "Completed findings no longer describe the selected source tree.",+ ]) {+ const stdout = capture();+ const stderr = capture();+ const deps = dependencies();+ deps.createSecurity = () => ({+ run: async (_repository, options) => {+ options?.onWarning?.(warning, { kind: "target_changed" });+ return fakeResult();+ },+ close: async () => {},+ preflight: async () => fakePreflight(),+ });- expect(- await main(["scan", ".", "--json"], stdout.stream, stderr.stream, deps),- ).toBe(0);- expect(JSON.parse(stdout.text())).toEqual(fakeResult().toJSON());- expect(stderr.text()).toContain(- "codex-security: warning: Repository HEAD changed while the scan was running; results were saved for the original revision.",- );+ expect(+ await main(["scan", ".", "--json"], stdout.stream, stderr.stream, deps),+ ).toBe(2);+ expect(JSON.parse(stdout.text())).toEqual({+ ...fakeResult().toJSON(),+ warnings: [warning],+ });+ expect(stderr.text()).toContain(`codex-security: warning: ${warning}`);+ expect(stderr.text()).toContain(+ "Scan target changed during execution; results do not represent the current checkout.",+ );+ }+ });++ test("preserves non-target warnings without failing the scan", async () => {+ for (const warning of [+ "Recovered finding: normalized its semantic anchor.",+ "Repository HEAD changed while the scan was running; informational retry recovered.",+ ]) {+ const stdout = capture();+ const stderr = capture();+ const deps = dependencies();+ deps.createSecurity = () => ({+ run: async (_repository, options) => {+ options?.onWarning?.(warning);+ return fakeResult();+ },+ close: async () => {},+ preflight: async () => fakePreflight(),+ });++ expect(+ await main(["scan", ".", "--json"], stdout.stream, stderr.stream, deps),+ ).toBe(0);+ expect(JSON.parse(stdout.text())).toEqual(fakeResult().toJSON());+ expect(stderr.text()).toContain(`codex-security: warning: ${warning}`);+ }});test("emits redacted scan warnings in verbose diagnostics", async () => {diff --git a/sdk/typescript/tests-ts/scan-recovery.test.ts b/sdk/typescript/tests-ts/scan-recovery.test.tsindex 75d62c3e2..153e5d4b3 100644--- a/sdk/typescript/tests-ts/scan-recovery.test.ts+++ b/sdk/typescript/tests-ts/scan-recovery.test.ts@@ -386,6 +386,31 @@ describe("malformed scan artifact recovery", () => {expect((await completeScan(fixture)).progress.status).toBe("complete");});+ test("preserves target-drift classification from prepared completion", async () => {+ const fixture = await startDraftScan();+ const source = join(fixture.repository, "src", "extract.py");+ const original = await readFile(source, "utf8");+ await writeFile(source, "# target changed during scan\n");++ const prepared = await workbench(fixture, [+ "prepare-scan-completion",+ "--scan-id",+ fixture.scanId,+ ]);+ const warning =+ "Directory contents changed while the scan was running; results were saved for the original snapshot.";+ expect(prepared["targetWarnings"]).toEqual([warning]);++ await writeFile(source, original);+ const completed = await workbench(fixture, [+ "complete-scan",+ "--scan-id",+ fixture.scanId,+ ]);+ expect((completed["scan"] as ScanSummary).warnings).toContain(warning);+ expect(completed["targetWarnings"]).toEqual([]);+ });+test("marks rejected prepared scans as failed without publishing completion", async () => {const fixture = await startDraftScan();await workbench(fixture, [@@ -461,9 +486,15 @@ describe("malformed scan artifact recovery", () => {expect((prepared["scan"] as ScanSummary).progress.status).toBe("running");expect((prepared["scan"] as ScanSummary).warnings).toEqual([warning]);- const completed = await completeScan(fixture);+ const completion = await workbench(fixture, [+ "complete-scan",+ "--scan-id",+ fixture.scanId,+ ]);+ const completed = completion["scan"] as ScanSummary;expect(completed.progress.status).toBe("complete");expect(completed.warnings).toEqual([warning]);+ expect(completion["targetWarnings"]).toEqual([]);const saved = await workbench(fixture, ["get-scan","--scan-id",