Skip to content

OpenAI's Codex Security CLI and TypeScript SDK for finding, validating, and fixing security vulnerabilities. npm: https://www.npmjs.com/package/@openai/codex-security

refactor(sdk): share saved scan recipe settings across registration and replay (#2477)

Michael D'Angelo6c17006870a6
diff --git a/sdk/typescript/src/api.ts b/sdk/typescript/src/api.ts
index 87541bbb6..d4f7b6336 100644
--- a/sdk/typescript/src/api.ts
+++ b/sdk/typescript/src/api.ts
@@ -203,7 +203,6 @@ import {
type TurnResultMetadata,
type ScanResultOptions,
} from "./result.js";
-import type { SeverityLevel } from "./models.js";
import {
formatSecurityPolicyText,
inspectSecurityPolicySources,
@@ -2739,14 +2738,9 @@ export class CodexSecurity {
repositoryRevision: expectation.repositoryRevision,
pluginVersion: runtime.plugin.version,
config: recipeConfig,
- failOnSeverity: options.failureSeverity,
+ options,
knowledgeBasePaths: knowledgeBase?.sources,
- maxCostUsd: options.maxCostUsd,
deepScan: deepScanConfiguration?.settings,
- maxTimeMinutes: options.maxTimeMinutes,
- auth: options.auth,
- cyberAccessProgram: options.cyberAccessProgram,
- ownerSuggestions: options.ownerSuggestions,
});
if (savedProviderProfile !== undefined) {
recipe["providerProfile"] = {
@@ -5237,13 +5231,8 @@ export class CodexSecurity {
repositoryRevision: revision,
pluginVersion: plugin.version,
config: { model },
- failOnSeverity: options.failureSeverity,
+ options,
knowledgeBasePaths: options.knowledgeBasePaths,
- maxCostUsd: options.maxCostUsd,
- maxTimeMinutes: options.maxTimeMinutes,
- auth: options.auth,
- cyberAccessProgram: options.cyberAccessProgram,
- ownerSuggestions: options.ownerSuggestions,
}),
mock: true,
},
@@ -6222,14 +6211,9 @@ function scanRecipe({
repositoryRevision,
pluginVersion,
config,
- failOnSeverity,
+ options,
knowledgeBasePaths,
- maxCostUsd,
- maxTimeMinutes,
deepScan,
- auth,
- cyberAccessProgram,
- ownerSuggestions,
}: {
repository: string;
target: NormalizedTarget;
@@ -6237,15 +6221,19 @@ function scanRecipe({
repositoryRevision: string | null;
pluginVersion: string;
config: JsonObject;
- failOnSeverity?: SeverityLevel;
+ options: ScanSettings;
knowledgeBasePaths?: string[];
- maxCostUsd?: number;
- maxTimeMinutes?: number;
deepScan?: Required<DeepScanOptions>;
- auth?: ScanAuthMode;
- cyberAccessProgram?: ScanSettings["cyberAccessProgram"];
- ownerSuggestions?: boolean;
}): JsonObject {
+ const optionalSettings = {
+ ownerSuggestions: options.ownerSuggestions,
+ auth: options.auth,
+ cyberAccessProgram: options.cyberAccessProgram,
+ failOnSeverity: options.failureSeverity,
+ knowledgeBasePaths,
+ maxCostUsd: options.maxCostUsd,
+ maxTimeMinutes: options.maxTimeMinutes,
+ } satisfies Partial<JsonObject>;
return {
repository,
target: { ...target, paths: [...target.paths] },
@@ -6253,13 +6241,11 @@ function scanRecipe({
...(repositoryRevision === null ? {} : { repositoryRevision }),
pluginVersion,
config,
- ...(ownerSuggestions === undefined ? {} : { ownerSuggestions }),
- ...(auth === undefined ? {} : { auth }),
- ...(cyberAccessProgram === undefined ? {} : { cyberAccessProgram }),
- ...(failOnSeverity === undefined ? {} : { failOnSeverity }),
- ...(knowledgeBasePaths === undefined ? {} : { knowledgeBasePaths }),
- ...(maxCostUsd === undefined ? {} : { maxCostUsd }),
- ...(maxTimeMinutes === undefined ? {} : { maxTimeMinutes }),
+ ...Object.fromEntries(
+ Object.entries(optionalSettings).filter(
+ ([, value]) => value !== undefined,
+ ),
+ ),
...(deepScan === undefined
? {}
: { deepScan: { ...deepScan }, deepScanResolved: true }),
diff --git a/sdk/typescript/src/cli.ts b/sdk/typescript/src/cli.ts
index 651de2285..7d7f6e537 100644
--- a/sdk/typescript/src/cli.ts
+++ b/sdk/typescript/src/cli.ts
@@ -64,6 +64,7 @@ import {
stripVTControlCharacters,
} from "node:util";
import { Cli, Formatter, z } from "incur";
+import type { ZodType } from "zod";
import stringWidth from "string-width";
import Ajv2020, { type ValidateFunction } from "ajv/dist/2020.js";
import { formatCliHelp } from "./cli-help.js";
@@ -8031,36 +8032,29 @@ async function prepareScanArgumentsFromRecipe(
"The saved scan recipe has an invalid Git head.",
);
}
- const threshold = ScanSettingsSchema.shape.failureSeverity.safeParse(
- recipe["failOnSeverity"],
+ const fields = ScanSettingsSchema.shape;
+ const readSetting = <T>(key: string, schema: ZodType<T>, label: string) => {
+ const result = schema.safeParse(recipe[key]);
+ if (!result.success)
+ throw new CodexSecurityError(
+ `The saved scan recipe contains an invalid ${label}.`,
+ );
+ return result.data;
+ };
+ const failureSeverity = readSetting(
+ "failOnSeverity",
+ fields.failureSeverity,
+ "severity policy",
+ );
+ const maxCostUsd = readSetting("maxCostUsd", fields.maxCostUsd, "cost limit");
+ const maxTimeMinutes = readSetting(
+ "maxTimeMinutes",
+ fields.maxTimeMinutes,
+ "investigation time limit",
);
- if (!threshold.success) {
- throw new CodexSecurityError(
- "The saved scan recipe contains an invalid severity policy.",
- );
- }
- const maxCostUsd = recipe["maxCostUsd"];
- if (
- maxCostUsd !== undefined &&
- (typeof maxCostUsd !== "number" ||
- !Number.isFinite(maxCostUsd) ||
- maxCostUsd <= 0)
- ) {
- throw new CodexSecurityError(
- "The saved scan recipe contains an invalid cost limit.",
- );
- }
const deepScan = DeepScanSettingsSchema.optional().safeParse(
recipe["deepScan"],
);
- const maxTimeMinutes = ScanSettingsSchema.shape.maxTimeMinutes.safeParse(
- recipe["maxTimeMinutes"],
- );
- if (!maxTimeMinutes.success) {
- throw new CodexSecurityError(
- "The saved scan recipe contains an invalid investigation time limit.",
- );
- }
if (!deepScan.success) {
throw new CodexSecurityError(
"The saved scan recipe contains invalid deep scan settings.",
@@ -8074,27 +8068,17 @@ async function prepareScanArgumentsFromRecipe(
"The saved scan recipe is missing resolved deep scan settings.",
);
}
- const ownerSuggestions = ScanSettingsSchema.shape.ownerSuggestions.safeParse(
- recipe["ownerSuggestions"],
+ const ownerSuggestions = readSetting(
+ "ownerSuggestions",
+ fields.ownerSuggestions,
+ "owner suggestions setting",
);
- if (!ownerSuggestions.success)
- throw new CodexSecurityError(
- "The saved scan recipe contains an invalid owner suggestions setting.",
- );
- const auth = z.enum(SCAN_AUTH_MODES).optional().safeParse(recipe["auth"]);
- const cyberAccessProgram =
- ScanSettingsSchema.shape.cyberAccessProgram.safeParse(
- recipe["cyberAccessProgram"],
- );
- if (!cyberAccessProgram.success) {
- throw new CodexSecurityError(
- "The saved scan recipe contains an invalid Cyber access program.",
- );
- }
- if (!auth.success)
- throw new CodexSecurityError(
- "The saved scan recipe contains an invalid authentication choice.",
- );
+ const cyberAccessProgram = readSetting(
+ "cyberAccessProgram",
+ fields.cyberAccessProgram,
+ "Cyber access program",
+ );
+ const auth = readSetting("auth", fields.auth, "authentication choice");
if (
mode !== "deep" &&
deepScan.data !== undefined &&
@@ -8135,9 +8119,9 @@ async function prepareScanArgumentsFromRecipe(
);
return {
repository,
- auth: auth.data ?? DEFAULT_SCAN_AUTH,
- cyberAccessProgram: cyberAccessProgram.data,
- ownerSuggestions: ownerSuggestions.data,
+ auth: auth ?? DEFAULT_SCAN_AUTH,
+ cyberAccessProgram,
+ ownerSuggestions,
...(severityRubric === undefined ? {} : { severityRubric }),
// Saved scopes contain literal repository names, not home-relative inputs.
target:
@@ -8164,9 +8148,9 @@ async function prepareScanArgumentsFromRecipe(
codexOverrides: Object.hasOwn(replayConfig, "approval_policy")
? replayConfig
: { ...replayConfig, approval_policy: "never" },
- failureSeverity: threshold.data,
+ failureSeverity,
maxCostUsd,
- maxTimeMinutes: maxTimeMinutes.data,
+ maxTimeMinutes,
dryRun: false,
...(recipe["mock"] === true ? { mock: true } : {}),
expectedPluginVersion: