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
refactor(sdk): consolidate internal runtime helpers (#1230)
mldangelo-oai2f1b02136722
diff --git a/sdk/typescript/dashboard/App.tsx b/sdk/typescript/dashboard/App.tsxindex ff1090bd0..fe3824390 100644--- a/sdk/typescript/dashboard/App.tsx+++ b/sdk/typescript/dashboard/App.tsx@@ -401,10 +401,6 @@ export function App() {});setOffset(0);}- const counts: Record<DashboardView, number | undefined> = {- findings: overview?.findings,- groups: overview?.groups,- };return (<div className="dashboard-shell"><header className="app-header">@@ -434,7 +430,7 @@ export function App() {onClick={() => navigate(tab.id)}><span>{tab.label}</span>- <span className="nav-count">{count(counts[tab.id])}</span>+ <span className="nav-count">{count(overview?.[tab.id])}</span></Button>))}</nav>diff --git a/sdk/typescript/src/api.ts b/sdk/typescript/src/api.tsindex 7cdf5310e..7e10813a5 100644--- a/sdk/typescript/src/api.ts+++ b/sdk/typescript/src/api.ts@@ -172,6 +172,7 @@ import { CODEX_SECURITY_THREAD_SOURCES } from "./thread-source.js";import { CODEX_EXECUTABLE_VERSION, CODEX_SDK_VERSION } from "./version.js";import { bundledCodexSdkEnvironment } from "./codex-sdk-environment.js";import {+ canonicalConfigPath,acquireCodexSecurityCredentialHomeLock,bootstrapPlugin,bundledPluginRoot,@@ -3369,21 +3370,7 @@ export class CodexSecurity {const stateDirectory = codexSecurityStateDirectory(this.#dependencies.environment,);- let canonicalStateDirectory = stateDirectory;- while (true) {- try {- canonicalStateDirectory = join(- await realpath(canonicalStateDirectory),- relative(canonicalStateDirectory, stateDirectory),- );- break;- } catch (error) {- if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error;- const parent = dirname(canonicalStateDirectory);- if (parent === canonicalStateDirectory) throw error;- canonicalStateDirectory = parent;- }- }+ const canonicalStateDirectory = await canonicalConfigPath(stateDirectory);requireOutputOutsideRepositories(protectedRoots, canonicalStateDirectory);return {repository: repo,diff --git a/sdk/typescript/src/bulk-scan-discovery.ts b/sdk/typescript/src/bulk-scan-discovery.tsindex 5a832b9c6..dca5d8825 100644--- a/sdk/typescript/src/bulk-scan-discovery.ts+++ b/sdk/typescript/src/bulk-scan-discovery.ts@@ -325,7 +325,7 @@ async function validateWizardOutput(outputDir: string): Promise<void> {}}-function createTerminalPrompt(output: PromptOutput): BulkScanPrompt {+export function createTerminalPrompt(output: PromptOutput): BulkScanPrompt {const context = (signal?: AbortSignal) => {const stream = new Writable({write(chunk: Buffer, _encoding, callback) {diff --git a/sdk/typescript/src/cli.ts b/sdk/typescript/src/cli.tsindex 6a2a6d0b7..513526431 100644--- a/sdk/typescript/src/cli.ts+++ b/sdk/typescript/src/cli.ts@@ -109,6 +109,7 @@ import {} from "./cloud-publish.js";import {createBulkScanDiscoveryDependencies,+ createTerminalPrompt,runBulkScanWizard,type BulkScanDiscoveryDependencies,type BulkScanPrompt,@@ -337,6 +338,22 @@ const PLUGIN_PATH_DESCRIPTION ="Codex Security plugin directory or ZIP (default: bundled plugin).";const PYTHON_PATH_DESCRIPTION ="Python interpreter (default: PYTHON or automatic discovery).";+const RUNTIME_OPTION_SCHEMAS = {+ pluginPath: optionValue("--plugin-path")+ .optional()+ .describe(PLUGIN_PATH_DESCRIPTION),+ python: optionValue("--python").optional().describe(PYTHON_PATH_DESCRIPTION),+ codex: z+ .array(optionValue("--codex"))+ .default([])+ .describe(CODEX_OVERRIDE_DESCRIPTION),+};+const SKILL_AUTH_OPTION = z+ .enum(SCAN_AUTH_MODES)+ .default("auto")+ .describe(+ "Select ChatGPT, OPENAI_API_KEY/CODEX_API_KEY, or automatic authentication.",+ );const PROJECT_CONFIG_OPTION = optionValue("--config").optional().describe(@@ -2774,12 +2791,7 @@ export async function main(scanDir === undefined ? "scan" : basename(scanDir);if (scanDir === undefined) {const prompt =- dependencies.publishPrompt ??- createBulkScanDiscoveryDependencies({- output: errorOutput,- now: dependencies.now,- currentDirectory: dependencies.currentDirectory,- }).prompt;+ dependencies.publishPrompt ?? createTerminalPrompt(errorOutput);if (!prompt.isInteractive()) {throw new CodexSecurityError(`Interactive scan selection requires a terminal. Select a saved scan: codex-security publish scan --scan SCAN_ID --to ${options.to}${options.to === "linear" ? " --linear-team TEAM_ID" : ""}.`,@@ -3291,16 +3303,7 @@ export async function main(.describe("Stop when estimated total USD cost across all three stages exceeds AMOUNT.",),- pluginPath: optionValue("--plugin-path")- .optional()- .describe(PLUGIN_PATH_DESCRIPTION),- python: optionValue("--python")- .optional()- .describe(PYTHON_PATH_DESCRIPTION),- codex: z- .array(optionValue("--codex"))- .default([])- .describe(CODEX_OVERRIDE_DESCRIPTION),+ ...RUNTIME_OPTION_SCHEMAS,}),examples: [{ args: { repository: "." } },@@ -3386,11 +3389,7 @@ export async function main(),prompt:dependencies.policyPrompt ??- createBulkScanDiscoveryDependencies({- output: errorOutput,- now: dependencies.now,- currentDirectory: dependencies.currentDirectory,- }).prompt,+ createTerminalPrompt(errorOutput),environment: dependencies.environment,errorOutput,now: dependencies.now,@@ -3525,16 +3524,7 @@ export async function main(.boolean().default(false).describe("Archive existing results; requires --output-dir."),- pluginPath: optionValue("--plugin-path")- .optional()- .describe(PLUGIN_PATH_DESCRIPTION),- python: optionValue("--python")- .optional()- .describe(PYTHON_PATH_DESCRIPTION),- codex: z- .array(optionValue("--codex"))- .default([])- .describe(CODEX_OVERRIDE_DESCRIPTION),+ ...RUNTIME_OPTION_SCHEMAS,failOnSeverity: FailureSeveritySchema.optional().describe("Exit 1 for findings at or above LEVEL.",),@@ -4136,16 +4126,7 @@ export async function main("Stop each component scan if estimated USD cost exceeds AMOUNT.",),showCost: SHOW_COST_OPTION,- pluginPath: optionValue("--plugin-path")- .optional()- .describe(PLUGIN_PATH_DESCRIPTION),- python: optionValue("--python")- .optional()- .describe(PYTHON_PATH_DESCRIPTION),- codex: z- .array(optionValue("--codex"))- .default([])- .describe(CODEX_OVERRIDE_DESCRIPTION),+ ...RUNTIME_OPTION_SCHEMAS,}).refine((options) =>@@ -4780,12 +4761,7 @@ export async function main(.describe("Finding text or a file containing findings."),}),options: z.object({- auth: z- .enum(SCAN_AUTH_MODES)- .default("auto")- .describe(- "Select ChatGPT, OPENAI_API_KEY/CODEX_API_KEY, or automatic authentication.",- ),+ auth: SKILL_AUTH_OPTION,...SKILL_CONFIG_OPTIONS.shape,}),async run({ options }) {@@ -4818,12 +4794,7 @@ export async function main(.describe("Finding text, a file, or a saved finding identifier."),}),options: z.object({- auth: z- .enum(SCAN_AUTH_MODES)- .default("auto")- .describe(- "Select ChatGPT, OPENAI_API_KEY/CODEX_API_KEY, or automatic authentication.",- ),+ auth: SKILL_AUTH_OPTION,...MODEL_OPTIONS.shape,scan: optionValue("--scan").optional()@@ -5034,12 +5005,7 @@ export async function main(.describe("Issue text, a file, or a saved finding identifier."),}),options: z.object({- auth: z- .enum(SCAN_AUTH_MODES)- .default("auto")- .describe(- "Select ChatGPT, OPENAI_API_KEY/CODEX_API_KEY, or automatic authentication.",- ),+ auth: SKILL_AUTH_OPTION,...MODEL_OPTIONS.shape,externalSandbox: z.boolean()@@ -8005,12 +7971,7 @@ async function chooseInteractiveAuthentication();if (authentication.method !== "api_key") return auth;const prompt =- dependencies.scanAuthenticationPrompt ??- createBulkScanDiscoveryDependencies({- output: errorOutput,- now: dependencies.now,- currentDirectory: dependencies.currentDirectory,- }).prompt;+ dependencies.scanAuthenticationPrompt ?? createTerminalPrompt(errorOutput);const hasStoredSignIn = dependencies.hasStoredChatGPTSignIn;if (!prompt.isInteractive() ||@@ -8801,11 +8762,7 @@ async function executeScan(arguments_.patch ||(await (dependencies.confirmPatchReview ??- createBulkScanDiscoveryDependencies({- output: errorOutput,- now: dependencies.now,- currentDirectory: dependencies.currentDirectory,- }).prompt.confirm+ createTerminalPrompt(errorOutput).confirm)("Review and patch these findings?"));if (confirmed) {const selectPatches =diff --git a/sdk/typescript/src/cloud-publish.ts b/sdk/typescript/src/cloud-publish.tsindex bbde97ed6..44b161ccf 100644--- a/sdk/typescript/src/cloud-publish.ts+++ b/sdk/typescript/src/cloud-publish.ts@@ -165,9 +165,6 @@ async function publishCloudPayload(findings: Finding[],dependencies: CloudPublicationDependencies,): Promise<CloudPublicationResult> {- if (findings.length === 0) {- throw new CodexSecurityError("There are no findings to publish.");- }dependencies.signal?.throwIfAborted();if (dependencies.dryRun) {return {diff --git a/sdk/typescript/src/contract.ts b/sdk/typescript/src/contract.tsindex 72f139e02..1d2780454 100644--- a/sdk/typescript/src/contract.ts+++ b/sdk/typescript/src/contract.ts@@ -288,11 +288,12 @@ export function normalizePersistedFindings(payload: unknown): unknown {normalizeLegacyStringLists(validation, ["evidence"]);}removeUnsupportedLegacyStrings(validation, ["method", "summary"]);- removeUnsupportedLegacyNullableStrings(validation, [- "status",- "disposition",- "result",- ]);+ removeUnsupportedLegacyStrings(+ validation,+ ["status", "disposition", "result"].filter(+ (field) => validation[field] !== null,+ ),+ );}const attackPath = finding["attackPath"];@@ -373,21 +374,6 @@ function removeUnsupportedLegacyStrings(}}-function removeUnsupportedLegacyNullableStrings(- section: JsonRecord,- fields: string[],-): void {- for (const field of fields) {- if (- field in section &&- section[field] !== null &&- (typeof section[field] !== "string" || section[field].length === 0)- ) {- delete section[field];- }- }-}-function validateCanonicalContract(manifest: ScanManifest,findings: FindingsDocument,diff --git a/sdk/typescript/src/cost.ts b/sdk/typescript/src/cost.tsindex fcbf4ced2..c5a207999 100644--- a/sdk/typescript/src/cost.ts+++ b/sdk/typescript/src/cost.ts@@ -242,21 +242,19 @@ export class ScanCostTracker {}}}- let changed = true;- while (changed) {- changed = false;+ let previousSize: number;+ do {+ previousSize = included.size;for (const session of this.#sessions.values()) {if (session.threadId !== null &&session.parentThreadId !== null &&- included.has(session.parentThreadId) &&- !included.has(session.threadId)+ included.has(session.parentThreadId)) {included.add(session.threadId);- changed = true;}}- }+ } while (included.size !== previousSize);for (const { session, error } of unreadable) {if (included.has(session.threadId!)) throw error;}diff --git a/sdk/typescript/src/deep-config.ts b/sdk/typescript/src/deep-config.tsindex 7fb83291c..5b95ccf49 100644--- a/sdk/typescript/src/deep-config.ts+++ b/sdk/typescript/src/deep-config.ts@@ -1,7 +1,8 @@-import { lstat, readFile, realpath } from "node:fs/promises";-import { basename, dirname, join, resolve } from "node:path";+import { lstat, readFile } from "node:fs/promises";+import { basename, dirname, join } from "node:path";import { parse as parseToml, type TomlTable } from "smol-toml";import { writeCodexConfig, type JsonObject } from "./config.js";+import { canonicalConfigPath } from "./runtime.js";import { DEFAULT_DEEP_SCAN_SETTINGS } from "./deep-scan-defaults.js";import { CodexSecurityError } from "./errors.js";import {@@ -166,25 +167,6 @@ async function runtimeConfigPath(path: string): Promise<string> {}}-async function canonicalConfigPath(path: string): Promise<string> {- let existing = resolve(path);- const missing: string[] = [];- while (true) {- try {- return join(await realpath(existing), ...missing);- } catch (error) {- const parent = dirname(existing);- if (- (error as NodeJS.ErrnoException).code !== "ENOENT" ||- parent === existing- )- throw error;- missing.unshift(basename(existing));- existing = parent;- }- }-}-async function readDeepScanDocument(source: string,signal?: AbortSignal,diff --git a/sdk/typescript/src/deep-progress.ts b/sdk/typescript/src/deep-progress.tsindex 20aad58d1..574de2740 100644--- a/sdk/typescript/src/deep-progress.ts+++ b/sdk/typescript/src/deep-progress.ts@@ -19,16 +19,15 @@ export class DeepScanProgressTracker {readonly #options: DeepScanProgressTrackerOptions;#timer: NodeJS.Timeout | null = null;#pending: Promise<void> | null = null;- #abortController: AbortController | null = null;+ readonly #abortController = new AbortController();#lastProgress: DeepScanProgress | null = null;- #stopped = false;public constructor(options: DeepScanProgressTrackerOptions) {this.#options = options;}public start(): void {- if (this.#timer !== null || this.#stopped) return;+ if (this.#timer !== null || this.#abortController.signal.aborted) return;const poll = () => {void this.refresh().catch((error: unknown) => {this.#options.onError?.(error);@@ -40,10 +39,9 @@ export class DeepScanProgressTracker {}public async refresh(): Promise<void> {- if (this.#stopped) return;+ if (this.#abortController.signal.aborted) return;if (this.#pending !== null) return await this.#pending;- const abortController = new AbortController();- this.#abortController = abortController;+ const abortController = this.#abortController;let update: Promise<void> | null = null;update = (async () => {try {@@ -51,7 +49,6 @@ export class DeepScanProgressTracker {await this.#options.read(abortController.signal),);if (- this.#stopped ||abortController.signal.aborted ||progress === null ||sameProgress(progress, this.#lastProgress)@@ -61,13 +58,10 @@ export class DeepScanProgressTracker {this.#lastProgress = progress;this.#options.onProgress(progress);} catch (error) {- if (this.#stopped || abortController.signal.aborted) return;+ if (abortController.signal.aborted) return;throw error;} finally {if (this.#pending === update) this.#pending = null;- if (this.#abortController === abortController) {- this.#abortController = null;- }}})();this.#pending = update;@@ -75,12 +69,10 @@ export class DeepScanProgressTracker {}public stop(): void {- if (this.#stopped) return;- this.#stopped = true;+ if (this.#abortController.signal.aborted) return;clearInterval(this.#timer ?? undefined);this.#timer = null;- this.#abortController?.abort();- this.#abortController = null;+ this.#abortController.abort();}}diff --git a/sdk/typescript/src/finding-catalogue.ts b/sdk/typescript/src/finding-catalogue.tsindex 8ae390d46..aab2a800c 100644--- a/sdk/typescript/src/finding-catalogue.ts+++ b/sdk/typescript/src/finding-catalogue.ts@@ -13,17 +13,7 @@ export function groupFindings(knownFindingGroups: readonly (readonly string[])[] = [],occurrenceGroups: readonly (readonly string[])[] = [],): ComparisonFinding[][] {- const parents = new Map<string, string>();- const root = (value: string): string => {- const path: string[] = [];- let current = value;- while (parents.has(current)) {- path.push(current);- current = parents.get(current)!;- }- for (const item of path) parents.set(item, current);- return current;- };+ const { parents, root } = findingGroupRoots();const link = (first: string, second: string): void => {const previous = root(first);const current = root(second);@@ -52,15 +42,11 @@ export function groupFindings(}}- const groups = new Map<string, ComparisonFinding[]>();- for (const finding of findings) {- const key = root(`occurrence:${finding.occurrenceId}`);- const group = groups.get(key);- if (group === undefined) groups.set(key, [finding]);- else group.push(finding);- }-- return [...groups.values()];+ return [+ ...Map.groupBy(findings, (finding) =>+ root(`occurrence:${finding.occurrenceId}`),+ ).values(),+ ];}export function findingCatalogue(@@ -187,3 +173,20 @@ function present(value: Record<string, unknown>): Record<string, unknown> {),);}++/** @internal */+export function findingGroupRoots() {+ const parents = new Map<string, string>();+ const root = (identity: string): string => {+ if (!parents.has(identity)) parents.set(identity, identity);+ const path: string[] = [];+ let current = identity;+ while (parents.get(current) !== current) {+ path.push(current);+ current = parents.get(current)!;+ }+ for (const item of path) parents.set(item, current);+ return current;+ };+ return { parents, root };+}diff --git a/sdk/typescript/src/runtime.ts b/sdk/typescript/src/runtime.tsindex 19cd3f8a9..f095d1827 100644--- a/sdk/typescript/src/runtime.ts+++ b/sdk/typescript/src/runtime.ts@@ -1840,15 +1840,26 @@ export async function canonicalizeModelSafePath(): Promise<string> {const path = resolve(expandHome(input));requireModelSafeOutputDir(path);- for (let ancestor = path; ; ancestor = dirname(ancestor)) {+ const canonical = await canonicalConfigPath(path);+ requireModelSafeOutputDir(canonical);+ return canonical;+}++export async function canonicalConfigPath(path: string): Promise<string> {+ let existing = resolve(path);+ const missing: string[] = [];+ while (true) {try {- const canonicalAncestor = resolve(ancestor, await realpath(ancestor));- const canonical = resolve(canonicalAncestor, relative(ancestor, path));- requireModelSafeOutputDir(canonical);- return canonical;+ return join(await realpath(existing), ...missing);} catch (error) {- if (nodeErrorCode(error) !== "ENOENT") throw error;- if (dirname(ancestor) === ancestor) throw error;+ const parent = dirname(existing);+ if (+ (error as NodeJS.ErrnoException).code !== "ENOENT" ||+ parent === existing+ )+ throw error;+ missing.unshift(basename(existing));+ existing = parent;}}}diff --git a/sdk/typescript/src/scan-comparison.ts b/sdk/typescript/src/scan-comparison.tsindex b092be417..42d4cb2c2 100644--- a/sdk/typescript/src/scan-comparison.ts+++ b/sdk/typescript/src/scan-comparison.ts@@ -36,6 +36,7 @@ import {compactFinding,findingCatalogue,groupFindings,+ findingGroupRoots,type ComparisonFinding as Finding,} from "./finding-catalogue.js";import {@@ -108,40 +109,20 @@ export interface ScanComparisonResult {export function unionFindingGroups(groups: readonly (readonly string[])[],): string[][] {- const parents = new Map<string, string>();- const representative = (identity: string): string => {- let root = identity;- while (parents.get(root) !== root) root = parents.get(root)!;- let current = identity;- while (parents.get(current) !== current) {- const previous = parents.get(current)!;- parents.set(current, root);- current = previous;- }- return root;- };+ const { parents, root: representative } = findingGroupRoots();for (const group of groups) {const [first, ...rest] = group.filter((identity) => identity.trim().length > 0,);if (first === undefined) continue;- if (!parents.has(first)) parents.set(first, first);const firstRoot = representative(first);for (const identity of rest) {- if (!parents.has(identity)) parents.set(identity, identity);parents.set(representative(identity), firstRoot);}}- const united = new Map<string, string[]>();- for (const identity of parents.keys()) {- const root = representative(identity);- const group = united.get(root);- if (group === undefined) united.set(root, [identity]);- else group.push(identity);- }- return [...united.values()];+ return [...Map.groupBy(parents.keys(), representative).values()];}/** @internal */diff --git a/sdk/typescript/src/scan-logs.ts b/sdk/typescript/src/scan-logs.tsindex 81a42daa3..efa6553b1 100644--- a/sdk/typescript/src/scan-logs.ts+++ b/sdk/typescript/src/scan-logs.ts@@ -138,8 +138,7 @@ export async function readScanLogs(options: ScanLogOptions) {// A Desktop owner can contain other work. Include its log without treating// the whole conversation tree as part of this scan.const traversed = new Set(options.executionThreadIds ?? included);- const pending = [...traversed];- for (const parentId of pending) {+ for (const parentId of traversed) {const parent = logs.get(parentId)?.[0];for (const [session] of logs.values()) {if (@@ -152,7 +151,6 @@ export async function readScanLogs(options: ScanLogOptions) {) {included.add(session.threadId);traversed.add(session.threadId);- pending.push(session.threadId);}}}