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(deduplication): bind provenance to each retrieved finding (#1752)
Michael D'Angelo175486a6d94e
diff --git a/sdk/typescript/src/deduplication/deduplication-prompts.ts b/sdk/typescript/src/deduplication/deduplication-prompts.tsindex 4110774be..fba0f5a13 100644--- a/sdk/typescript/src/deduplication/deduplication-prompts.ts+++ b/sdk/typescript/src/deduplication/deduplication-prompts.ts@@ -67,15 +67,15 @@ export function groupingReviewPrompt(findings: readonly Finding[]): string {export function savedSourceContext(approvedRepositoryId: string,findings: readonly Finding[],- repositories: ReadonlyMap<string, readonly string[]>,- sourceSnapshots?: ReadonlyMap<string, FindingSourceSnapshot>,+ repositories: ReadonlyMap<Finding, readonly string[]>,+ sourceSnapshots?: ReadonlyMap<Finding, FindingSourceSnapshot>,): string {return `Host-provided saved repository associations:\n${JSON.stringify({approvedRepositoryId,findings: findings.map((finding) => ({findingId: finding.findingId,- repositoryIds: repositories.get(finding.findingId) ?? [],- sourceSnapshot: sourceSnapshots?.get(finding.findingId),+ repositoryIds: repositories.get(finding) ?? [],+ sourceSnapshot: sourceSnapshots?.get(finding),})),})}\nThe approved checkout can establish current source only for findings associated with approvedRepositoryId. A sourceSnapshot identifies the saved occurrence's repository, recorded revision, and working-tree snapshot digest; a revision can be a baseline for uncommitted changes, so preserve the supplied snapshot evidence. Missing sourceSnapshot means its saved source could not be established, not that the current checkout is its source. Current files do not establish another repository's or an earlier revision's code. Treat disjoint repository associations as separate source contexts: similar names, paths, or snippets do not prove a shared maintained control. Establish any common originating control from supplied source provenance before deciding SAME across those contexts; missing provenance is not evidence that they are the same repository. These associations do not authorize reading another checkout. Use supplied evidence and available revisions in approved checkouts, and report a required source-access blocker when it prevents a decision.\n\n`;}diff --git a/sdk/typescript/src/deduplication/scan.ts b/sdk/typescript/src/deduplication/scan.tsindex 5c784228d..331ce27fc 100644--- a/sdk/typescript/src/deduplication/scan.ts+++ b/sdk/typescript/src/deduplication/scan.ts@@ -1,3 +1,4 @@+import type { Finding } from "../models.js";import { loadContractWithScanDirectory } from "../contract.js";import type { CodexSecuritySurface } from "../api.js";import { environmentEntry } from "../auth.js";@@ -339,13 +340,11 @@ async function deduplicateResolvedScan(options.onDiagnostic,): undefined;- const sourceRepositories = new Map<string, readonly string[]>(- contract.findings.findings.map((finding) => [- finding.findingId,- [contract.manifest.scan.target.targetId],- ]),+ const selectedFindingIds = new Set(+ contract.findings.findings.map((finding) => finding.findingId),);- const sourceSnapshots = new Map<string, FindingSourceSnapshot>();+ const sourceRepositories = new Map<Finding, readonly string[]>();+ const sourceSnapshots = new Map<Finding, FindingSourceSnapshot>();const deduplicator = new FindingDeduplicator({potentialDuplicates: async (findingId) => {@@ -364,17 +363,21 @@ async function deduplicateResolvedScan(? neighborhood.repositoryIds![finding.findingId]: undefined;if (repositories !== undefined)- sourceRepositories.set(finding.findingId, repositories);- else if (scope.repositoryId !== undefined)- sourceRepositories.set(finding.findingId, [scope.repositoryId]);+ sourceRepositories.set(finding, repositories);+ else if (+ scope.repositoryId !== undefined ||+ selectedFindingIds.has(finding.findingId)+ )+ sourceRepositories.set(finding, [+ scope.repositoryId ?? contract.manifest.scan.target.targetId,+ ]);const snapshot = Object.hasOwn(neighborhood.sourceSnapshots ?? {},finding.findingId,)? neighborhood.sourceSnapshots![finding.findingId]: undefined;- if (snapshot !== undefined)- sourceSnapshots.set(finding.findingId, snapshot);+ if (snapshot !== undefined) sourceSnapshots.set(finding, snapshot);}return neighborhood;},@@ -394,9 +397,7 @@ async function deduplicateResolvedScan(options.signal,options.concurrency,);- const reviewed = await deduplicator.run(- contract.findings.findings.map((finding) => finding.findingId),- );+ const reviewed = await deduplicator.run([...selectedFindingIds]);await checkpoints?.assertSourceUnchanged();options.signal?.throwIfAborted();const result: DeduplicateScanResult = { scanId, ...reviewed };diff --git a/sdk/typescript/tests-ts/finding-deduplication.test.ts b/sdk/typescript/tests-ts/finding-deduplication.test.tsindex 4d44cedd7..eb8548a9f 100644--- a/sdk/typescript/tests-ts/finding-deduplication.test.ts+++ b/sdk/typescript/tests-ts/finding-deduplication.test.ts@@ -1,11 +1,17 @@-import { readJson } from "./support/json.js";+import { readJson, writeJson } from "./support/json.js";import { workflowFixture } from "./support/workflow-fixture.js";import { emptyNeighborhoodReviewer } from "./support/deduplication.js";import { readFile } from "node:fs/promises";+import { createHash } from "node:crypto";import { join } from "node:path";import { expect, test, mock } from "bun:test";import Ajv2020 from "ajv/dist/2020.js";-import type { Finding, FindingsDocument } from "../src/models.js";+import type { Finding, FindingsDocument, ScanManifest } from "../src/models.js";+import { deriveFindingIdentity } from "../src/contract.js";+import type {+ FindingNeighborhood,+ FindingSourceSnapshot,+} from "../src/finding-retrieval.js";import type { CodexReview } from "../src/deduplication/codex-review.js";import {contradictionFreeSubgroups,@@ -1155,6 +1161,315 @@ test("accepts complete canonical and merged reviews and rejects invalid assignme}});+test.each(+ (+ [+ "stable",+ "changed",+ "missing",+ "same-body-missing",+ "same-body-changed",+ "empty",+ ] as const+ ).flatMap((kind) => [false, true].map((reverse) => ({ kind, reverse }))),+)(+ "saved-scan provenance follows each retrieved record: %j",+ async ({ kind, reverse }) => {+ const fixture = await workflowFixture();+ const gates = [+ Promise.withResolvers<void>(),+ Promise.withResolvers<void>(),+ ];+ let pending: Promise<unknown> | undefined;+ try {+ const manifestPath = join(fixture.scanDir, "scan-manifest.json");+ const findingsPath = join(fixture.scanDir, "findings.json");+ const manifest = await readJson<ScanManifest>(manifestPath);+ const targetId = manifest.scan.target.targetId;+ const first = fixture.document.findings[0]!;+ const second: Finding = {+ ...structuredClone(first),+ ...deriveFindingIdentity(manifest.scan.id, targetId, first.ruleId, {+ anchor: "second-synthetic-control",+ }),+ title: "Second saved finding",+ };+ await writeJson(findingsPath, {+ ...fixture.document,+ findings: [first, second],+ });+ const findingsBytes = await readFile(findingsPath, "utf8");+ manifest.scan.artifacts.find(+ (artifact) => artifact.path === "findings.json",+ )!.sha256 = createHash("sha256").update(findingsBytes).digest("hex");+ await writeJson(manifestPath, manifest);+ const manifestBytes = await readFile(manifestPath, "utf8");+ const latest = [first, second].map((finding): Finding => {+ if (+ kind === "stable" ||+ kind.startsWith("same-body") ||+ kind === "empty"+ )+ return structuredClone(finding);+ return {+ ...structuredClone(finding),+ ...deriveFindingIdentity(+ "scan_later_observation",+ targetId,+ finding.ruleId,+ finding.identity,+ ),+ title: `Later observation: ${finding.title}`,+ };+ });+ const initialSnapshot = {+ repositoryId: "target_initial",+ revision: manifest.scan.target.revision!,+ snapshotDigest: manifest.scan.target.snapshotDigest ?? null,+ } satisfies FindingSourceSnapshot;+ const laterSnapshot =+ kind === "stable"+ ? initialSnapshot+ : {+ repositoryId: "target_later",+ revision: "a".repeat(40),+ snapshotDigest: `codex-security-snapshot/v1:sha256:${"b".repeat(64)}`,+ };+ const ids = [first.findingId, second.findingId];+ const repositories = (repositoryIds: string[]) =>+ Object.fromEntries(ids.map((id) => [id, repositoryIds]));+ const snapshots = (snapshot: FindingSourceSnapshot) =>+ Object.fromEntries(ids.map((id) => [id, snapshot]));+ const neighborhoods: FindingNeighborhood[] = [+ {+ finding: first,+ potentialDuplicates: [second],+ repositoryIds: repositories([initialSnapshot.repositoryId]),+ sourceSnapshots: snapshots(initialSnapshot),+ },+ {+ finding: latest[1]!,+ potentialDuplicates: [latest[0]!],+ ...(kind.includes("missing")+ ? {}+ : {+ repositoryIds: repositories(+ kind === "empty" ? [] : [laterSnapshot.repositoryId],+ ),+ sourceSnapshots:+ kind === "empty" ? {} : snapshots(laterSnapshot),+ }),+ },+ ];+ const originalNeighborhoods = structuredClone(neighborhoods);+ const starts = [+ Promise.withResolvers<void>(),+ Promise.withResolvers<void>(),+ ];+ const reviewed: string[][] = [];+ const writes: unknown[] = [];+ pending = deduplicateScanInternal(+ manifest.scan.id,+ {+ findingsUrl: "https://synthetic.test/api",+ allRepositories: true,+ concurrency: 2,+ },+ {+ environment: fixture.environment,+ resolveReviewEnvironment: async () => ({+ CODEX_HOME: fixture.environment.CODEX_HOME,+ }),+ runWorkbench: async () => ({+ scan: {+ scanId: manifest.scan.id,+ scanDir: fixture.scanDir,+ targetPath: fixture.repository,+ progress: { status: "complete" },+ },+ }),+ fetch: async (url, options) => {+ if (options.method === "POST") {+ writes.push(JSON.parse(String(options.body)));+ return Response.json({});+ }+ const id = decodeURIComponent(url.pathname.split("/").at(-2)!);+ const index = ids.indexOf(id);+ expect(index).toBeGreaterThanOrEqual(0);+ starts[index]!.resolve();+ await gates[index]!.promise;+ return Response.json(neighborhoods[index]);+ },+ reviewRunner: {+ async run<T>(review: CodexReview<T>): Promise<T> {+ const assignedIds =+ review.findingIds ?? fail("Missing review participants");+ const origin =+ review.stage === "screening" ? ids.indexOf(assignedIds[0]!) : 1;+ const neighborhood = neighborhoods[origin]!;+ const expected = [+ neighborhood.finding,+ ...neighborhood.potentialDuplicates,+ ];+ const records: { findings: Finding[] } = JSON.parse(+ review.prompt+ .split("\n")+ .find((line) => line.startsWith('{"findings":'))!,+ );+ expect(records.findings).toEqual(expected);+ const context: {+ approvedRepositoryId: string;+ findings: {+ findingId: string;+ repositoryIds: string[];+ sourceSnapshot?: FindingSourceSnapshot;+ }[];+ } = JSON.parse(+ review.prompt+ .split("\n")+ .find((line) => line.startsWith('{"approvedRepositoryId":'))!,+ );+ expect(context.approvedRepositoryId).toBe(targetId);+ expect(context.findings).toHaveLength(expected.length);+ for (const [index, finding] of expected.entries()) {+ const snapshot =+ neighborhood.sourceSnapshots?.[finding.findingId];+ expect(context.findings[index]).toEqual({+ findingId: finding.findingId,+ repositoryIds: neighborhood.repositoryIds?.[+ finding.findingId+ ] ?? [targetId],+ ...(snapshot === undefined+ ? {}+ : { sourceSnapshot: snapshot }),+ });+ expect(+ Object.hasOwn(context.findings[index]!, "sourceSnapshot"),+ ).toBe(snapshot !== undefined);+ }+ reviewed.push([...assignedIds]);+ return review.validate(+ review.stage === "screening"+ ? {+ decisions: {+ "pair-1": {+ decision: "SAME",+ rationale: "Inspect the supplied source.",+ },+ },+ }+ : {+ decision: "SAME",+ rationale: "One correction covers both findings.",+ },+ );+ },+ },+ },+ );+ await Promise.race([+ Promise.all(starts.map((start) => start.promise)),+ pending.then(() =>+ fail("Review completed before both lookups started"),+ ),+ ]);+ for (const index of reverse ? [1, 0] : [0, 1]) {+ gates[index]!.resolve();+ await new Promise<void>((resolve) => setImmediate(resolve));+ }+ const group = [...ids].sort();+ expect(await pending).toEqual({+ scanId: manifest.scan.id,+ uniqueFindingIds: [group[0]],+ duplicateGroups: [group],+ deduplicationStatus: "completed",+ });+ expect(reviewed).toEqual([ids, [...ids].reverse(), [...ids].reverse()]);+ expect(writes).toEqual([{ groups: [group] }]);+ expect(neighborhoods).toEqual(originalNeighborhoods);+ expect(await readFile(findingsPath, "utf8")).toBe(findingsBytes);+ expect(await readFile(manifestPath, "utf8")).toBe(manifestBytes);+ } finally {+ for (const gate of gates) gate.resolve();+ await pending?.catch(() => undefined);+ await fixture[Symbol.asyncDispose]();+ }+ },+);++test.each(+ [false, true].flatMap((allRepositories) =>+ [false, true].map((emptyMaps) => ({ allRepositories, emptyMaps })),+ ),+)(+ "saved-scan repository fallback respects selection: %j",+ async ({ allRepositories, emptyMaps }) => {+ await using fixture = await workflowFixture();+ const manifest = await readJson<ScanManifest>(+ join(fixture.scanDir, "scan-manifest.json"),+ );+ const selected = fixture.document.findings[0]!;+ const neighbor = entry(2);+ let reviews = 0;+ const result = await deduplicateScanInternal(+ manifest.scan.id,+ { findingsUrl: "https://synthetic.test/api", allRepositories },+ {+ environment: fixture.environment,+ resolveReviewEnvironment: async () => ({+ CODEX_HOME: fixture.environment.CODEX_HOME,+ }),+ runWorkbench: async () => ({+ scan: {+ scanId: manifest.scan.id,+ scanDir: fixture.scanDir,+ targetPath: fixture.repository,+ progress: { status: "complete" },+ },+ }),+ fetch: async () =>+ Response.json({+ finding: selected,+ potentialDuplicates: [neighbor],+ ...(emptyMaps ? { repositoryIds: {}, sourceSnapshots: {} } : {}),+ }),+ reviewRunner: {+ async run<T>(review: CodexReview<T>): Promise<T> {+ reviews++;+ const context = JSON.parse(+ review.prompt+ .split("\n")+ .find((line) => line.startsWith('{"approvedRepositoryId":'))!,+ );+ expect(context.findings).toEqual([+ {+ findingId: selected.findingId,+ repositoryIds: [manifest.scan.target.targetId],+ },+ {+ findingId: neighbor.findingId,+ repositoryIds: allRepositories+ ? []+ : [manifest.scan.target.targetId],+ },+ ]);+ return review.validate({+ decisions: {+ "pair-1": {+ decision: "DISTINCT",+ rationale: "Independent controls.",+ },+ },+ });+ },+ },+ },+ );+ expect(reviews).toBe(1);+ expect(result.duplicateGroups).toEqual([]);+ },+);+test("resolves a saved scan and retrieves its IDs without uploading or modifying artifacts", async () => {const fixture = await workflowFixture();const { scanDir: directory } = fixture;