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(imports): share SARIF rule reference validation (#2235)
Michael D'Angelodc2448826fd9
diff --git a/sdk/typescript/src/endor-findings.ts b/sdk/typescript/src/endor-findings.tsindex 33710c310..97a288926 100644--- a/sdk/typescript/src/endor-findings.ts+++ b/sdk/typescript/src/endor-findings.ts@@ -5,7 +5,6 @@ import { isRecord } from "./record.js";import {array,hasSarifExternalPropertyReferences,- matchesSarifRuleReference,normalizeRecords,readScannerJson,record,@@ -14,6 +13,7 @@ import {sarifResultContext,strings,text,+ validateSarifRuleReference,} from "./scanner-import.js";/** @internal Repository claims retained in the adapter's original SARIF context. */@@ -219,17 +219,8 @@ export function normalizeEndorSarif(throw new Error("Endor result must reference exactly one rule in its run.",);- const { rule, index: ruleIndex } = selectedRule;- if (- result["ruleIndex"] !== undefined &&- result["ruleIndex"] !== ruleIndex- )- throw new Error("Endor ruleIndex and ruleId disagree.");- if (result["rule"] !== undefined) {- const reference = record(result["rule"], "Endor rule reference");- if (!matchesSarifRuleReference(reference, ruleId, selectedRule))- throw new Error("Inconsistent or external Endor rule reference.");- }+ const { rule } = selectedRule;+ validateSarifRuleReference(result, ruleId, selectedRule, "Endor");const message = record(result["message"], "Endor message");const short = record(rule["shortDescription"] ?? {},diff --git a/sdk/typescript/src/scanner-import.ts b/sdk/typescript/src/scanner-import.tsindex 4f9c5cddf..f56a1e68c 100644--- a/sdk/typescript/src/scanner-import.ts+++ b/sdk/typescript/src/scanner-import.ts@@ -250,19 +250,38 @@ export function sarifResultContext(};}-export function matchesSarifRuleReference(- reference: Record<string, unknown>,+/** The top-level index and nested descriptor must agree with the selected driver rule. */+export function validateSarifRuleReference(+ result: Record<string, unknown>,ruleId: string,selected: IndexedSarifRule | undefined,+ producer: "Endor" | "Semgrep" | "Snyk",) {- 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"])+ // Snyk's original diagnostics use unqualified SARIF field names.+ const label = producer === "Snyk" ? "Result" : producer;+ if (+ result["ruleIndex"] !== undefined &&+ result["ruleIndex"] !== selected?.index+ )+ throw new Error(`${label} ruleIndex and ruleId disagree.`);+ if (result["rule"] === undefined) return;+ const reference = record(+ result["rule"],+ producer === "Snyk" ? "Rule reference" : `${producer} rule reference`,);+ if (+ reference["toolComponent"] !== undefined ||+ (reference["id"] !== undefined && reference["id"] !== ruleId) ||+ (reference["index"] !== undefined &&+ reference["index"] !== selected?.index) ||+ (reference["guid"] !== undefined &&+ reference["guid"] !== selected?.rule["guid"])+ )+ throw new Error(+ producer === "Snyk"+ ? "Result uses an inconsistent or external rule reference."+ : `Inconsistent or external ${producer} rule reference.`,+ );}/** Exclude every colliding record: choosing the first could overwrite another occurrence. */diff --git a/sdk/typescript/src/semgrep-findings.ts b/sdk/typescript/src/semgrep-findings.tsindex 548375348..c53d7501e 100644--- a/sdk/typescript/src/semgrep-findings.ts+++ b/sdk/typescript/src/semgrep-findings.ts@@ -16,7 +16,6 @@ import {import {array,hasSarifExternalPropertyReferences,- matchesSarifRuleReference,normalizeRecords,readScannerJson,record,@@ -25,6 +24,7 @@ import {sarifResultContext,stableId,text,+ validateSarifRuleReference,} from "./scanner-import.js";export interface SemgrepImportOptions {@@ -256,17 +256,7 @@ export function normalizeSemgrepSarif("Semgrep result must reference exactly one rule in its run.",);const rule = selectedRule?.rule;- const ruleIndex = selectedRule?.index;- if (- result["ruleIndex"] !== undefined &&- result["ruleIndex"] !== ruleIndex- )- throw new Error("Semgrep ruleIndex and ruleId disagree.");- if (result["rule"] !== undefined) {- const ref = record(result["rule"], "Semgrep rule reference");- if (!matchesSarifRuleReference(ref, ruleId, selectedRule))- throw new Error("Inconsistent or external Semgrep rule reference.");- }+ validateSarifRuleReference(result, ruleId, selectedRule, "Semgrep");const { physical, artifact } = primarySarifLocation(result);const rawPath = sarifArtifactUri(artifact, context.runContext);const region = record(physical["region"], "Semgrep region");diff --git a/sdk/typescript/src/snyk-findings.ts b/sdk/typescript/src/snyk-findings.tsindex 85e51c3ab..9360e1660 100644--- a/sdk/typescript/src/snyk-findings.ts+++ b/sdk/typescript/src/snyk-findings.ts@@ -6,7 +6,6 @@ import {array,hasSarifExternalPropertyReferences,indexSarifRules,- matchesSarifRuleReference,normalizeRecords,readScannerJson,record,@@ -15,6 +14,7 @@ import {stableId,strings,text,+ validateSarifRuleReference,} from "./scanner-import.js";function snykIdentity(value: unknown, project: string | null): string {@@ -302,16 +302,8 @@ export function normalizeSnykSarif(const match = rulesById.get(id);if (!match)throw new Error("Result must reference exactly one rule in its run.");- const { rule, index } = match;- if (result["ruleIndex"] !== undefined && result["ruleIndex"] !== index)- throw new Error("Result ruleIndex and ruleId disagree.");- if (result["rule"] !== undefined) {- const ref = record(result["rule"], "Rule reference");- if (!matchesSarifRuleReference(ref, id, match))- throw new Error(- "Result uses an inconsistent or external rule reference.",- );- }+ const { rule } = match;+ validateSarifRuleReference(result, id, match, "Snyk");const help = requiredText(record(rule["help"], "Rule help")["markdown"],"Rule help markdown",