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(imports): share SARIF rule indexing and reference checks (#2200)

Michael D'Angelo78b300b1d6fe
diff --git a/sdk/typescript/src/endor-findings.ts b/sdk/typescript/src/endor-findings.ts
index 62bfffde1..f715b8fc1 100644
--- a/sdk/typescript/src/endor-findings.ts
+++ b/sdk/typescript/src/endor-findings.ts
@@ -6,6 +6,7 @@ import {
array,
hasSarifExternalPropertyReferences,
indexSarifRules,
+ matchesSarifRuleReference,
normalizeRecords,
readScannerJson,
record,
@@ -201,7 +202,7 @@ export function normalizeEndorSarif(
const rules = array(driver["rules"] ?? [], "Endor rules").map((r) =>
record(r, "Endor rule"),
);
- const rulesById = indexSarifRules(rules);
+ const rulesById = indexSarifRules(rules.entries());
const { runs: _runs, ...documentContext } = document;
const { results: _results, tool: _tool, ...runContext } = run;
const { rules: _rules, ...driverContext } = driver;
@@ -240,14 +241,7 @@ export function normalizeEndorSarif(
throw new Error("Endor ruleIndex and ruleId disagree.");
if (result["rule"] !== undefined) {
const reference = record(result["rule"], "Endor rule reference");
- if (
- reference["toolComponent"] !== undefined ||
- (reference["id"] !== undefined && reference["id"] !== ruleId) ||
- (reference["index"] !== undefined &&
- reference["index"] !== ruleIndex) ||
- (reference["guid"] !== undefined &&
- reference["guid"] !== rule["guid"])
- )
+ if (!matchesSarifRuleReference(reference, ruleId, selectedRule))
throw new Error("Inconsistent or external Endor rule reference.");
}
const message = record(result["message"], "Endor message");
diff --git a/sdk/typescript/src/scanner-import.ts b/sdk/typescript/src/scanner-import.ts
index d9f92f425..e0a594656 100644
--- a/sdk/typescript/src/scanner-import.ts
+++ b/sdk/typescript/src/scanner-import.ts
@@ -210,19 +210,35 @@ export function sarifRunContextSelector(runContext: Record<string, unknown>) {
};
}
+type IndexedSarifRule = { rule: Record<string, unknown>; index: number };
+
/** Duplicate IDs cannot identify a single rule in this run. */
-export function indexSarifRules(rules: Record<string, unknown>[]) {
- const byId = new Map<
- unknown,
- { rule: Record<string, unknown>; index: number } | null
- >();
- for (const [index, rule] of rules.entries()) {
+export function indexSarifRules(
+ entries: Iterable<[number, Record<string, unknown>]>,
+) {
+ const byId = new Map<unknown, IndexedSarifRule | null>();
+ for (const [index, rule] of entries) {
const id = rule["id"];
byId.set(id, byId.has(id) ? null : { rule, index });
}
return byId;
}
+export function matchesSarifRuleReference(
+ reference: Record<string, unknown>,
+ ruleId: string,
+ selected: IndexedSarifRule | undefined,
+) {
+ return (
+ reference["toolComponent"] === undefined &&
+ (reference["id"] === undefined || reference["id"] === ruleId) &&
+ (reference["index"] === undefined ||
+ reference["index"] === selected?.index) &&
+ (reference["guid"] === undefined ||
+ reference["guid"] === selected?.rule["guid"])
+ );
+}
+
/** Exclude every colliding record: choosing the first could overwrite another occurrence. */
export function normalizeRecords<T>(
records: T[],
diff --git a/sdk/typescript/src/semgrep-findings.ts b/sdk/typescript/src/semgrep-findings.ts
index 95f1b3665..903d1c805 100644
--- a/sdk/typescript/src/semgrep-findings.ts
+++ b/sdk/typescript/src/semgrep-findings.ts
@@ -17,6 +17,7 @@ import {
array,
hasSarifExternalPropertyReferences,
indexSarifRules,
+ matchesSarifRuleReference,
normalizeRecords,
readScannerJson,
record,
@@ -230,7 +231,7 @@ export function normalizeSemgrepSarif(
const rules = array(driver["rules"] ?? [], "Semgrep rules").map((r) =>
record(r, "Semgrep rule"),
);
- const rulesById = indexSarifRules(rules);
+ const rulesById = indexSarifRules(rules.entries());
const { runs: _runs, ...documentContext } = document;
const { results: _results, tool: _tool, ...runContext } = run;
const { rules: _rules, ...driverContext } = driver;
@@ -277,12 +278,7 @@ export function normalizeSemgrepSarif(
throw new Error("Semgrep ruleIndex and ruleId disagree.");
if (result["rule"] !== undefined) {
const ref = record(result["rule"], "Semgrep rule reference");
- if (
- ref["toolComponent"] !== undefined ||
- (ref["id"] !== undefined && ref["id"] !== ruleId) ||
- (ref["index"] !== undefined && ref["index"] !== ruleIndex) ||
- (ref["guid"] !== undefined && ref["guid"] !== rule?.["guid"])
- )
+ if (!matchesSarifRuleReference(ref, ruleId, selectedRule))
throw new Error("Inconsistent or external Semgrep rule reference.");
}
const locations = array(result["locations"] ?? [], "Semgrep locations");
diff --git a/sdk/typescript/src/snyk-findings.ts b/sdk/typescript/src/snyk-findings.ts
index a351f4ca6..85e51c3ab 100644
--- a/sdk/typescript/src/snyk-findings.ts
+++ b/sdk/typescript/src/snyk-findings.ts
@@ -5,6 +5,8 @@ import { isRecord } from "./record.js";
import {
array,
hasSarifExternalPropertyReferences,
+ indexSarifRules,
+ matchesSarifRuleReference,
normalizeRecords,
readScannerJson,
record,
@@ -263,14 +265,10 @@ export function normalizeSnykSarif(
const rules = array(driver["rules"], "SARIF rules").map((rule) =>
record(rule, "SARIF rule"),
);
- const rulesById = new Map<
- unknown,
- { rule: Record<string, unknown>; index: number } | null
- >();
- rules.forEach((rule, index) => {
- const id = rule["id"];
- rulesById.set(id, rulesById.has(id) ? null : { rule, index });
- });
+ // SDK inputs can leave descriptor slots empty. Keep the original rule indexes.
+ const rulesById = indexSarifRules(
+ rules.entries().filter(([index]) => index in rules),
+ );
const project = snykSarifProject(run);
const { results: _results, tool: _tool, ...runContext } = run;
const { rules: _rules, ...driverContext } = driver;
@@ -309,12 +307,7 @@ export function normalizeSnykSarif(
throw new Error("Result ruleIndex and ruleId disagree.");
if (result["rule"] !== undefined) {
const ref = record(result["rule"], "Rule reference");
- if (
- ref["toolComponent"] !== undefined ||
- (ref["id"] !== undefined && ref["id"] !== id) ||
- (ref["index"] !== undefined && ref["index"] !== index) ||
- (ref["guid"] !== undefined && ref["guid"] !== rule["guid"])
- )
+ if (!matchesSarifRuleReference(ref, id, match))
throw new Error(
"Result uses an inconsistent or external rule reference.",
);