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

refactor(sdk): share finding-group logic across scan comparisons (#1364)

mldangelo-oai2b759675b283
diff --git a/sdk/typescript/src/deduplication/deduplication.ts b/sdk/typescript/src/deduplication/deduplication.ts
index baa54e921..a1454bfcc 100644
--- a/sdk/typescript/src/deduplication/deduplication.ts
+++ b/sdk/typescript/src/deduplication/deduplication.ts
@@ -1,3 +1,4 @@
+import { findingGroupRoots } from "../finding-catalogue.js";
import type { Finding } from "../models.js";
import type { FindingNeighborhood } from "../finding-retrieval.js";
import {
@@ -338,41 +339,24 @@ export class FindingDeduplicator {
else rejected.push(state.ids);
}
- const adjacent = new Map<string, Set<string>>();
- for (const pair of supported) {
- for (const [left, right] of [pair, [pair[1], pair[0]]] as const) {
- const neighbors = adjacent.get(left) ?? new Set<string>();
- neighbors.add(right);
- adjacent.set(left, neighbors);
- }
- }
-
- const components: {
- members: string[];
- supported: [string, string][];
- rejected: [string, string][];
- }[] = [];
- const componentByFinding = new Map<string, (typeof components)[number]>();
- for (const id of findings.keys()) {
- this.signal?.throwIfAborted();
- if (!adjacent.has(id) || componentByFinding.has(id)) continue;
- const component: (typeof components)[number] = {
- members: [],
- supported: [],
- rejected: [],
- };
- components.push(component);
- const pending = [id];
- while (pending.length > 0) {
- const member = pending.pop()!;
- if (componentByFinding.has(member)) continue;
- componentByFinding.set(member, component);
- pending.push(...adjacent.get(member)!);
- }
- }
+ const { parents, root } = findingGroupRoots();
+ for (const [left, right] of supported) parents.set(root(right), root(left));
// Preserve finding insertion order for contradiction-grouping ties.
- for (const id of findings.keys())
- componentByFinding.get(id)?.members.push(id);
+ const membersByRoot = Map.groupBy(findings.keys(), (id) => {
+ this.signal?.throwIfAborted();
+ return parents.has(id) ? root(id) : undefined;
+ });
+ membersByRoot.delete(undefined);
+ const components = [...membersByRoot.values()].map((members) => ({
+ members,
+ supported: [] as [string, string][],
+ rejected: [] as [string, string][],
+ }));
+ const componentByFinding = new Map(
+ components.flatMap((component) =>
+ component.members.map((id) => [id, component] as const),
+ ),
+ );
for (const pair of supported)
componentByFinding.get(pair[0])!.supported.push(pair);
for (const pair of rejected) {
diff --git a/sdk/typescript/src/finding-catalogue.ts b/sdk/typescript/src/finding-catalogue.ts
index aab2a800c..d728b7d93 100644
--- a/sdk/typescript/src/finding-catalogue.ts
+++ b/sdk/typescript/src/finding-catalogue.ts
@@ -58,18 +58,14 @@ export function findingCatalogue(
const latest = occurrences.at(-1)!;
const card = compactFinding(latest);
if (occurrences.length > 1) {
- const description = (finding: ComparisonFinding) => {
- const value: Record<string, unknown> = { ...compactFinding(finding) };
- delete value["occurrenceId"];
- delete value["findingId"];
- return value;
- };
- const current = description(latest);
+ const current = compactFinding(latest);
const seen = new Set<string>();
const aliases = occurrences.slice(0, -1).flatMap((finding) => {
const value = Object.fromEntries(
- Object.entries(description(finding)).filter(
+ Object.entries(compactFinding(finding)).filter(
([field, value]) =>
+ field !== "occurrenceId" &&
+ field !== "findingId" &&
JSON.stringify(value) !== JSON.stringify(current[field]),
),
);
diff --git a/sdk/typescript/src/scan-comparison.ts b/sdk/typescript/src/scan-comparison.ts
index c7f74916d..ed938b16a 100644
--- a/sdk/typescript/src/scan-comparison.ts
+++ b/sdk/typescript/src/scan-comparison.ts
@@ -837,7 +837,7 @@ function reconcileComparison(
related: response.related.filter(isSeparateGroup),
}),
};
- validateComparison(input, comparison, allowHistoricalUncertainty, true);
+ validateComparison(input, comparison, allowHistoricalUncertainty);
return { comparison, complete: matches.length === groups.length };
}
@@ -1177,15 +1177,11 @@ function validateComparison(
input: ScanComparisonInput,
response: ScanComparisonResult,
allowHistoricalUncertainty: boolean,
- enforceConfirmedIdentities = false,
): void {
const beforeIds = new Set(
input.before.map(({ occurrenceId }) => occurrenceId),
);
const afterIds = new Set(input.after.map(({ occurrenceId }) => occurrenceId));
- const findingIds = new Map(
- [...input.before, ...input.after].flatMap(findingIdEntry),
- );
const matchedBefore = new Map<string, number>();
const matchedAfter = new Map<string, number>();
const uncertainPairs = new Set<string>();
@@ -1211,55 +1207,11 @@ function validateComparison(
}
}
- const confirmedGroups = unionFindingGroups([
- ...(input.knownFindingGroups ?? []),
- ...[...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 knownOccurrences = knownGroup.flatMap(
- (findingId) => occurrencesByFinding.get(findingId) ?? [],
- );
- const matchedGroups = new Set(
- knownOccurrences.flatMap((occurrenceId) => {
- const group =
- matchedBefore.get(occurrenceId) ?? matchedAfter.get(occurrenceId);
- return group === undefined ? [] : [group];
- }),
- );
- if (
- matchedGroups.size > 1 ||
- (matchedGroups.size === 1 &&
- knownOccurrences.some(
- (occurrenceId) =>
- !matchedBefore.has(occurrenceId) &&
- !matchedAfter.has(occurrenceId),
- )) ||
- (knownOccurrences.some((occurrenceId) => beforeIds.has(occurrenceId)) &&
- knownOccurrences.some((occurrenceId) => afterIds.has(occurrenceId)) &&
- matchedGroups.size === 0)
- ) {
- throw new CodexSecurityError(
- "Scan comparison contradicts previously confirmed finding groups.",
- );
- }
- }
- }
-
for (const candidate of response.uncertain) {
- const beforeFindingId = findingIds.get(candidate.beforeOccurrenceId);
- const afterFindingId = findingIds.get(candidate.afterOccurrenceId);
if (
!beforeIds.has(candidate.beforeOccurrenceId) ||
matchedBefore.has(candidate.beforeOccurrenceId) ||
!afterIds.has(candidate.afterOccurrenceId) ||
- (enforceConfirmedIdentities &&
- beforeFindingId !== undefined &&
- beforeFindingId === afterFindingId) ||
(!allowHistoricalUncertainty &&
matchedAfter.has(candidate.afterOccurrenceId))
) {
@@ -1279,24 +1231,9 @@ function validateComparison(
uncertainPairs.add(pair);
}
- const knownGroupByFindingId = new Map(
- confirmedGroups.flatMap((group, index) =>
- group.map((findingId) => [findingId, index] as const),
- ),
- );
const relatedPairs = new Set<string>();
for (const candidate of response.related ?? []) {
const beforeGroup = matchedBefore.get(candidate.beforeOccurrenceId);
- const beforeFindingId = findingIds.get(candidate.beforeOccurrenceId);
- const afterFindingId = findingIds.get(candidate.afterOccurrenceId);
- const knownBeforeGroup =
- beforeFindingId === undefined
- ? undefined
- : knownGroupByFindingId.get(beforeFindingId);
- const knownAfterGroup =
- afterFindingId === undefined
- ? undefined
- : knownGroupByFindingId.get(afterFindingId);
const pair = JSON.stringify([
candidate.beforeOccurrenceId,
candidate.afterOccurrenceId,
@@ -1306,9 +1243,6 @@ function validateComparison(
!afterIds.has(candidate.afterOccurrenceId) ||
(beforeGroup !== undefined &&
beforeGroup === matchedAfter.get(candidate.afterOccurrenceId)) ||
- (enforceConfirmedIdentities &&
- knownBeforeGroup !== undefined &&
- knownBeforeGroup === knownAfterGroup) ||
uncertainPairs.has(pair) ||
relatedPairs.has(pair)
) {