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(deep-scan): preserve independently fixable findings across batches and resumes (#2311)
Michael D'Angelof7494f6dd06d
diff --git a/plugins/codex-security/mcp-app/src/deep-scan/iteration.ts b/plugins/codex-security/mcp-app/src/deep-scan/iteration.tsindex 7ab6d82e4..745669aae 100644--- a/plugins/codex-security/mcp-app/src/deep-scan/iteration.ts+++ b/plugins/codex-security/mcp-app/src/deep-scan/iteration.ts@@ -1,3 +1,4 @@+import { isDeepStrictEqual } from "node:util";import {scanDraftInputSchema,type ScanDraftInput,@@ -70,6 +71,10 @@ function report(finding: Finding, retained: boolean) {identity: finding.identity,title: finding.title,fix: finding.remediation,+ validation: finding.validation,+ attackPath: finding.attackPath,+ rootCause: finding.rootCause,+ codeEvidenceIds: finding.codeEvidence?.map(({ id }) => id),locations: (locations ?? finding.locations).map(({ path, startLine, endLine, role }) =>[path, startLine, endLine, role] as const,@@ -125,6 +130,86 @@ function repairKeys(response: string, records: ReturnType<typeof report>[]) {return keys;}+function equalRepairs(a?: number[], b?: number[]) {+ return (+ !!a && !!b && a.length === b.length && a.every((key, i) => key === b[i])+ );+}++function contextWindowExceeded(error: unknown) {+ return errorMessage(error).startsWith(+ "Codex ran out of room in the model's context window.",+ );+}++async function compareRecords(+ records: ComparisonReport[],+ compare: CompareReports,+) {+ const rows = records.map((record, id) => ({ id, ...record }));+ return repairKeys(+ await compare(+ instructions + "\n" + JSON.stringify(rows),+ duplicateResponseSchema,+ ),+ records,+ );+}++// A repair must cite a file in both reports. After the model rejects the full+// batch, compare new reports only with earlier reports that can share a repair.+async function compareWithinContext(+ records: ComparisonReport[],+ exact: string[],+ previous: number,+ compare: CompareReports,+ matches: Map<number, Set<number>>,+) {+ const byFile = new Map<string, number[]>();+ const seen = new Set<string>();+ for (const [id, record] of records.entries()) {+ if (seen.has(exact[id]!)) continue;+ seen.add(exact[id]!);+ const files = new Set(+ [...record.locations, ...record.evidence].flatMap(([path]) =>+ path ? [path] : [],+ ),+ );+ if (id >= previous) {+ const candidates = [+ ...new Set([...files].flatMap((path) => byFile.get(path) ?? [])),+ ];+ async function check(ids: number[]): Promise<void> {+ try {+ const keys = await compareRecords(+ [record, ...ids.map((i) => records[i]!)],+ compare,+ );+ for (const [i, other] of ids.entries()) {+ if (equalRepairs(keys.get(0), keys.get(i + 1))) {+ const earlier = matches.get(id) ?? new Set<number>();+ earlier.add(other);+ matches.set(id, earlier);+ }+ }+ } catch (error) {+ if (!contextWindowExceeded(error)) throw error;+ if (ids.length === 1) return; // Even this pair cannot fit. Retain both reports.+ const mid = Math.ceil(ids.length / 2);+ await check(ids.slice(0, mid));+ await check(ids.slice(mid));+ }+ }+ if (candidates.length) await check(candidates);+ }+ for (const path of files) {+ const ids = byFile.get(path) ?? [];+ ids.push(id);+ byFile.set(path, ids);+ }+ }+}+function hasAdditionalSink(finding: ReturnType<typeof report>,other: ReturnType<typeof report>,@@ -183,7 +268,7 @@ function differentWorkerSites() {const left = asRecord(a.identity)?.anchor;const right = asRecord(b.identity)?.anchor;- return Boolean(left && right && left !== right && a.ruleId !== b.ruleId);+ return Boolean(left && right && left !== right);}// A source can identify a second missing control in a different file, such as@@ -212,7 +297,7 @@ function sourcesNeedSeparateReports(a: Finding, b: Finding) {const source = left.finding as Finding;return b.provenance.sourceFindings?.some((right, j) => {const other = right.finding as Finding;- if (right.id.startsWith(worker) && differentWorkerSites(source, other))+ if (distinctIdentity(source, other, right.id.startsWith(worker)))return true;return (separateControls(source, other) ||@@ -251,16 +336,17 @@ function distinctIdentity(function addLocations(finding: Finding, source: Pick<Finding, "locations">) {for (const location of source.locations) {- if (- !finding.locations.some(- (l) =>- l.path === location.path &&- l.startLine === location.startLine &&- (l.endLine ?? l.startLine) ===- (location.endLine ?? location.startLine),- )- )- finding.locations.push(structuredClone(location));+ const sameRange = finding.locations.filter(+ (l) =>+ l.path === location.path &&+ l.startLine === location.startLine &&+ (l.endLine ?? l.startLine) === (location.endLine ?? location.startLine),+ );+ if (sameRange.some((l) => !location.role || l.role === location.role))+ continue;+ const unclassified = sameRange.find((l) => !l.role);+ if (unclassified) unclassified.role = location.role;+ else finding.locations.push(structuredClone(location));}}@@ -269,6 +355,7 @@ function addLocations(finding: Finding, source: Pick<Finding, "locations">) {const repairGroupSchema = z.strictObject({order: z.number().int().nonnegative(),sources: z.array(z.string()),+ representative: scanDraftInputSchema.shape.findings.element.optional(),});const repairGroupsSchema = z.array(repairGroupSchema).min(1);type RepairGroup = z.infer<typeof repairGroupSchema>;@@ -299,18 +386,54 @@ function hasCompleteSources(finding: Finding) {);}-function restoreRepairGroups(findings: Finding[]) {+function withoutSourceHistory(finding: Finding): Finding {+ const {+ sourceFindingIds,+ sourceFindings,+ deepScanRepairGroups,+ ...provenance+ } = finding.provenance;+ return { ...finding, provenance };+}++// The previous reducer could synthesize an accepted finding. Most modern+// groups can be rebuilt directly from their first source and citations.+function savedRepresentative(finding: Finding) {+ const members = finding.provenance.sourceFindings;+ if (!members?.length) return;+ const accepted = withoutSourceHistory(finding);+ const schema = scanDraftInputSchema.shape.findings.element;+ const primary = schema.safeParse(members[0]!.finding).data;+ if (!primary) return accepted;+ for (const member of members.slice(1)) {+ const locations = schema.shape.locations.safeParse(+ member.finding.locations,+ ).data;+ if (!locations) return accepted;+ addLocations(primary, { locations });+ }+ if (!isDeepStrictEqual(withoutSourceHistory(primary), accepted))+ return accepted;+}++type RestoredGroup = {+ finding: Finding;+ target: number;+ representative?: Finding;+ restoredWithoutGroups?: boolean;+};+function restoreRepairGroups(findings: Finding[]): RestoredGroup[] {function visibleGroups() {// Pre-rewrite reducers could write arbitrary fields in provenance.return findings.map((finding, target) => {delete finding.provenance.deepScanRepairGroups;- return { finding, target };+ return { finding, target, restoredWithoutGroups: true };});}if (findings[0]?.provenance.deepScanRepairGroups === undefined)return visibleGroups();- const groups = new Map<number, { finding: Finding; target: number }>();+ const groups = new Map<number, RestoredGroup>();let previousTarget = -1;for (const visible of findings) {const parts = repairGroupsSchema.safeParse(@@ -331,13 +454,17 @@ function restoreRepairGroups(findings: Finding[]) {)return visibleGroups();delete visible.provenance.deepScanRepairGroups;- groups.set(target, { finding: visible, target });+ groups.set(target, {+ finding: visible,+ target,+ representative: parts[0]!.representative,+ });continue;}const originals = new Map(visible.provenance.sourceFindings?.map((source) => [source.id, source]),);- for (const { order, sources } of parts) {+ for (const { order, sources, representative } of parts) {if (!sources.length || order < target || groups.has(order))return visibleGroups();const members = [];@@ -348,7 +475,7 @@ function restoreRepairGroups(findings: Finding[]) {members.push(source);}const finding = scanDraftInputSchema.shape.findings.element.safeParse(- members[0]!.finding,+ representative ?? members[0]!.finding,).data;if (!finding) return visibleGroups();delete finding.provenance.deepScanRepairGroups;@@ -362,7 +489,7 @@ function restoreRepairGroups(findings: Finding[]) {if (!locations) return visibleGroups();addLocations(finding, { locations });}- groups.set(order, { finding, target });+ groups.set(order, { finding, target, representative });}if (originals.size) return visibleGroups();}@@ -411,6 +538,7 @@ export async function mergeBatch(copies.set(content(source.finding), group);}let repairs: Map<number, number[] | undefined> | undefined;+ const fallbackMatches = new Map<number, Set<number>>();let records: ComparisonReport[] = [];let limitation: string | undefined;const unseen = new Set(@@ -419,20 +547,44 @@ export async function mergeBatch(if (unseen.size > (previous ? 0 : 1)) {try {records = originals.map((finding, id) => report(finding, id < previous));- const rows = records.map((record, id) => ({ id, ...record }));- repairs = repairKeys(- await compare(- instructions + "\n" + JSON.stringify(rows),- duplicateResponseSchema,- ),- records,- );+ // Exact repeats share an assignment. Keep every original for provenance+ // and counts without sending identical reports to the model again.+ const unique = new Map<string, number>();+ const distinct: ComparisonReport[] = [];+ const assignments = exact.map((key, id) => {+ const existing = unique.get(key);+ if (existing !== undefined) return existing;+ const index = distinct.length;+ unique.set(key, index);+ distinct.push(records[id]!);+ return index;+ });+ const keys = await compareRecords(distinct, compare);+ repairs = new Map();+ for (const [id, assignment] of assignments.entries()) {+ if (keys.has(assignment)) repairs.set(id, keys.get(assignment));+ }if (repairs.size < records.length)limitation = `Deep Scan comparison omitted ${records.length - repairs.size} of ${records.length} reports from its repair assignments.`;} catch (error) {- // Failed comparisons preserve all findings except exact repeats.onError(error);limitation = `Deep Scan could not compare this batch for duplicates: ${errorMessage(error)}`;+ if (contextWindowExceeded(error) && records.length > 2) {+ limitation +=+ " Retried reports with shared files in smaller comparisons. Pairs that could not fit were kept separate.";+ try {+ await compareWithinContext(+ records,+ exact,+ previous,+ compare,+ fallbackMatches,+ );+ } catch (retryError) {+ onError(retryError);+ limitation += ` Smaller comparisons stopped: ${errorMessage(retryError)}`;+ }+ }}}const changed = new Map<number, number>();@@ -447,10 +599,8 @@ export async function mergeBatch(const other = repairs?.get(prior.id);if (incomplete.has(prior.target) ||- !key ||- !other ||- key.length !== other.length ||- !key.every((v, i) => v === other[i])+ (!equalRepairs(key, other) &&+ !fallbackMatches.get(id)?.has(prior.id)))return false;if (@@ -483,8 +633,7 @@ export async function mergeBatch(retained.push(group);} else {addLocations(group.finding, source);- if (!copy && group.id < previous && !changed.has(group.target))- changed.set(group.target, workerIndex);+ if (!copy && group.id < previous) changed.set(group.id, workerIndex);}const sourceId = `${worker.id}:${index}`;copies.set(exact[id]!, group);@@ -513,13 +662,24 @@ export async function mergeBatch(}}// A later worker can add a separately fixable path to a previously merged group.+ const lastChange = new Map<number, number>();+ for (const [id, worker] of changed) {+ const target = priorTargets[id]!;+ lastChange.set(target, Math.max(lastChange.get(target) ?? -1, worker));+ }for (const group of retained.slice(0, previous)) {- const worker = changed.get(group.target);+ const worker = changed.get(group.id) ?? lastChange.get(group.target);if (worker === undefined || group.target === group.id) continue;group.target = group.id;group.worker = worker;}- if (repairs && retained.some((group) => group.worker >= 0)) {+ const reviewable = retained.filter(+ (group) =>+ group.target === group.id &&+ !incomplete.has(group.target) &&+ (repairs || (fallbackMatches.size > 0 && group.id < previous)),+ );+ if (reviewable.some((group) => group.worker >= 0)) {const sources = new Map<number, Record<string, unknown>[]>();for (const group of retained) {if (incomplete.has(group.target)) continue;@@ -531,12 +691,10 @@ export async function mergeBatch(}const reviewFailures = new Set<string>();const matches = await checkSameFix(- retained.filter(- (group) => group.target === group.id && !incomplete.has(group.target),- ),+ reviewable,priorTargets,records,- repairs,+ repairs ?? new Map(),sources,compare,(error) => {@@ -596,9 +754,13 @@ export async function mergeBatch(...group.finding.provenance.sourceFindings!,);}+ const representative = group.restoredWithoutGroups+ ? savedRepresentative(group.finding)+ : group.representative;(target.provenance.deepScanRepairGroups as RepairGroup[]).push({order,sources: sourceIds(group.finding),+ ...(representative && { representative }),});}aggregate.findings = [...visible.values()];diff --git a/plugins/codex-security/mcp-app/src/deep-scan/same-fix.ts b/plugins/codex-security/mcp-app/src/deep-scan/same-fix.tsindex c38dd818d..f9f33ffb3 100644--- a/plugins/codex-security/mcp-app/src/deep-scan/same-fix.ts+++ b/plugins/codex-security/mcp-app/src/deep-scan/same-fix.ts@@ -116,7 +116,7 @@ async function confirmSources(sources as { id: number; same: boolean; survivingPaths: string[] }[],(s) => s.id,)- .filter((s) => s.same && !s.survivingPaths.length)+ .filter((s) => s.same === true && !s.survivingPaths.length).map((s) => s.id),);for (const [id, { pair }] of batch.entries())diff --git a/plugins/codex-security/mcp-app/tests/test_deep_scan_iterations.ts b/plugins/codex-security/mcp-app/tests/test_deep_scan_iterations.tsindex 151f90df4..d4358d92d 100644--- a/plugins/codex-security/mcp-app/tests/test_deep_scan_iterations.ts+++ b/plugins/codex-security/mcp-app/tests/test_deep_scan_iterations.ts@@ -766,6 +766,30 @@ test("a later source with a surviving path reopens the merge and counts as new",);});+test("a context fallback rechecks a prior consolidation before counting it as new", async () => {+ const [left, alias, clarification] = [+ "Unchecked lookup",+ "Another lookup caller",+ "More detail on the lookup",+ ].map((title) => ({ ...lookup, title }));+ const { draft, terminal } = await scanLoop([[left, alias], [clarification]], {+ workers: 1,+ maxDiscoveryRuns: 2,+ stopAfterNoNew: 1,+ comparisons: [+ [[0, 1], [1]],+ () => {+ throw new Error("Codex ran out of room in the model's context window.");+ },+ [[0, 1], [2]],+ ],+ sameFix: approvePairs,+ });+ assert.equal(draft.findings.length, 1);+ assert.equal(draft.findings[0]!.provenance.sourceFindings?.length, 3);+ assert.equal(terminal.terminalReason, "saturated");+});+test("a failed comparison waits for every started worker batch", async () => {const { checkSameFix } = await importSource(join(import.meta.dirname, "../src/deep-scan/same-fix.ts"),@@ -883,6 +907,85 @@ test("a saved same-fix merge keeps both original comparison inputs on resume", a),[1],);+ // Both workers amend the prior visible group, but the second worker adds+ // the independently fixable path to its secondary repair group.+ const split = parseScanUnion(+ JSON.parse(JSON.stringify(persistedScanUnion(before))),+ );+ const splitCounts = await mergeBatch(+ split,+ [+ worker("w3", [{ ...left, title: "Left clarification" }]),+ worker("w4", [{ ...alias, title: "Alias with a separate repair" }]),+ ],+ "/tmp",+ async (+ _prompt: string,+ schema: { properties: Record<string, unknown> },+ ) => {+ if ("reviews" in schema.properties)+ return JSON.stringify({ reviews: [] });+ return defects([+ [0, 2],+ [1, 3],+ ]);+ },+ fail,+ );+ assert.deepEqual(splitCounts, [0, 1]);+ const legacy = {+ scanId: before.scanId,+ findings: [+ {+ ...left,+ title: "Accepted combined report",+ summary: "The previous reducer combined the validated explanations.",+ locations: [...left.locations, { path: "combined.py", startLine: 3 }],+ provenance: {+ ...left.provenance,+ sourceFindingIds: ["old:0"],+ sourceFindings: [{ id: "old:0", finding: left }],+ },+ },+ ],+ };+ const singleton = legacy.findings[0]!;+ const representative = { ...singleton, provenance: left.provenance };+ Object.assign(singleton.provenance, {+ deepScanRepairGroups: [{ order: 0, sources: ["old:0"], representative }],+ });+ await mergeBatch(+ legacy,+ [worker("next", [alias])],+ "/tmp",+ async (_p: string, schema: { properties: Record<string, unknown> }) => {+ if ("reviews" in schema.properties)+ return JSON.stringify({ reviews: [samePair(0)] });+ if ("sources" in schema.properties)+ return JSON.stringify({+ sources: [{ id: 0, same: true, survivingPaths: [] }],+ });+ return defects([[0, 1], [1]]);+ },+ fail,+ );+ const legacyRestored = parseScanUnion(+ JSON.parse(JSON.stringify(persistedScanUnion(legacy))),+ );+ await mergeBatch(+ legacyRestored,+ [worker("last", [unrelated])],+ "/tmp",+ async () => defects([[0], [1], [2]]),+ fail,+ );+ assert.equal(legacyRestored.findings[0].title, "Accepted combined report");+ assert.match(legacyRestored.findings[0].summary, /combined the validated/);+ assert.ok(+ legacyRestored.findings[0].locations.some(+ ({ path }: { path: string }) => path === "combined.py",+ ),+ );const draft = scanUnionToDraft(restored);assert.deepEqual(draft.scope, {summary: "Review by w1",@@ -1023,6 +1126,120 @@ test("a merged report cannot lend its extra citation to a later comparison", asy);});+test("a context-sized report does not prevent comparison of later observations", async () => {+ const tooLarge = () => {+ throw new Error(+ "Codex ran out of room in the model's context window. Start a new thread or clear earlier history before retrying.",+ );+ };+ const separate = { ...base, title: "Oversized report" };+ const alias = { ...base, title: "Second description" };+ const { draft } = await scanLoop([[separate, base, alias]], {+ maxDiscoveryRuns: 1,+ stopAfterNoNew: 1,+ comparisons: [+ tooLarge,+ tooLarge,+ tooLarge,+ tooLarge,+ (rows) => {+ assert.deepEqual(+ rows.map(({ title }) => title),+ [alias.title, base.title],+ );+ return {+ uncertain: [],+ defects: [+ {+ operation: "check",+ failure: "fixture",+ path: "fixture.py",+ reports: rows.map(({ id }) => ({ id, evidence: 0 })),+ },+ ],+ };+ },+ ],+ });+ assert.deepEqual(+ draft.findings.map((f) => f.provenance.sourceFindings?.length),+ [1, 2],+ );+ assert.match(draft.scope?.limitations?.join(" ") ?? "", /context window/);+});++test("same-worker identities and independently cited controls survive an identical repair assignment", async () => {+ const root = {+ ...lookup,+ identity: { anchor: "lookup.owner" },+ taxonomy: { category: "authorization", cwe: ["CWE-862"] },+ };+ const second = {+ ...root,+ title: "Different instance",+ identity: { anchor: "lookup.other" },+ };+ const authorization = {+ ...root,+ title: "Also missing the caller check",+ taxonomy: { category: "authorization", cwe: ["CWE-862", "CWE-306"] },+ locations: [+ ...root.locations,+ { path: "caller.py", startLine: 4, role: "control" },+ ],+ };+ const { draft } = await scanLoop([[root, second, authorization]], {+ maxDiscoveryRuns: 1,+ stopAfterNoNew: 1,+ comparisons: [[[0, 1, 2]]],+ sameFix: approvePairs,+ });+ assert.deepEqual(+ draft.findings.map((f) => f.title),+ [root.title, second.title, authorization.title],+ );+});++test("repair assignment includes validated attack paths and a shared range retains its root role", async () => {+ const first = {+ ...lookup,+ locations: [{ path: "fixture.py", startLine: 4, role: "entrypoint" }],+ };+ const second = {+ ...first,+ title: "Alias",+ locations: [{ ...first.locations[0], role: "root_control" }],+ validation: { summary: "Verified dispatch." },+ attackPath: { steps: ["Dispatch calls the helper."] },+ };+ const { draft } = await scanLoop([[first, second]], {+ maxDiscoveryRuns: 1,+ stopAfterNoNew: 1,+ comparisons: [+ (rows) => {+ const record = rows[1] as unknown as typeof second;+ assert.deepEqual(record.validation, second.validation);+ assert.deepEqual(record.attackPath, second.attackPath);+ return {+ uncertain: [],+ defects: [+ {+ operation: "repair",+ failure: "fixture",+ path: "fixture.py",+ reports: rows.map(({ id }) => ({ id, evidence: 0 })),+ },+ ],+ };+ },+ ],+ });+ assert.deepEqual(+ draft.findings.map((f) => f.locations.map(({ role }) => role)),+ [["entrypoint", "root_control"]],+ );+});+for (const conflict of ["reviews", "sources"]) {test(`conflicting ${conflict} cannot hide an independent finding`, async () => {const { checkSameFix } = await importSource(@@ -1095,10 +1312,13 @@ test("repeated original observations require one source comparison", async () =>maxDiscoveryRuns: 1,stopAfterNoNew: 1,comparisons: [- () => ({- uncertain: observations.map((_, i) => i).concat(observations.length),- defects: [],- }),+ (rows) => {+ assert.deepEqual(+ rows.map(({ title }) => title),+ [lookup.title, alias.title],+ );+ return { uncertain: rows.map(({ id }) => id), defects: [] };+ },],sameFix: approvePairs,sameSource: () => {diff --git a/plugins/codex-security/mcp-app/tests/test_deep_scan_same_fix.ts b/plugins/codex-security/mcp-app/tests/test_deep_scan_same_fix.tsindex 2485d66d4..3d3d28f2e 100644--- a/plugins/codex-security/mcp-app/tests/test_deep_scan_same_fix.ts+++ b/plugins/codex-security/mcp-app/tests/test_deep_scan_same_fix.ts@@ -17,7 +17,11 @@ const groups = [const review = { pair: 0, decision: "same", leftEvidence: 0, rightEvidence: 0 };const confirmation = { id: 0, same: true, survivingPaths: [] };-for (const pass of ["pair review", "source confirmation"]) {+for (const pass of [+ "pair review",+ "source confirmation",+ "non-boolean confirmation",+]) {test(`contradictory ${pass} preserves the two findings`, async () => {const matches = await checkSameFix(groups,@@ -41,7 +45,15 @@ for (const pass of ["pair review", "source confirmation"]) {sources:pass === "source confirmation"? [confirmation, { ...confirmation, same: false }]- : [confirmation],+ : [+ {+ ...confirmation,+ same:+ pass === "non-boolean confirmation"+ ? "false"+ : true,+ },+ ],},),assert.fail,