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

perf: speed up confirmed finding matching (#808)

mldangelo-oaic8296885fbbf
diff --git a/sdk/typescript/src/scan-comparison.ts b/sdk/typescript/src/scan-comparison.ts
index b8e07203b..33d7379ab 100644
--- a/sdk/typescript/src/scan-comparison.ts
+++ b/sdk/typescript/src/scan-comparison.ts
@@ -1258,18 +1258,16 @@ function validateComparison(
...[...new Set(findingIds.values())].map((findingId) => [findingId]),
]);
if (enforceConfirmedIdentities) {
+ const occurrencesByFinding = Map.groupBy(
+ findingIds.keys(),
+ (occurrenceId) => findingIds.get(occurrenceId)!,
+ );
for (const knownGroup of confirmedGroups) {
- const knownFindingIds = new Set(knownGroup);
- const knownBefore = input.before.filter(({ occurrenceId }) => {
- const findingId = findingIds.get(occurrenceId);
- return findingId !== undefined && knownFindingIds.has(findingId);
- });
- const knownAfter = input.after.filter(({ occurrenceId }) => {
- const findingId = findingIds.get(occurrenceId);
- return findingId !== undefined && knownFindingIds.has(findingId);
- });
+ const knownOccurrences = knownGroup.flatMap(
+ (findingId) => occurrencesByFinding.get(findingId) ?? [],
+ );
const matchedGroups = new Set(
- [...knownBefore, ...knownAfter].flatMap(({ occurrenceId }) => {
+ knownOccurrences.flatMap((occurrenceId) => {
const group =
matchedBefore.get(occurrenceId) ?? matchedAfter.get(occurrenceId);
return group === undefined ? [] : [group];
@@ -1278,13 +1276,13 @@ function validateComparison(
if (
matchedGroups.size > 1 ||
(matchedGroups.size === 1 &&
- [...knownBefore, ...knownAfter].some(
- ({ occurrenceId }) =>
+ knownOccurrences.some(
+ (occurrenceId) =>
!matchedBefore.has(occurrenceId) &&
!matchedAfter.has(occurrenceId),
)) ||
- (knownBefore.length > 0 &&
- knownAfter.length > 0 &&
+ (knownOccurrences.some((occurrenceId) => beforeIds.has(occurrenceId)) &&
+ knownOccurrences.some((occurrenceId) => afterIds.has(occurrenceId)) &&
matchedGroups.size === 0)
) {
throw new CodexSecurityError(
diff --git a/sdk/typescript/tests-ts/api-credentials.test.ts b/sdk/typescript/tests-ts/api-credentials.test.ts
index 79dc8cfd5..ed69de78c 100644
--- a/sdk/typescript/tests-ts/api-credentials.test.ts
+++ b/sdk/typescript/tests-ts/api-credentials.test.ts
@@ -630,7 +630,7 @@ process.exit(process.exitCode ?? 0);
{ pluginPath: PLUGIN_ROOT },
{
environment: {
- ...process.env,
+ PATH: process.env["PATH"],
NODE_OPTIONS: `--import=${pathToFileURL(script).href}`,
CODEX_HOME: ambientHome,
CODEX_SECURITY_STATE_DIR: stateDir,
diff --git a/sdk/typescript/tests-ts/cli-authentication.test.ts b/sdk/typescript/tests-ts/cli-authentication.test.ts
index 56e1ef8d7..76c45852e 100644
--- a/sdk/typescript/tests-ts/cli-authentication.test.ts
+++ b/sdk/typescript/tests-ts/cli-authentication.test.ts
@@ -785,12 +785,10 @@ describe("CLI authentication", () => {
'cli_auth_credentials_store = "file"\n',
);
const environment = {
- ...process.env,
+ PATH: process.env["PATH"],
HOME: userHome,
USERPROFILE: userHome,
CODEX_HOME: configuredHome,
- OPENAI_API_KEY: undefined,
- CODEX_API_KEY: undefined,
};
const run = (args: string[], input?: string): number | null =>
spawnSync(
@@ -816,8 +814,9 @@ describe("CLI authentication", () => {
{
cwd: repository,
env: {
- ...process.env,
- CODEX_HOME: undefined,
+ PATH: process.env["PATH"],
+ HOME: root,
+ USERPROFILE: root,
Codex_Home: " ",
},
encoding: "utf8",
diff --git a/sdk/typescript/tests-ts/publication-store.test.ts b/sdk/typescript/tests-ts/publication-store.test.ts
index 722983acc..c908946de 100644
--- a/sdk/typescript/tests-ts/publication-store.test.ts
+++ b/sdk/typescript/tests-ts/publication-store.test.ts
@@ -249,6 +249,7 @@ describe("read-only publication history", () => {
destination: "linear" as const,
teamId: "team-example",
projectId: "project-example",
+ environment: fixture.environment,
};
const original = await prepareScanPublication(scanDirectory, options);
seedPublicationScan(fixture, original);
diff --git a/sdk/typescript/tests-ts/publication.test.ts b/sdk/typescript/tests-ts/publication.test.ts
index 6e321eb3e..104c8f0bb 100644
--- a/sdk/typescript/tests-ts/publication.test.ts
+++ b/sdk/typescript/tests-ts/publication.test.ts
@@ -11,7 +11,7 @@ import {
writeFile,
} from "node:fs/promises";
import { tmpdir } from "node:os";
-import { join } from "node:path";
+import { dirname, join } from "node:path";
import { afterEach, describe, expect, test } from "bun:test";
import { prepareScanPublication } from "../src/publication.js";
import type {
@@ -24,12 +24,17 @@ import { PLUGIN_ROOT } from "./plugin-root.js";
const EXAMPLE = join(PLUGIN_ROOT, "examples", "completed-scan");
const temporaryDirectories: string[] = [];
-const DESTINATION = {
- destination: "linear",
- teamId: "team_example",
- projectId: "project_example",
- uploadedAt: "2026-06-01T10:30:00Z",
-} as const;
+function publicationOptions(scanDirectory: string) {
+ return {
+ destination: "linear",
+ teamId: "team_example",
+ projectId: "project_example",
+ uploadedAt: "2026-06-01T10:30:00Z",
+ environment: {
+ CODEX_SECURITY_STATE_DIR: join(dirname(scanDirectory), "state"),
+ },
+ } as const;
+}
afterEach(async () => {
await Promise.all(
@@ -76,7 +81,7 @@ describe("scan publication preparation", () => {
await expect(
prepareScanPublication(scanDirectory, {
- ...DESTINATION,
+ ...publicationOptions(scanDirectory),
signal: controller.signal,
}),
).rejects.toBe(reason);
@@ -86,7 +91,7 @@ describe("scan publication preparation", () => {
const scanDirectory = await copyExample();
await expect(
prepareScanPublication(scanDirectory, {
- ...DESTINATION,
+ ...publicationOptions(scanDirectory),
expectedScanId: "another-scan",
}),
).rejects.toThrow(
@@ -95,7 +100,7 @@ describe("scan publication preparation", () => {
expect(
(
await prepareScanPublication(scanDirectory, {
- ...DESTINATION,
+ ...publicationOptions(scanDirectory),
expectedScanId: "scan_example_001",
})
).scanId,
@@ -106,7 +111,7 @@ describe("scan publication preparation", () => {
const scanDirectory = await copyExample();
const publication = await prepareScanPublication(
scanDirectory,
- DESTINATION,
+ publicationOptions(scanDirectory),
);
expect(publication).toMatchObject({
@@ -183,7 +188,10 @@ describe("scan publication preparation", () => {
await reseal(scanDirectory);
const { description } = (
- await prepareScanPublication(scanDirectory, DESTINATION)
+ await prepareScanPublication(
+ scanDirectory,
+ publicationOptions(scanDirectory),
+ )
).issues[0]!;
const metadata = description.indexOf("**Scan ID:**");
@@ -221,7 +229,7 @@ describe("scan publication preparation", () => {
const publication = await prepareScanPublication(
join(alias, "scan"),
- DESTINATION,
+ publicationOptions(scanDirectory),
);
expect(publication.scanDirectory).toBe(await realpath(scanDirectory));
@@ -246,7 +254,10 @@ describe("scan publication preparation", () => {
await reseal(scanDirectory);
const { description } = (
- await prepareScanPublication(scanDirectory, DESTINATION)
+ await prepareScanPublication(
+ scanDirectory,
+ publicationOptions(scanDirectory),
+ )
).issues[0]!;
expect(description).toContain(`**Coverage mode:** ${mode}`);
@@ -261,6 +272,7 @@ describe("scan publication preparation", () => {
destination: "linear",
teamId: "team_example",
uploadedAt: "2026-06-01T10:30:00Z",
+ environment: publicationOptions(scanDirectory).environment,
});
expect(publication.destination).toEqual({
@@ -325,7 +337,10 @@ describe("scan publication preparation", () => {
await reseal(scanDirectory);
const { description } = (
- await prepareScanPublication(scanDirectory, DESTINATION)
+ await prepareScanPublication(
+ scanDirectory,
+ publicationOptions(scanDirectory),
+ )
).issues[0]!;
expect(description).toContain("**Root control:** `src/archive.py:12`");
@@ -362,7 +377,10 @@ describe("scan publication preparation", () => {
await writeJson(manifestPath, manifest);
const { description } = (
- await prepareScanPublication(scanDirectory, DESTINATION)
+ await prepareScanPublication(
+ scanDirectory,
+ publicationOptions(scanDirectory),
+ )
).issues[0]!;
expect(description).toContain(
@@ -385,7 +403,10 @@ describe("scan publication preparation", () => {
await writeJson(manifestPath, manifest);
const { description } = (
- await prepareScanPublication(scanDirectory, DESTINATION)
+ await prepareScanPublication(
+ scanDirectory,
+ publicationOptions(scanDirectory),
+ )
).issues[0]!;
expect(description).toContain("**Remote:** ssh://github.com/example/repo");
@@ -430,7 +451,10 @@ describe("scan publication preparation", () => {
await reseal(scanDirectory);
const { description } = (
- await prepareScanPublication(scanDirectory, DESTINATION)
+ await prepareScanPublication(
+ scanDirectory,
+ publicationOptions(scanDirectory),
+ )
).issues[0]!;
expect(description).toContain(
@@ -459,8 +483,12 @@ describe("scan publication preparation", () => {
await writeJson(findingsPath, findings);
await reseal(scanDirectory);
- const issue = (await prepareScanPublication(scanDirectory, DESTINATION))
- .issues[0]!;
+ const issue = (
+ await prepareScanPublication(
+ scanDirectory,
+ publicationOptions(scanDirectory),
+ )
+ ).issues[0]!;
expect(issue.title).toStartWith(
`[Codex Security][${severity.toUpperCase()}] `,
);
@@ -478,7 +506,12 @@ describe("scan publication preparation", () => {
await reseal(scanDirectory);
expect(
- (await prepareScanPublication(scanDirectory, DESTINATION)).issues,
+ (
+ await prepareScanPublication(
+ scanDirectory,
+ publicationOptions(scanDirectory),
+ )
+ ).issues,
).toEqual([]);
});
@@ -490,7 +523,7 @@ describe("scan publication preparation", () => {
await writeJson(findingsPath, findings);
await expect(
- prepareScanPublication(scanDirectory, DESTINATION),
+ prepareScanPublication(scanDirectory, publicationOptions(scanDirectory)),
).rejects.toThrow();
});
});
diff --git a/sdk/typescript/tests-ts/scan-comparison.property.test.ts b/sdk/typescript/tests-ts/scan-comparison.property.test.ts
index d6b27bd90..c2950abf6 100644
--- a/sdk/typescript/tests-ts/scan-comparison.property.test.ts
+++ b/sdk/typescript/tests-ts/scan-comparison.property.test.ts
@@ -49,6 +49,80 @@ function compare(
}
describe("finding-comparison invariants", () => {
+ test("keeps transitive identity groups separate across repeated occurrences", async () => {
+ await fc.assert(
+ fc.asyncProperty(
+ fc.array(
+ fc.record({
+ before: fc.integer({ min: 0, max: 4 }),
+ after: fc.integer({ min: 0, max: 4 }),
+ }),
+ { minLength: 1, maxLength: 20 },
+ ),
+ async (counts) => {
+ const before = counts.flatMap((count, group) =>
+ Array.from({ length: count.before }, (_, occurrence) => ({
+ occurrenceId: `before-${group}-${occurrence}`,
+ findingId: `old-${group}`,
+ })),
+ );
+ const after = counts.flatMap((count, group) =>
+ Array.from({ length: count.after }, (_, occurrence) => ({
+ occurrenceId: `after-${group}-${occurrence}`,
+ findingId: `new-${group}`,
+ })),
+ );
+ const knownFindingGroups = counts.flatMap((_, group) => [
+ [`old-${group}`, `historical-${group}`],
+ [`historical-${group}`, `new-${group}`],
+ ]);
+ const expected = counts
+ .flatMap((count, group) =>
+ count.before > 0 && count.after > 0
+ ? [
+ [
+ Array.from(
+ { length: count.before },
+ (_, occurrence) => `before-${group}-${occurrence}`,
+ ),
+ Array.from(
+ { length: count.after },
+ (_, occurrence) => `after-${group}-${occurrence}`,
+ ),
+ ],
+ ]
+ : [],
+ )
+ .sort();
+
+ for (const input of [
+ { before, after, knownFindingGroups },
+ {
+ before: [...before].reverse(),
+ after: [...after].reverse(),
+ knownFindingGroups: [...knownFindingGroups].reverse(),
+ },
+ ]) {
+ const result = await compare(input, {
+ matches: [],
+ uncertain: [],
+ });
+ expect(
+ result.matches
+ .map(({ beforeOccurrenceIds, afterOccurrenceIds }) => [
+ [...beforeOccurrenceIds].sort(),
+ [...afterOccurrenceIds].sort(),
+ ])
+ .sort(),
+ ).toEqual(expected);
+ expect(result.uncertain).toEqual([]);
+ }
+ },
+ ),
+ propertyOptions,
+ );
+ });
+
test("preserves valid identities regardless of finding order", async () => {
await fc.assert(
fc.asyncProperty(identities, async (ids) => {