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

fix(config): align source attribution with merged settings (#1807)

Michael D'Angelocd8f2a149688
diff --git a/sdk/typescript/src/project-config.ts b/sdk/typescript/src/project-config.ts
index ea85a9825..7cd110a1c 100644
--- a/sdk/typescript/src/project-config.ts
+++ b/sdk/typescript/src/project-config.ts
@@ -314,6 +314,18 @@ export function resolveScanSettings(
file?.codex ?? {},
overrides.codexOverrides ?? {},
);
+ const nativeSourceTree = (
+ value: JsonObject,
+ source: ConfigurationSource,
+ ): JsonObject =>
+ Object.fromEntries(
+ Object.entries(value).map(([key, item]) => [
+ key,
+ item !== null && typeof item === "object" && !Array.isArray(item)
+ ? nativeSourceTree(item, source)
+ : source,
+ ]),
+ );
const recordNativeSources = (
value: JsonObject,
source: ConfigurationSource,
@@ -322,19 +334,24 @@ export function resolveScanSettings(
for (const [key, item] of Object.entries(value)) {
const path: `codex.${string}` = `${prefix}.${key}`;
if (item !== null && typeof item === "object" && !Array.isArray(item)) {
- delete sources[path];
recordNativeSources(item, source, path);
- } else {
- for (const existing of Object.keys(sources) as ProvenanceKey[]) {
- if (existing.startsWith(`${path}.`)) delete sources[existing];
- }
+ } else if (item === source) {
sources[path] = source;
}
}
};
- recordNativeSources(DEFAULT_CODEX_CONFIG, "default");
- recordNativeSources(file?.codex ?? {}, "project");
- recordNativeSources(overrides.codexOverrides ?? {}, "cli");
+ // Match the value merge: defaults beneath the combined project/CLI overrides.
+ const nativeSources = mergeCodexOverrides(
+ nativeSourceTree(DEFAULT_CODEX_CONFIG, "default"),
+ mergeCodexOverrides(
+ nativeSourceTree(file?.codex ?? {}, "project"),
+ nativeSourceTree(overrides.codexOverrides ?? {}, "cli"),
+ ),
+ );
+ // Literal dotted keys can share a flattened name; retain layer precedence.
+ for (const source of ["default", "project", "cli"] as const) {
+ recordNativeSources(nativeSources, source);
+ }
const knowledgeBasePaths =
choose(
"scan.knowledge_base",
diff --git a/sdk/typescript/tests-ts/cli-project-config.test.ts b/sdk/typescript/tests-ts/cli-project-config.test.ts
index 47014d749..aef66479a 100644
--- a/sdk/typescript/tests-ts/cli-project-config.test.ts
+++ b/sdk/typescript/tests-ts/cli-project-config.test.ts
@@ -7,7 +7,11 @@ import { ScanCostTracker } from "../src/cost.js";
import type { ScanTokenUsage } from "../src/index.js";
import { CodexSecurity, type ScanOptions } from "../src/api.js";
import { main } from "../src/cli.js";
-import type { CodexSecurityConfig, JsonObject } from "../src/config.js";
+import {
+ mergedCodexConfig,
+ type CodexSecurityConfig,
+ type JsonObject,
+} from "../src/config.js";
import type { ProjectConfigInput } from "../src/project-config-schema.js";
import { readProjectConfig } from "../src/project-config.js";
import { dependencies, fakeResult, fakeSecurity } from "./cli-fixtures.js";
@@ -1115,6 +1119,48 @@ test("a malformed selected file fails before constructing a client", async () =>
});
});
+test("dry-run reports native defaults restored by a CLI table", async () => {
+ const input = await fixture({
+ codex: { features: { multi_agent_v2: null } },
+ });
+ const onConfig = mock<(config: CodexSecurityConfig) => void>();
+ const { stdout, runCli } = createCliTest(main);
+ expect(
+ await runCli(
+ [
+ "scan",
+ input.repository,
+ "-c",
+ input.config,
+ "--codex",
+ "features.multi_agent_v2.max_concurrent_threads_per_session=2",
+ "--dry-run",
+ "--json",
+ ],
+ dependencies({
+ currentDirectory: input.repository,
+ onConfig,
+ onRun: throwing("Dry-run must not start a scan"),
+ }),
+ ),
+ ).toBe(0);
+ expect(JSON.parse(stdout.text())).toMatchObject({
+ dryRun: true,
+ projectConfig: {
+ sources: {
+ "codex.features.multi_agent_v2.enabled": "default",
+ "codex.features.multi_agent_v2.max_concurrent_threads_per_session":
+ "cli",
+ },
+ },
+ });
+ expect(
+ (await mergedCodexConfig(onConfig.mock.lastCall![0]))["features"],
+ ).toMatchObject({
+ multi_agent_v2: { enabled: true, max_concurrent_threads_per_session: 2 },
+ });
+});
+
test("dry-run uses the real SDK without initializing its runtime and reports provenance", async () => {
const input = await fixture({
scan: {
diff --git a/sdk/typescript/tests-ts/project-config.test.ts b/sdk/typescript/tests-ts/project-config.test.ts
index 5e4ec2aa5..726b71d3b 100644
--- a/sdk/typescript/tests-ts/project-config.test.ts
+++ b/sdk/typescript/tests-ts/project-config.test.ts
@@ -13,6 +13,7 @@ import {
type ProjectConfigInput,
} from "../src/project-config-schema.js";
import { DEFAULT_DEEP_SCAN_SETTINGS } from "../src/deep-scan-defaults.js";
+import { mergedCodexConfig, type JsonValue } from "../src/config.js";
import { ConfigurationError } from "../src/errors.js";
import { createApiTestFixtures } from "./support/temporary-directories.js";
@@ -460,6 +461,98 @@ describe("project configuration resolution", () => {
});
});
+ test.each<{ value: JsonValue; source: "default" | "project" }>([
+ { value: null, source: "default" },
+ { value: false, source: "default" },
+ { value: [], source: "default" },
+ { value: {}, source: "default" },
+ { value: { enabled: true }, source: "project" },
+ ])(
+ "tracks restored native defaults after replacing $value with a CLI table",
+ async ({ value, source }) => {
+ const root = await temporaryDirectory();
+ const input = { codex: { features: { multi_agent_v2: value } } };
+ const original = structuredClone(input);
+ const resolved = resolveScanSettings(
+ { input, directory: root },
+ {
+ codexOverrides: {
+ features: {
+ multi_agent_v2: { max_concurrent_threads_per_session: 2 },
+ },
+ },
+ },
+ root,
+ );
+ expect(
+ (await mergedCodexConfig(resolved.config))["features"],
+ ).toMatchObject({
+ multi_agent_v2: {
+ enabled: true,
+ max_concurrent_threads_per_session: 2,
+ },
+ });
+ expect(resolved.sources).toMatchObject({
+ "codex.features.multi_agent_v2.enabled": source,
+ "codex.features.multi_agent_v2.max_concurrent_threads_per_session":
+ "cli",
+ });
+ expect(
+ Object.hasOwn(resolved.sources, "codex.features.multi_agent_v2"),
+ ).toBe(false);
+ expect(Object.isFrozen(resolved.sources)).toBe(true);
+ expect(input).toEqual(original);
+ },
+ );
+
+ test("preserves literal native keys and profile sources across empty-table and list replacements", async () => {
+ const root = await temporaryDirectory();
+ const { config, sources } = resolveScanSettings(
+ {
+ directory: root,
+ input: {
+ codex: {
+ "setting.with.dot": { keep: true, retained: null, replace: [] },
+ setting: { with: { dot: { keep: true } } },
+ "literal.branch": { leaf: true },
+ synthetic: { old: true },
+ profile: "review",
+ profiles: { review: { model: "gpt-5.6-terra" } },
+ },
+ },
+ },
+ {
+ codexOverrides: {
+ "setting.with.dot": { keep: false, replace: {} },
+ literal: { branch: false },
+ synthetic: [],
+ },
+ },
+ root,
+ );
+ expect(config.codexOverrides["setting.with.dot"]).toEqual({
+ keep: false,
+ retained: null,
+ replace: {},
+ });
+ expect(config.codexOverrides["synthetic"]).toEqual([]);
+ expect(config.codexOverrides["literal.branch"]).toEqual({ leaf: true });
+ expect(config.codexOverrides["literal"]).toEqual({ branch: false });
+ expect(sources).toMatchObject({
+ "codex.setting.with.dot.keep": "cli",
+ "codex.setting.with.dot.retained": "project",
+ "codex.literal.branch.leaf": "project",
+ "codex.literal.branch": "cli",
+ "codex.synthetic": "cli",
+ "codex.profile": "project",
+ "codex.profiles.review.model": "project",
+ });
+ expect(Object.hasOwn(sources, "codex.setting.with.dot.replace")).toBe(
+ false,
+ );
+ expect(Object.hasOwn(sources, "codex.synthetic.old")).toBe(false);
+ });
+
test("ignores valid inactive deep defaults but rejects explicit deep CLI options in standard mode", async () => {
const root = await temporaryDirectory();
const project = {