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): simplify publication evidence bookkeeping (#1387)
mldangelo-oai21780b92f2fa
diff --git a/sdk/typescript/src/publication-events.ts b/sdk/typescript/src/publication-events.tsindex ce5d67e23..faa73b5ce 100644--- a/sdk/typescript/src/publication-events.ts+++ b/sdk/typescript/src/publication-events.ts@@ -204,12 +204,9 @@ function collectPublicationClaims(value: unknown,claims: PublicationClaim[],): void {- const visited = new Set<Record<string, unknown>>();- const pending: unknown[] = [value];+ const pending = isRecord(value) ? [value] : [];while (pending.length > 0) {- const candidate = pending.pop();- if (!isRecord(candidate) || visited.has(candidate)) continue;- visited.add(candidate);+ const candidate = pending.pop()!;collectDirectClaims(candidate, claims);const data = candidate["data"];@@ -231,8 +228,8 @@ function collectPublicationClaims(}}}- for (let index = nested.length - 1; index >= 0; index -= 1) {- if (isRecord(nested[index])) pending.push(nested[index]);+ for (const child of nested.reverse()) {+ if (isRecord(child)) pending.push(child);}}}diff --git a/sdk/typescript/src/publication.ts b/sdk/typescript/src/publication.tsindex 80bb2b971..d6c7d3e44 100644--- a/sdk/typescript/src/publication.ts+++ b/sdk/typescript/src/publication.ts@@ -54,7 +54,6 @@ export interface PreparedPublicationIssue {export interface PreparedScanPublication {scanId: string;- uploadId: string;scanDirectory: string;destination: LinearPublicationDestination;issues: PreparedPublicationIssue[];@@ -142,7 +141,6 @@ export async function prepareScanPublication(return {scanId,- uploadId: scanId,scanDirectory: canonicalScanDirectory,...(classification === undefined && options.findingIds === undefined? {}diff --git a/sdk/typescript/src/publish.ts b/sdk/typescript/src/publish.tsindex b5ba6fa75..c230cb5f4 100644--- a/sdk/typescript/src/publish.ts+++ b/sdk/typescript/src/publish.ts@@ -201,7 +201,6 @@ export interface PublishScanDependencies {type PublicationHandoffEvidence = {source: "handoff";- rawLine: string;ownerFindingId?: string;resolution: ClaimResolution;possibleMutation?: boolean;@@ -218,30 +217,14 @@ type PublicationHandoffEvidence = {type PublicationEvidence =PublicationEventEvidence | PublicationHandoffEvidence;-type CompletedPublicationEvent = Extract<- PublicationEventEvidence,- { status: "completed" }->;-type FailedPublicationEvent = Extract<- PublicationEventEvidence,- { status: "failed" }->;--interface FindingEvidenceBucket {- completed: CompletedPublicationEvent[];- rejected: FailedPublicationEvent[];- handoffs: PublicationHandoffEvidence[];-}-interface IndexedPublicationEvidence {- byOwner: Map<string, FindingEvidenceBucket>;+ byOwner: Map<string, PublicationEvidence[]>;unowned: PublicationEvidence[];claimLedger: Map<string,{kinds: Set<PublicationClaim["kind"]>;- owners: Set<string>;- reservedByUnknownOwner: boolean;+ owners: Set<string | undefined>;}>;}@@ -350,7 +333,6 @@ export async function publishScanInternal(options.signal?.throwIfAborted();const handoff = await createPublicationHandoff(prepared, environment);const progressObserver = options.onProgress;- const completedFindings = new Set<string>();reportPublicationProgress(progressObserver, {type: "started",scanId: prepared.scanId,@@ -446,19 +428,15 @@ export async function publishScanInternal(const outcomes = new Map([...handoffResults.created, ...handoffResults.failed].map(findingEntry),);- for (const preparedIssue of prepared.issues) {- const issue = outcomes.get(preparedIssue.findingId);- if (issue === undefined || completedFindings.has(issue.findingId)) {- continue;- }- completedFindings.add(issue.findingId);+ for (const [index, preparedIssue] of prepared.issues.entries()) {+ const issue = outcomes.get(preparedIssue.findingId)!;reportPublicationProgress(progressObserver, {type: "issue_completed",findingId: issue.findingId,...("issueIdentifier" in issue? { issueIdentifier: issue.issueIdentifier }: { error: issue.error }),- completed: completedFindings.size,+ completed: index + 1,total: prepared.issues.length,});}@@ -964,7 +942,6 @@ async function collectPublicationHandoffEvidence(evidence.push({source: "handoff",status: "invalid",- rawLine,resolution: resolveClaims([]),error: "Codex wrote an invalid Linear publication handoff.",});@@ -980,7 +957,6 @@ async function collectPublicationHandoffEvidence(evidence.push({source: "handoff",status: "invalid",- rawLine,resolution,...possibleMutation,error: "Codex wrote an unexpected Linear publication handoff.",@@ -992,7 +968,6 @@ async function collectPublicationHandoffEvidence(evidence.push({source: "handoff",status: "invalid",- rawLine,resolution,...possibleMutation,error: "Codex wrote a Linear publication for an unknown finding.",@@ -1010,7 +985,6 @@ async function collectPublicationHandoffEvidence((!Object.hasOwn(record, "error") && argumentsValid);const base = {source: "handoff" as const,- rawLine,ownerFindingId: issue.findingId,resolution,...(mutationPossible ? { possibleMutation: true as const } : {}),@@ -1126,12 +1100,10 @@ function reconcilePublicationEvidence(const collidingOwners = new Set<string>();for (const reservation of indexed.claimLedger.values()) {- if (- reservation.kinds.size > 1 ||- reservation.owners.size > 1 ||- (reservation.reservedByUnknownOwner && reservation.owners.size > 0)- ) {- for (const owner of reservation.owners) collidingOwners.add(owner);+ if (reservation.kinds.size > 1 || reservation.owners.size > 1) {+ for (const owner of reservation.owners) {+ if (owner !== undefined) collidingOwners.add(owner);+ }}}for (const outcome of outcomes) {@@ -1139,7 +1111,6 @@ function reconcilePublicationEvidence(outcome.created = undefined;outcome.error ="Codex wrote a Linear publication that reused or relabeled a claim across incompatible publication evidence.";- outcome.indeterminate = true;indeterminate = true;}@@ -1191,7 +1162,7 @@ function reconcilePublicationEvidence(function indexPublicationEvidence(evidence: readonly PublicationEvidence[],): IndexedPublicationEvidence {- const byOwner = new Map<string, FindingEvidenceBucket>();+ const byOwner = new Map<string, PublicationEvidence[]>();const unowned: PublicationEvidence[] = [];const claimLedger: IndexedPublicationEvidence["claimLedger"] = new Map();@@ -1201,15 +1172,10 @@ function indexPublicationEvidence(const key = alias.value;const reservation = claimLedger.get(key) ?? {kinds: new Set<PublicationClaim["kind"]>(),- owners: new Set<string>(),- reservedByUnknownOwner: false,+ owners: new Set<string | undefined>(),};reservation.kinds.add(alias.kind);- if (item.ownerFindingId === undefined) {- reservation.reservedByUnknownOwner = true;- } else {- reservation.owners.add(item.ownerFindingId);- }+ reservation.owners.add(item.ownerFindingId);claimLedger.set(key, reservation);}}@@ -1218,18 +1184,8 @@ function indexPublicationEvidence(unowned.push(item);continue;}- const bucket: FindingEvidenceBucket = byOwner.get(item.ownerFindingId) ?? {- completed: [],- rejected: [],- handoffs: [],- };- if (item.source === "handoff") {- bucket.handoffs.push(item);- } else if (item.status === "completed") {- bucket.completed.push(item);- } else {- bucket.rejected.push(item);- }+ const bucket = byOwner.get(item.ownerFindingId) ?? [];+ bucket.push(item);byOwner.set(item.ownerFindingId, bucket);}@@ -1238,11 +1194,10 @@ function indexPublicationEvidence(function reconcileFindingEvidence(issue: PreparedPublicationIssue,- bucket: FindingEvidenceBucket | undefined,+ bucket: PublicationEvidence[] = [],): FindingReconciliation {- const completed = bucket?.completed ?? [];- const rejected = bucket?.rejected ?? [];- const handoffs = bucket?.handoffs ?? [];+ const events = bucket.filter((item) => item.source === "event");+ const handoffs = bucket.filter((item) => item.source === "handoff");const failed = (error: string,indeterminate: boolean,@@ -1260,14 +1215,14 @@ function reconcileFindingEvidence(},});- if (completed.length + rejected.length > 1) {+ if (events.length > 1) {return failed("Codex attempted to create more than one Linear issue for this finding.",true,);}- const completedCall = completed[0];- const eventFailure = rejected[0];+ const completedCall = events.find((event) => event.status === "completed");+ const eventFailure = events.find((event) => event.status === "failed");const failedEventMayHaveMutated =eventFailure !== undefined && eventFailure.resolution.claims.length > 0;if (completedCall !== undefined && !completedCall.argumentsValid) {diff --git a/sdk/typescript/tests-ts/publication-check.test.ts b/sdk/typescript/tests-ts/publication-check.test.tsindex 02e3f1f89..9feb6ab18 100644--- a/sdk/typescript/tests-ts/publication-check.test.ts+++ b/sdk/typescript/tests-ts/publication-check.test.ts@@ -18,7 +18,6 @@ const OPTIONS: CheckScanPublicationOptions = {};const PUBLICATION: PreparedScanPublication = {scanId: "scan-example",- uploadId: "scan-example",scanDirectory: join(tmpdir(), "completed-scan"),destination: {type: "linear",diff --git a/sdk/typescript/tests-ts/publication-events.test.ts b/sdk/typescript/tests-ts/publication-events.test.tsindex 9aaabbcc9..978ae7077 100644--- a/sdk/typescript/tests-ts/publication-events.test.ts+++ b/sdk/typescript/tests-ts/publication-events.test.ts@@ -9,7 +9,6 @@ import type { PreparedScanPublication } from "../src/publication.js";function publication(count = 1): PreparedScanPublication {return {scanId: "scan_example",- uploadId: "scan_example",scanDirectory: "/synthetic/sealed-scan",destination: {type: "linear",diff --git a/sdk/typescript/tests-ts/publication-store.test.ts b/sdk/typescript/tests-ts/publication-store.test.tsindex 603102150..4e4e74b84 100644--- a/sdk/typescript/tests-ts/publication-store.test.ts+++ b/sdk/typescript/tests-ts/publication-store.test.ts@@ -69,7 +69,6 @@ async function publicationFixture(};const publication: PreparedScanPublication = {scanId: SCAN_ID,- uploadId: SCAN_ID,scanDirectory,destination: {type: "linear",@@ -474,7 +473,6 @@ describe("read-only publication history", () => {const otherScan: PreparedScanPublication = {...fixture.publication,scanId: OTHER_SCAN_ID,- uploadId: OTHER_SCAN_ID,scanDirectory,issues: fixture.publication.issues.map((issue) => ({...issue,@@ -802,7 +800,6 @@ connection.close()const otherScan: PreparedScanPublication = {...fixture.publication,scanId: OTHER_SCAN_ID,- uploadId: OTHER_SCAN_ID,scanDirectory: anotherDirectory,issues: [{diff --git a/sdk/typescript/tests-ts/publication.test.ts b/sdk/typescript/tests-ts/publication.test.tsindex ede21567c..48aea3ac4 100644--- a/sdk/typescript/tests-ts/publication.test.ts+++ b/sdk/typescript/tests-ts/publication.test.ts@@ -85,7 +85,7 @@ describe("scan publication preparation", () => {).toBe("scan_example_001");});- test("prepares sealed findings with scan-based upload IDs and full traceability", async () => {+ test("prepares sealed findings with full traceability", async () => {const scanDirectory = await copyExample();const publication = await prepareScanPublication(scanDirectory,@@ -94,7 +94,6 @@ describe("scan publication preparation", () => {expect(publication).toMatchObject({scanId: "scan_example_001",- uploadId: "scan_example_001",scanDirectory: await realpath(scanDirectory),destination: {type: "linear",diff --git a/sdk/typescript/tests-ts/publish.test.ts b/sdk/typescript/tests-ts/publish.test.tsindex fd247be94..c9d32d043 100644--- a/sdk/typescript/tests-ts/publish.test.ts+++ b/sdk/typescript/tests-ts/publish.test.ts@@ -63,7 +63,6 @@ function preparedPublication(): PreparedScanPublication {return {scanId,- uploadId: scanId,scanDirectory: join(tmpdir(), "completed-scan"),destination: {type: "linear",@@ -376,6 +375,7 @@ describe("skip-recorded publication", () => {},preparePublicationStore: rejecting("Previews must not write history."),resolveCodex: throwing("Previews must not start Codex."),+ runCodex: rejecting("Previews must not start Codex."),writeReceipt: rejecting("Previews must not write receipts."),},);@@ -387,7 +387,16 @@ describe("skip-recorded publication", () => {inspectPublicationStore: rejecting("Ordinary previews stay offline."),},);- expect(ordinary.issues).toEqual(publication.issues);+ expect(ordinary).toEqual({+ scanId: "scan-example",+ uploadId: "scan-example",+ destination: publication.destination,+ created: [],+ failed: [],+ counts: { findings: 2, created: 0, failed: 0 },+ dryRun: true,+ issues: publication.issues,+ });expect(ordinary).not.toHaveProperty("skipped");const preview = await publishScanInternal("scan",@@ -1037,7 +1046,8 @@ describe("connected Linear publication", () => {test("rejects pre-aborted publication before preparing scans or touching local state", async () => {const publication = preparedPublication();const controller = new AbortController();- controller.abort(new Error("Publication was canceled before it started."));+ const reason = new Error("Publication was canceled before it started.");+ controller.abort(reason);const prepare = mock(resolving(publication));const preparePublicationStore = mock(async () => {});const resolveCodex = mock(unusedCodexCommand);@@ -1062,7 +1072,7 @@ describe("connected Linear publication", () => {},),),- ).rejects.toThrow("Publication was canceled before it started.");+ ).rejects.toBe(reason);expect(prepare).not.toHaveBeenCalled();expect(preparePublicationStore).not.toHaveBeenCalled();@@ -2872,60 +2882,6 @@ describe("connected Linear publication", () => {expect(runCodex).not.toHaveBeenCalled();});- test("previews every finding without starting Codex or writing a receipt", async () => {- const publication = preparedPublication(2);- const result = await publishScanInternal(- publication.scanDirectory,- { ...OPTIONS, dryRun: true },- dependencies(- publication,- {},- {- resolveCodex: throwing("dry runs must not resolve Codex"),- runCodex: rejecting("dry runs must not start Codex"),- writeReceipt: rejecting("dry runs must not write receipts"),- },- ),- );-- expect(result).toEqual({- scanId: "scan-example",- uploadId: "scan-example",- destination: publication.destination,- created: [],- failed: [],- counts: { findings: 2, created: 0, failed: 0 },- dryRun: true,- issues: publication.issues,- });- });-- test("rejects an already-aborted publication before preparing or starting Codex", async () => {- const publication = preparedPublication();- const controller = new AbortController();- const reason = new Error("Publication was canceled before startup.");- controller.abort(reason);- const prepare = mock(resolving(publication));- const runCodex = mock(successfulCodexResult);-- await expect(- publishScanInternal(- publication.scanDirectory,- { ...OPTIONS, signal: controller.signal },- dependencies(- publication,- {},- {- prepare,- runCodex,- },- ),- ),- ).rejects.toBe(reason);- expect(prepare).not.toHaveBeenCalled();- expect(runCodex).not.toHaveBeenCalled();- });-test("forwards cancellation and saves verified issues before reporting interruption", async () => {const publication = preparedPublication(2);const controller = new AbortController();