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(publication): pass payloads in memory and preserve failures (#1507)
mldangelo-oai8c4931d996eb
diff --git a/plugins/codex-security/scripts/workbench_db.py b/plugins/codex-security/scripts/workbench_db.pyindex 1217d33b7..505df4d84 100644--- a/plugins/codex-security/scripts/workbench_db.py+++ b/plugins/codex-security/scripts/workbench_db.py@@ -3333,7 +3333,9 @@ def main(*, before_archive: Callable[[], None] | None = None) -> None:print(json.dumps(result, allow_nan=False, sort_keys=True))returnif args.command == "inspect-linear-publication":- result = publication.inspect_linear_publication(_WORKBENCH_PUBLICATION_CONTEXT, args)+ result = publication.inspect_linear_publication(+ _WORKBENCH_PUBLICATION_CONTEXT, read_json_object(Path(args.input_file))+ )print(json.dumps(result, allow_nan=False, sort_keys=True))returnwith closing(@@ -3395,7 +3397,12 @@ def main(*, before_archive: Callable[[], None] | None = None) -> None:"export-findings": partial(publication.export_findings, _WORKBENCH_PUBLICATION_CONTEXT),}if handler := handlers.get(args.command):- result = handler(connection, args)+ payload = (+ read_json_object(Path(args.input_file))+ if args.command in {"prepare-linear-publication", "record-linear-publications"}+ else args+ )+ result = handler(connection, payload)elif args.command == "get-workspace":result = workspace_state(connection,diff --git a/plugins/codex-security/scripts/workbench_publication.py b/plugins/codex-security/scripts/workbench_publication.pyindex d873cb411..3124a9701 100644--- a/plugins/codex-security/scripts/workbench_publication.py+++ b/plugins/codex-security/scripts/workbench_publication.py@@ -29,12 +29,10 @@ from finalize_scan_contract import (def linear_publication_input(- db: Any,- args: argparse.Namespace,+ payload: dict[str, Any],*,recording: bool,) -> tuple[dict[str, Any], dict[str, str], list[dict[str, str]]]:- payload = db.read_json_object(Path(args.input_file))required = {"scanId", "scanDirectory", "destination", "findings"}if recording:required.add("publications")@@ -140,9 +138,9 @@ def verify_linear_publication_scan(def inspect_linear_publication(db: Any,- args: argparse.Namespace,+ payload: dict[str, Any],) -> dict[str, Any]:- payload, destination, findings = linear_publication_input(db, args, recording=False)+ payload, destination, findings = linear_publication_input(payload, recording=False)database_uri = f"file:{quote(str(db.database_path()), safe='')}?mode=ro"with closing(sqlite3.connect(database_uri, uri=True, timeout=5)) as connection:connection.row_factory = sqlite3.Row@@ -190,9 +188,9 @@ def inspect_linear_publication(def prepare_linear_publication(db: Any,connection: sqlite3.Connection,- args: argparse.Namespace,+ payload: dict[str, Any],) -> dict[str, Any]:- payload, destination, findings = linear_publication_input(db, args, recording=False)+ payload, destination, findings = linear_publication_input(payload, recording=False)connection.execute("BEGIN IMMEDIATE")with connection:scan = verify_linear_publication_scan(db, connection, payload, findings)@@ -207,9 +205,9 @@ def prepare_linear_publication(def record_linear_publications(db: Any,connection: sqlite3.Connection,- args: argparse.Namespace,+ payload: dict[str, Any],) -> dict[str, Any]:- payload, destination, findings = linear_publication_input(db, args, recording=True)+ payload, destination, findings = linear_publication_input(payload, recording=True)publications = payload["publications"]if not isinstance(publications, list):raise SystemExit("Linear publication results must be an array.")diff --git a/sdk/typescript/src/cli.ts b/sdk/typescript/src/cli.tsindex 4cc19497d..7a0e6bee7 100644--- a/sdk/typescript/src/cli.ts+++ b/sdk/typescript/src/cli.ts@@ -151,6 +151,7 @@ import {AuthenticationRequiredError,ConfigurationError,InvalidTargetError,+ IncompleteScanError,LocalPluginBootstrapError,OutputDirectoryError,OutputInsideProtectedRootError,@@ -9022,6 +9023,7 @@ const LOCAL_SYSCALL_CODES = new Set([]);function isLocalScanFailure(error: unknown): boolean {+ if (error instanceof IncompleteScanError) error = error.cause;if (error instanceof InvalidTargetError ||error instanceof OutputDirectoryError ||@@ -9130,13 +9132,19 @@ function scanFailureMessage(const nativeRefreshRecovery = message.match(/\b(?:your access token could not be refreshed because you have since logged out or signed in to another account\. Please sign in again\.|your authentication session could not be refreshed automatically\. Please log out and sign in again\.)/iu,)?.[0];- if (nativeRefreshRecovery !== undefined) return nativeRefreshRecovery;+ if (nativeRefreshRecovery !== undefined)+ return error instanceof IncompleteScanError+ ? diagnosticValue(error)+ : nativeRefreshRecovery;+ const detail =+ error instanceof IncompleteScanError ? `${diagnosticValue(error)}\n` : "";if (/\byour access token could not be refreshed(?: because your refresh token (?:has expired|was already used|was revoked))?\. Please log out and sign in again\./iu.test(message,)) {return (+ detail +"Codex Security's stored ChatGPT sign-in could not be refreshed. " +"Codex may still need it to load workspace-managed policies when an API key is selected for model authentication. " +"If the sign-in recently changed, check 'npx @openai/codex-security login status' and retry. " +@@ -9145,18 +9153,18 @@ function scanFailureMessage(}switch (classification) {case "unauthorized":- return authenticationFailureMessage(authentication);+ return detail + authenticationFailureMessage(authentication);case "forbidden":if (authentication?.method === "command") {- return "The configured Codex provider denied access. Check the command credentials and provider permissions.";+ return `${detail}The configured Codex provider denied access. Check the command credentials and provider permissions.`;}return authentication?.method === "api_key"- ? `The API key from ${authentication.source} cannot access the configured model. ` ++ ? `${detail}The API key from ${authentication.source} cannot access the configured model. ` +"Retry with '--auth chatgpt' or use an API key with model access."- : "The stored ChatGPT credentials cannot access the configured model. " ++ : `${detail}The stored ChatGPT credentials cannot access the configured model. ` +"Use an account or API key with model access.";case "rate_limited":- return "The configured account reached its rate limit. Wait and retry.";+ return `${detail}The configured account reached its rate limit. Wait and retry.`;case "network_error":case "timeout":case "unknown":diff --git a/sdk/typescript/src/component-scan.ts b/sdk/typescript/src/component-scan.tsindex 4760530ce..7c8fe64f3 100644--- a/sdk/typescript/src/component-scan.ts+++ b/sdk/typescript/src/component-scan.ts@@ -1,3 +1,4 @@+import { notify } from "./value.js";import { writeFile } from "node:fs/promises";import { tmpdir } from "node:os";import { basename, join, relative, sep } from "node:path";@@ -427,12 +428,6 @@ async function deduplicateFindings(return { findings, ...matching, ...(error === undefined ? {} : { error }) };}-function notify(callback: () => unknown): void {- try {- void Promise.resolve(callback()).catch(() => {});- } catch {}-}-function mergeFindingGroups(findings: CombinedFinding[],occurrenceIds: Set<string>,diff --git a/sdk/typescript/src/deduplication/diagnostics.ts b/sdk/typescript/src/deduplication/diagnostics.tsindex 885991024..51a3fc47d 100644--- a/sdk/typescript/src/deduplication/diagnostics.ts+++ b/sdk/typescript/src/deduplication/diagnostics.ts@@ -1,3 +1,4 @@+import { notify } from "../value.js";import type { DeduplicationReviewStage } from "../errors.js";/** Best-effort diagnostics; never written to the dedupe result's JSON stream. */@@ -22,11 +23,7 @@ export function emitDiagnostic(observer: DeduplicationDiagnosticObserver | undefined,diagnostic: Omit<DeduplicationDiagnostic, "timestamp">,): void {- try {- void Promise.resolve(- observer?.({ timestamp: new Date().toISOString(), ...diagnostic }),- ).catch(() => {});- } catch {- // Optional diagnostics must not interrupt a review or discard its result.- }+ notify(() =>+ observer?.({ timestamp: new Date().toISOString(), ...diagnostic }),+ );}diff --git a/sdk/typescript/src/multiscan.ts b/sdk/typescript/src/multiscan.tsindex 40d4c2838..5f7ee310f 100644--- a/sdk/typescript/src/multiscan.ts+++ b/sdk/typescript/src/multiscan.ts@@ -1,3 +1,4 @@+import { notify } from "./value.js";import { execFile as execFileCallback } from "node:child_process";import { randomUUID } from "node:crypto";import { realpathSync } from "node:fs";@@ -641,9 +642,7 @@ function notifyProgress(options: MultiscanOptions,event: Parameters<NonNullable<MultiscanOptions["onProgress"]>>[0],): void {- try {- void Promise.resolve(options.onProgress?.(event)).catch(() => {});- } catch {}+ notify(() => options.onProgress?.(event));}async function ensureOutputDirectory(path: string): Promise<string> {diff --git a/sdk/typescript/src/publication-store.ts b/sdk/typescript/src/publication-store.tsindex 52bbb02f6..643840df2 100644--- a/sdk/typescript/src/publication-store.ts+++ b/sdk/typescript/src/publication-store.ts@@ -1,6 +1,5 @@import { findingEntry } from "./value.js";-import { mkdtemp, realpath, rm, stat, writeFile } from "node:fs/promises";-import { tmpdir } from "node:os";+import { stat } from "node:fs/promises";import { join } from "node:path";import { isRecord } from "./record.js";import { CodexSecurityError } from "./errors.js";@@ -8,7 +7,6 @@ import type { PreparedScanPublication } from "./publication.js";import type { PublishedScanIssue } from "./publish.js";import {codexSecurityStateDirectory,- requireOutputOutsideRepository,resolveWorkbenchRuntime,runWorkbench,} from "./runtime.js";@@ -134,13 +132,17 @@ async function runPublicationWorkbench(signal?.throwIfAborted();const stateDirectory = codexSecurityStateDirectory(environment);const database = join(stateDirectory, "workbench.sqlite3");- try {- if (!(await stat(database)).isFile()) throw new Error("not a regular file");- } catch (error) {+ const metadata = await stat(database).catch((error: unknown) => {+ if (!isRecord(error) || error["code"] !== "ENOENT") throw error;throw new CodexSecurityError("Cannot publish findings because the local Codex Security scan-history database does not exist. Use the state directory where this scan was completed.",{ cause: error },);+ });+ if (!metadata.isFile()) {+ throw new CodexSecurityError(+ "Cannot publish findings because the local Codex Security scan-history database is not a regular file.",+ );}const [python, pluginRoot] = await resolveWorkbenchRuntime({environment,@@ -154,44 +156,26 @@ async function runPublicationWorkbench(occurrenceId,}),);- let temporaryRoot = stateDirectory;- if (command === "inspect-linear-publication") {- temporaryRoot = await realpath(tmpdir());- const scanRoot = await realpath(publication.scanDirectory);- requireOutputOutsideRepository(scanRoot, temporaryRoot, "temporary");- }- const directory = await mkdtemp(join(temporaryRoot, "publication-"));- try {- const input = join(directory, "publication.json");- await writeFile(- input,- JSON.stringify({- scanId: publication.scanId,- scanDirectory: publication.scanDirectory,- destination: publication.destination,- findings,- ...(issues === undefined ? {} : { publications: issues }),- }),- { encoding: "utf8", flag: "wx", mode: 0o600 },- );- return await runWorkbench(- {- python,- pluginRoot,- environment,- ...(signal === undefined ? {} : { signal }),- failureMessage:- command === "record-linear-publications"- ? "Could not persist created Linear issues in the local Codex Security scan history"- : "Cannot publish findings without their existing local Codex Security scan history",- },- [command, "--input-file", input],- );- } finally {- await rm(directory, { recursive: true, force: true }).catch(- () => undefined,- );- }+ return await runWorkbench(+ {+ python,+ pluginRoot,+ environment,+ ...(signal === undefined ? {} : { signal }),+ failureMessage:+ command === "record-linear-publications"+ ? "Could not persist created Linear issues in the local Codex Security scan history"+ : "Cannot publish findings without their existing local Codex Security scan history",+ },+ [command],+ JSON.stringify({+ scanId: publication.scanId,+ scanDirectory: publication.scanDirectory,+ destination: publication.destination,+ findings,+ ...(issues === undefined ? {} : { publications: issues }),+ }),+ );}function matchesPublication(diff --git a/sdk/typescript/src/publication.ts b/sdk/typescript/src/publication.tsindex 9895c5ba4..7f57ba977 100644--- a/sdk/typescript/src/publication.ts+++ b/sdk/typescript/src/publication.ts@@ -38,7 +38,7 @@ export interface PrepareScanPublicationOptions {environment?: NodeJS.ProcessEnv;signal?: AbortSignal;expectedScanId?: string;- /** Publish only these finding IDs (for example dedupe's uniqueFindingIds). */+ /** Publish only these finding IDs from the supplied scan; dedupe representatives may belong to other scans. */findingIds?: readonly string[];/** Use this assessment; otherwise use the scan's saved classification when present. */classification?: SeverityClassification | ScanSeverityClassification;diff --git a/sdk/typescript/src/publish.ts b/sdk/typescript/src/publish.tsindex 255ebf0b8..0b6abb9f9 100644--- a/sdk/typescript/src/publish.ts+++ b/sdk/typescript/src/publish.ts@@ -1,4 +1,4 @@-import { findingEntry } from "./value.js";+import { findingEntry, notify } from "./value.js";import {spawn,spawnSync,@@ -204,6 +204,7 @@ type PublicationHandoffEvidence = {ownerFindingId?: string;resolution: ClaimResolution;possibleMutation?: boolean;+ cause?: unknown;} & (| { status: "success" }| { status: "failure"; error: string }@@ -371,7 +372,6 @@ export async function publishScanInternal(if (dependencies.runCodex === undefined &&error instanceof CodexSecurityError &&- error.message === "Could not start Codex for Linear publication." &&isRecord(cause) &&typeof cause["syscall"] === "string" &&cause["syscall"].startsWith("spawn ")@@ -391,14 +391,30 @@ export async function publishScanInternal(: invocation!.exitCode === 0? "Codex did not create a Linear issue for this finding.": codexFailureMessage(invocation!.stderr, invocation!.exitCode);- const evidence: PublicationEvidence[] = [- ...collectPublicationEvents(- invocation?.stdout ?? "",- prepared,- failureMessage,- ),- ...(await collectPublicationHandoffEvidence(handoff.file, prepared)),- ];+ const events = collectPublicationEvents(+ invocation?.stdout ?? "",+ prepared,+ failureMessage,+ );+ const connectorEvents = events.map((item) => item.rawLine);+ let eventLogNotice: string | undefined;+ const preserveConnectorEvents = async (): Promise<void> => {+ if (eventLogNotice !== undefined || connectorEvents.length === 0) return;+ try {+ const file = await (dependencies.writeEvents ?? writePublicationEvents)(+ handoff.directory,+ connectorEvents,+ );+ eventLogNotice = `Linear connector-event evidence remains at ${file}.`;+ } catch (error) {+ eventLogNotice = `Could not preserve Linear connector-event evidence: ${errorMessage(error)}.`;+ }+ };+ const handoffEvidence = await collectPublicationHandoffEvidence(+ handoff.file,+ prepared,+ );+ const evidence = [...events, ...handoffEvidence];const handoffResults = reconcilePublicationEvidence(prepared,evidence,@@ -430,22 +446,6 @@ export async function publishScanInternal(}}const recoveryMessage = `The publication handoff remains at ${handoff.file}; recover it before retrying to avoid creating duplicate issues.`;- const connectorEvents = evidence.flatMap((item) =>- item.source === "event" ? [item.rawLine] : [],- );- let eventLogNotice: string | undefined;- const preserveConnectorEvents = async (): Promise<void> => {- if (eventLogNotice !== undefined || connectorEvents.length === 0) return;- try {- const file = await (dependencies.writeEvents ?? writePublicationEvents)(- handoff.directory,- connectorEvents,- );- eventLogNotice = `Linear connector-event evidence remains at ${file}.`;- } catch (error) {- eventLogNotice = `Could not preserve Linear connector-event evidence: ${errorMessage(error)}.`;- }- };if (handoffResults.indeterminate) {result.indeterminate = true;result.warnings = [@@ -506,7 +506,9 @@ export async function publishScanInternal(: `Could not persist created Linear issues: ${persistenceFailure.detail}`;const cause =persistenceFailure === undefined- ? options.signal?.reason+ ? options.signal?.aborted+ ? options.signal.reason+ : handoffEvidence.find((item) => item.cause !== undefined)?.cause: persistenceFailure.cause;if (persistenceFailure !== undefined && !result.indeterminate) {throw new CodexSecurityError(`${reason}. ${recoveryDetails}`, { cause });@@ -810,11 +812,7 @@ function reportPublicationProgress(event: PublishScanProgress,): void {if (observer === undefined) return;- try {- observer(event);- } catch {- // Optional progress reporting must not stop issue publication.- }+ notify(() => observer(event));}function publicationPrompt(@@ -914,22 +912,34 @@ async function createPublicationHandoff(return { directory, file, publicationFile };}+async function readPublicationHandoff(file: string): Promise<string> {+ try {+ return await readFile(file, "utf8");+ } catch (error) {+ if (isRecord(error) && error["code"] === "ENOENT") return "";+ throw new CodexSecurityError(+ `Could not read the publication handoff: ${errorMessage(error)}. The publication handoff remains at ${file}; recover it before retrying to avoid creating duplicate issues.`,+ { cause: error },+ );+ }+}+async function collectPublicationHandoffEvidence(file: string,publication: PreparedScanPublication,): Promise<PublicationHandoffEvidence[]> {let content: string;try {- content = await readFile(file, "utf8");+ content = await readPublicationHandoff(file);} catch (error) {- if ((error as NodeJS.ErrnoException).code === "ENOENT") return [];return [{source: "handoff",status: "invalid",possibleMutation: true,resolution: resolveClaims([]),- error: `Could not read the Linear publication handoff: ${errorMessage(error)}`,+ error: errorMessage(error),+ cause: error,},];}@@ -1351,13 +1361,7 @@ async function preserveVerifiedHandoff(publication: PreparedScanPublication,issues: readonly PublishedScanIssue[],): Promise<void> {- let current: string;- try {- current = await readFile(file, "utf8");- } catch (error) {- if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error;- current = "";- }+ const current = await readPublicationHandoff(file);const planned = new Map(publication.issues.map(findingEntry));const verified = new Map(issues.map(findingEntry));const recorded = new Set<string>();@@ -1504,7 +1508,7 @@ async function runPublicationCodex(cleanup();reject(new CodexSecurityError(- "Could not start Codex for Linear publication.",+ `Could not start Codex for Linear publication: ${errorMessage(error)}`,{cause: error,},diff --git a/sdk/typescript/src/runtime.ts b/sdk/typescript/src/runtime.tsindex 5b7355e45..285cb6bda 100644--- a/sdk/typescript/src/runtime.ts+++ b/sdk/typescript/src/runtime.ts@@ -1,5 +1,5 @@import { gitProtectionRoots } from "./targets.js";-import { isNonEmptyString } from "./value.js";+import { isNonEmptyString, notify } from "./value.js";import { execFile as execFileCallback, spawn } from "node:child_process";import { randomUUID } from "node:crypto";import {@@ -1596,6 +1596,22 @@ def before_archive():workbench["main"](before_archive=before_archive)`;+// Internal publication callers use only the SDK's bundled workbench.+const PUBLICATION_PROGRAM = String.raw`+import json, runpy, sys+from contextlib import closing+workbench = runpy.run_path(sys.argv[1])+context = workbench["_WORKBENCH_PUBLICATION_CONTEXT"]+payload = json.load(sys.stdin, parse_constant=workbench["reject_non_finite_json"])+handler = getattr(workbench["publication"], sys.argv[2].replace("-", "_"))+if sys.argv[2] == "inspect-linear-publication":+ result = handler(context, payload)+else:+ with closing(workbench["connect"]()) as connection:+ result = handler(context, connection, payload)+print(json.dumps(result, allow_nan=False, sort_keys=True))+`;+const workbenchComparisonSupport = new Map<string,{ stdin: boolean; related: boolean }@@ -1647,10 +1663,22 @@ export async function runWorkbench(? (options.stateDirectory ??codexSecurityStateDirectory(options.environment)): undefined;+ const publicationInput =+ input !== undefined &&+ arguments_.length === 1 &&+ [+ "inspect-linear-publication",+ "prepare-linear-publication",+ "record-linear-publications",+ ].includes(arguments_[0]!);// OS argv cannot carry NUL, but workbench text fields can.const framedArguments =!native && arguments_.some((argument) => argument.includes("\0"));- let program = archiveHandshake ? ARCHIVE_REGISTRATION_PROGRAM : undefined;+ let program = archiveHandshake+ ? ARCHIVE_REGISTRATION_PROGRAM+ : publicationInput+ ? PUBLICATION_PROGRAM+ : undefined;if (framedArguments)program =WORKBENCH_ARGUMENTS_PROGRAM + (program ?? WORKBENCH_SCRIPT_PROGRAM);@@ -2154,11 +2182,7 @@ async function prepareOutputDirectory(const archiveDir = await planOutputArchive(path);if (archiveDir !== null) {await rename(path, archiveDir);- try {- void Promise.resolve(onOutputArchived?.(archiveDir)).catch(- () => undefined,- );- } catch {}+ notify(() => onOutputArchived?.(archiveDir));existing = null;}}diff --git a/sdk/typescript/src/value.ts b/sdk/typescript/src/value.tsindex a27d69655..0d722843f 100644--- a/sdk/typescript/src/value.ts+++ b/sdk/typescript/src/value.ts@@ -21,3 +21,10 @@ export function parseJson(read: () => string): unknown {export const findingEntry = <T extends { findingId: string }>(finding: T,): [string, T] => [finding.findingId, finding];++/** @internal Optional observers must not block or interrupt their operation. */+export function notify(callback: () => unknown): void {+ try {+ void Promise.resolve(callback()).catch(() => {});+ } catch {}+}diff --git a/sdk/typescript/tests-ts/cli-error-diagnostics.test.ts b/sdk/typescript/tests-ts/cli-error-diagnostics.test.tsindex 7a406a662..46726b524 100644--- a/sdk/typescript/tests-ts/cli-error-diagnostics.test.ts+++ b/sdk/typescript/tests-ts/cli-error-diagnostics.test.ts@@ -1,9 +1,11 @@-import { rm, writeFile } from "node:fs/promises";+import { mkdir, rm, writeFile } from "node:fs/promises";import { join } from "node:path";import { expect, test } from "bun:test";import { readCodexHomeConfig } from "../src/auth.js";import { main } from "../src/cli.js";+import { IncompleteScanError } from "../src/errors.js";import { parseImportedFindings } from "../src/findings-import.js";+import { runWorkbench } from "../src/runtime.js";import { PLUGIN_ROOT } from "./plugin-root.js";import { dependencies } from "./cli-fixtures.js";import { createCliTest } from "./support/cli-run.js";@@ -16,6 +18,45 @@ const escapedDetail =const escapedSingleLineDetail ="Synthetic sk-proj-SYNTHETIC_KEY_123 ]52;c;U1lOVEhFVElD 2J second diagnostic line";+test("scans list retains actual SQLite diagnostics and recovery advice", async () => {+ const root = await temporaryDirectory("history-database-diagnostic-", true);+ const stateDirectory = join(root, "state");+ const database = join(stateDirectory, "workbench.sqlite3");+ try {+ await mkdir(database, { recursive: true });+ const environment = {+ ...process.env,+ CODEX_SECURITY_STATE_DIR: stateDirectory,+ };+ const { stdout, stderr, runCli } = createCliTest(main);+ expect(+ await runCli(+ ["scans", "list"],+ dependencies({+ currentDirectory: root,+ environment,+ onWorkbench: (args, input, signal) =>+ runWorkbench(+ { pluginRoot: PLUGIN_ROOT, environment, signal },+ args,+ input,+ ),+ }),+ ),+ ).toBe(2);+ expect(stdout.text()).toBe("");+ expect(stderr.text()).toContain("Traceback");+ expect(stderr.text()).toContain(+ "sqlite3.OperationalError: unable to open database file",+ );+ expect(stderr.text()).toContain(database);+ expect(stderr.text()).toContain("SQLite journal files are writable");+ expect(stderr.text().match(/CODEX_SECURITY_STATE_DIR/gu)).toHaveLength(1);+ } finally {+ await rm(root, { recursive: true, force: true });+ }+});+test("feedback escapes real config diagnostics only at the text output boundary", async () => {const root = await temporaryDirectory("feedback-config-diagnostic-", true);const environment = { CODEX_HOME: root };@@ -105,6 +146,96 @@ for (const scenario of [});}+test.each([+ {+ label: "filesystem",+ diagnostic:+ "EACCES: permission denied, open /synthetic/scan/artifacts/candidates.jsonl",+ advice: "cannot access the configured model",+ },+ {+ label: "wrapped filesystem",+ diagnostic:+ "EACCES: permission denied, mkdtemp /synthetic/codex-output-schema",+ causeCode: "EACCES",+ advice: undefined,+ },+ {+ label: "native refresh recovery",+ diagnostic:+ "Synthetic context: your access token could not be refreshed because you have since logged out or signed in to another account. Please sign in again. Synthetic artifact recovery failed.",+ advice: "Please sign in again.",+ },+ {+ label: "expired native refresh",+ diagnostic:+ "Synthetic context: your access token could not be refreshed because your refresh token has expired. Please log out and sign in again. Synthetic artifact recovery failed.",+ advice: "stored ChatGPT sign-in could not be refreshed",+ },+ {+ label: "authentication",+ diagnostic: "401 synthetic unauthorized request",+ advice: "Authentication failed",+ },+ {+ label: "authorization",+ diagnostic: "403 synthetic model access denied",+ advice: "cannot access the configured model",+ },+ {+ label: "rate limit",+ diagnostic: "429 synthetic quota exceeded",+ advice: "reached its rate limit",+ },+])(+ "retains incomplete worker $label diagnostics",+ async ({ diagnostic, advice, ...scenario }) => {+ const cause = Object.assign(new Error("Synthetic worker cause"), {+ code: "causeCode" in scenario ? scenario.causeCode : undefined,+ });+ const failure = new IncompleteScanError(`${diagnostic}; ${detail}`, {+ cause,+ });+ for (const json of [false, true]) {+ const { stdout, stderr, runCli } = createCliTest(main);+ const deps = dependencies({+ onRun: () => {+ throw failure;+ },+ });+ expect(+ await runCli(["scan", ".", ...(json ? ["--json"] : [])], deps),+ ).toBe(2);+ const expected = `${diagnostic}; ${escapedSingleLineDetail}`;+ expect(stderr.text()).toContain(expected);+ if (advice === undefined) {+ expect(stderr.text()).not.toContain(+ "cannot access the configured model",+ );+ } else {+ expect(stderr.text()).toContain(advice);+ expect(stderr.text().split(advice)).toHaveLength(2);+ }+ expect(stderr.text()).not.toContain(controls);+ if (json) {+ const output = JSON.parse(stdout.text());+ expect(output).toMatchObject({ status: "failed", code: "SCAN_FAILED" });+ expect(output.message).toContain(expected);+ if (advice === undefined) {+ expect(output.message).not.toContain(+ "cannot access the configured model",+ );+ } else {+ expect(output.message).toContain(advice);+ expect(output.message.split(advice)).toHaveLength(2);+ }+ } else expect(stdout.text()).toBe("");+ expect(failure.message).toBe(`${diagnostic}; ${detail}`);+ expect(failure.cause).toBe(cause);+ }+ },+);+test("successful raw exports preserve terminal controls as artifact bytes", async () => {const deps = dependencies();deps.exportFindings = async () => Buffer.from(detail);diff --git a/sdk/typescript/tests-ts/publication-store.test.ts b/sdk/typescript/tests-ts/publication-store.test.tsindex 4e4e74b84..136496821 100644--- a/sdk/typescript/tests-ts/publication-store.test.ts+++ b/sdk/typescript/tests-ts/publication-store.test.ts@@ -2,15 +2,15 @@ import { once } from "node:events";import { createHash, randomUUID } from "node:crypto";import { existsSync } from "node:fs";import {+ chmod,mkdir,readFile,readdir,stat,- symlink,writeFile,} from "node:fs/promises";import * as os from "node:os";-import { dirname, join, toNamespacedPath } from "node:path";+import { join, toNamespacedPath } from "node:path";import { afterEach, describe, expect, spyOn, test } from "bun:test";import {inspectPublicationStore,@@ -269,37 +269,56 @@ describe("read-only publication history", () => {).toEqual([{ seal_manifest_digest: digest }]);});- test("keeps inspection temporaries outside the completed scan", async () => {+ test("inspects history without temporary-directory access", async () => {const fixture = await publicationFixture();- const scan = fixture.publication.scanDirectory;- const nested = join(scan, "temporary");- const alias = join(dirname(fixture.stateDirectory), "temporary-link");- await mkdir(nested);- await symlink(- nested,- alias,- process.platform === "win32" ? "junction" : "dir",- );- const temporary = spyOn(os, "tmpdir");+ const temporary = spyOn(os, "tmpdir").mockImplementation(() => {+ throw new Error("No temporary directory is available.");+ });try {- for (const root of [scan, nested, alias]) {- temporary.mockReturnValue(root);- await expect(- inspectPublicationStore(fixture.publication, fixture.environment),- ).rejects.toThrow(/temporary directory must be outside/u);- expect(await readdir(scan)).toEqual(["temporary"]);- expect(await readdir(nested)).toEqual([]);- }+ await expect(+ inspectPublicationStore(fixture.publication, fixture.environment),+ ).resolves.toEqual([]);} finally {temporary.mockRestore();}});- test("forwards cancellation to Python discovery and the workbench and cleans up its input", async () => {+ test.each([+ ["-", "{}", /unexpected or missing fields/u],+ ["broken.json", "{", /broken.json: invalid JSON/u],+ ["array.json", "[]", /array.json: expected a JSON object/u],+ ["nonfinite.json", '{"value": NaN}', /nonfinite.json: invalid JSON/u],+ ] as const)(+ "preserves public publication file input: %s",+ async (filename, contents, diagnostic) => {+ const fixture = await publicationFixture({ createDatabase: false });+ const directory = await temporaryDirectory();+ await writeFile(join(directory, filename), contents);+ const result = runNodePython(+ fixture.python,+ [+ join(PLUGIN_ROOT, "scripts", "workbench_db.py"),+ "inspect-linear-publication",+ "--input-file",+ filename,+ ],+ {+ cwd: directory,+ env: fixture.environment,+ input: "THIS IS NOT JSON AND MUST NOT BE READ",+ },+ );+ expect(result.status).toBe(1);+ expect(result.stdout).toBe("");+ expect(result.stderr).toMatch(diagnostic);+ expect(existsSync(fixture.stateDirectory)).toBe(false);+ },+ );++ test("forwards cancellation to Python discovery and the workbench", async () => {const fixture = await publicationFixture();const controller = new AbortController();const reason = new Error("Synthetic inspection cancellation.");- let inputFile = "";const inspecting = Promise.withResolvers<void>();const python = spyOn(runtime, "resolvePluginPython").mockImplementation(async (options) => {@@ -308,11 +327,13 @@ describe("read-only publication history", () => {},);const workbench = spyOn(runtime, "runWorkbench").mockImplementation(- async (options, args) => {+ async (options, args, input) => {inspecting.resolve();expect(options.signal).toBe(controller.signal);- expect(args[0]).toBe("inspect-linear-publication");- inputFile = args[args.indexOf("--input-file") + 1]!;+ expect(args).toEqual(["inspect-linear-publication"]);+ expect(JSON.parse(input!)).toMatchObject({+ scanId: fixture.publication.scanId,+ });await once(options.signal!, "abort");throw options.signal!.reason;},@@ -326,8 +347,6 @@ describe("read-only publication history", () => {await inspecting.promise;controller.abort(reason);await expect(pending).rejects.toBe(reason);- expect(inputFile).not.toBe("");- expect(existsSync(dirname(inputFile))).toBe(false);} finally {controller.abort(reason);workbench.mockRestore();@@ -718,7 +737,7 @@ connection.close()]);});- test("rejects a missing local scan-history database without creating one", async () => {+ test("rejects missing and non-regular publication history without initializing it", async () => {const fixture = await publicationFixture({ createDatabase: false });await expect(@@ -726,8 +745,46 @@ connection.close()).rejects.toThrow(/scan-history database does not exist/u);expect(existsSync(fixture.stateDirectory)).toBe(false);+ await mkdir(join(fixture.stateDirectory, "workbench.sqlite3"), {+ recursive: true,+ });+ await expect(+ preparePublicationStore(fixture.publication, fixture.environment),+ ).rejects.toThrow(/not a regular file/u);});+ test.skipIf(process.platform === "win32" || process.getuid?.() === 0)(+ "preserves actual filesystem permission errors before opening publication history",+ async () => {+ const fixture = await publicationFixture({ createDatabase: false });+ await mkdir(fixture.stateDirectory, { mode: 0o700 });+ await writeFile(+ join(fixture.stateDirectory, "workbench.sqlite3"),+ "synthetic",+ );+ await chmod(fixture.stateDirectory, 0);+ try {+ for (const operation of [+ inspectPublicationStore,+ preparePublicationStore,+ (+ publication: PreparedScanPublication,+ environment: NodeJS.ProcessEnv,+ ) => recordPublishedIssues(publication, [], environment),+ ]) {+ await expect(+ operation(fixture.publication, fixture.environment),+ ).rejects.toMatchObject({+ code: "EACCES",+ message: expect.stringContaining("workbench.sqlite3"),+ });+ }+ } finally {+ await chmod(fixture.stateDirectory, 0o700);+ }+ },+ );+test("rejects a scan absent from existing local scan history", async () => {const fixture = await publicationFixture({ seedScan: false });diff --git a/sdk/typescript/tests-ts/publish.test.ts b/sdk/typescript/tests-ts/publish.test.tsindex 167734f2a..3815b6def 100644--- a/sdk/typescript/tests-ts/publish.test.ts+++ b/sdk/typescript/tests-ts/publish.test.ts@@ -4,6 +4,8 @@ import { cancelInspection } from "./support/workbench-fakes.js";import { resolving } from "./support/promises.js";import { nodeCommand } from "./support/shell.js";import { randomUUID } from "node:crypto";+import * as filesystem from "node:fs/promises";+import { CodexSecurityError } from "../src/errors.js";import {appendFile,mkdir,@@ -2740,27 +2742,166 @@ describe("connected Linear publication", () => {).toBe(true);});- test("removes source-bearing handoffs when the Codex executable cannot be spawned", async () => {- const publication = preparedPublication();- const persisted = mock(copyPublishedIssues);- const missingExecutable = join(- tmpdir(),- `codex-security-missing-executable-${randomUUID()}`,- );- const injected = dependencies(publication, {- resolveCodex: () => ({ command: missingExecutable }),- runCodex: undefined,- recordPublishedIssues: persisted,- });-- await expect(- publishScanInternal(publication.scanDirectory, OPTIONS, injected),- ).rejects.toThrow("Could not start Codex for Linear publication.");+ test.each(process.platform === "win32" ? ["ENOENT"] : ["ENOENT", "EACCES"])(+ "removes source-bearing handoffs when Codex spawn fails with %s",+ async (code) => {+ const publication = preparedPublication();+ const persisted = mock(copyPublishedIssues);+ const executable = join(await temporaryDirectory(), "unavailable-codex");+ if (code === "EACCES")+ await writeFile(executable, "synthetic", { mode: 0o600 });+ const injected = dependencies(publication, {+ resolveCodex: () => ({ command: executable }),+ runCodex: undefined,+ recordPublishedIssues: persisted,+ });+ let failure: unknown;+ try {+ await publishScanInternal(publication.scanDirectory, OPTIONS, injected);+ } catch (error) {+ failure = error;+ }+ expect(failure).toBeInstanceOf(CodexSecurityError);+ const cause = (failure as Error).cause as NodeJS.ErrnoException;+ expect(cause.code).toBe(code);+ expect((failure as Error).message).toContain(cause.message);+ expect((failure as Error).message).toContain(executable);+ const handoffRoot = publicationHandoffRoot(injected);+ expect(await readdir(handoffRoot)).toEqual([]);+ expect(persisted).not.toHaveBeenCalled();+ },+ );- const handoffRoot = publicationHandoffRoot(injected);- expect(await readdir(handoffRoot)).toEqual([]);- expect(persisted).not.toHaveBeenCalled();- });+ test.each([+ ["initial handoff-only read", 1, false, false],+ ["later handoff-only read", 2, false, false],+ ["initial connector-only read", 1, true, false],+ ["later connector-only read", 2, true, false],+ ["initial read and event-write failure", 1, true, true],+ ["later read and event-write failure", 2, true, true],+ ] as const)(+ "preserves recovery evidence after %s fails",+ async (_name, failedRead, hasEvents, eventWriteFails) => {+ const publication = preparedPublication();+ const persisted = mock(copyPublishedIssues);+ const writeReceipt = mock(async (_receipt: PublishScanResult) => {});+ const originalReadFile = filesystem.readFile;+ const code = failedRead === 1 ? "EACCES" : "EIO";+ const failure = Object.assign(+ new Error(`${code}: could not read synthetic handoff`),+ { code },+ );+ const event = issueEvent(publication.issues[0]!);+ const writeFailure = new Error(+ "Synthetic connector-event storage unavailable.",+ );+ let handoffFile = "",+ reads = 0;+ let restore = () => {};+ let rejected: unknown;+ try {+ await publishScanInternal(+ publication.scanDirectory,+ OPTIONS,+ dependencies(publication, {+ runCodex: async (_command, _args, input) => {+ handoffFile = publicationData(input).handoffFile;+ if (!hasEvents) {+ await writeHandoff(input, [+ handoffRecord(publication, publication.issues[0]!, {+ identifier: "SEC-RECOVERABLE",+ }),+ ]);+ }+ const read = spyOn(filesystem, "readFile").mockImplementation(+ (async (...args: Parameters<typeof originalReadFile>) => {+ if (args[0] === handoffFile && ++reads === failedRead)+ throw failure;+ return originalReadFile(...args);+ }) as typeof originalReadFile,+ );+ restore = () => {+ read.mockRestore();+ };+ return {+ exitCode: 0,+ stdout: hasEvents ? event : "",+ stderr: "",+ };+ },+ ...(eventWriteFails+ ? {+ writeEvents: async () => {+ throw writeFailure;+ },+ }+ : {}),+ recordPublishedIssues: persisted,+ writeReceipt,+ }),+ );+ } catch (error) {+ rejected = error;+ } finally {+ restore();+ }+ expect(rejected).toBeInstanceOf(CodexSecurityError);+ expect((rejected as Error).message).toContain(failure.message);+ expect((rejected as Error).message).toContain(handoffFile);+ expect((rejected as Error).message).toContain(+ "recover it before retrying",+ );+ expect(reads).toBe(failedRead === 1 && hasEvents ? 2 : failedRead);+ const handoffContent = await readFile(handoffFile, "utf8");+ if (hasEvents) {+ if (failedRead === 1) {+ expect(await readJsonLines(handoffFile)).toMatchObject([+ {+ findingId: publication.issues[0]!.findingId,+ issueIdentifier: `SEC-${publication.issues[0]!.findingId.slice(8)}`,+ },+ ]);+ } else {+ expect(handoffContent).toBe("");+ }+ if (eventWriteFails) {+ expect((rejected as Error).message).toContain(writeFailure.message);+ expect((rejected as Error).message).toContain(+ "Could not preserve Linear connector-event evidence",+ );+ } else {+ const eventsFile = await publicationEventsFile(handoffFile);+ expect(await readFile(eventsFile, "utf8")).toBe(`${event}\n`);+ expect((rejected as Error).message).toContain(eventsFile);+ }+ } else {+ expect(handoffContent).toContain("SEC-RECOVERABLE");+ expect(+ (await readdir(dirname(handoffFile))).filter((name) =>+ name.startsWith("events-"),+ ),+ ).toEqual([]);+ }+ expect(+ await readFile(join(dirname(handoffFile), "publication.json"), "utf8"),+ ).toContain("unsafe(input)");+ const causes: unknown[] = [];+ for (let cause = rejected; cause instanceof Error; cause = cause.cause)+ causes.push(cause);+ expect(causes).toContain(failure);+ if (failedRead === 1) {+ expect(writeReceipt).toHaveBeenCalled();+ for (const [receipt] of writeReceipt.mock.calls) {+ expect(receipt.indeterminate).toBe(true);+ expect(receipt.warnings?.join(" ")).toContain(failure.message);+ }+ expect(persisted).toHaveBeenCalledTimes(hasEvents ? 1 : 0);+ } else {+ expect(writeReceipt).not.toHaveBeenCalled();+ expect(persisted).not.toHaveBeenCalled();+ }+ },+ );test("retains handoffs when an injected publisher rejects after a possible mutation", async () => {const publication = preparedPublication();@@ -2773,7 +2914,18 @@ describe("connected Linear publication", () => {identifier: "SEC-RECOVERABLE",}),]);- throw new Error("The publisher failed after a possible mutation.");+ throw new CodexSecurityError(+ "The publisher failed after a possible mutation.",+ {+ cause: Object.assign(+ new Error("EACCES: synthetic publisher spawn failed"),+ {+ syscall: "spawn synthetic-codex",+ code: "EACCES",+ },+ ),+ },+ );},});@@ -3228,9 +3380,13 @@ describe("connected Linear publication", () => {);});- test("does not allow a failing progress observer to stop issue publication", async () => {+ test.each([+ ["throws", throwing("The optional progress display failed.")],+ ["rejects", rejecting("The optional progress display failed.")],+ ["never settles", () => new Promise<void>(() => {})],+ ] as const)("ignores optional progress that %s", async (_name, observer) => {const publication = preparedPublication();- const onProgress = mock(throwing("The optional progress display failed."));+ const onProgress = mock(observer);const result = await publishScanInternal(publication.scanDirectory,{diff --git a/sdk/typescript/tests-ts/runtime.test.ts b/sdk/typescript/tests-ts/runtime.test.tsindex 929b300a4..4261a3012 100644--- a/sdk/typescript/tests-ts/runtime.test.ts+++ b/sdk/typescript/tests-ts/runtime.test.ts@@ -5387,6 +5387,7 @@ describe("runtime directories and plugin Python boundary", () => {"--message=\0high\ud800.low\udfff.pair😀","path-\udcff",],+ ["inspect-linear-publication", "--input-file", "-"],].map((args) => ({ args })),)("runs custom workbench $args without output limits, credentials, or generated bytecode",