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 result context for Semgrep and Endor (#2219)

Michael D'Angelo9285a09f9417
diff --git a/sdk/typescript/src/endor-findings.ts b/sdk/typescript/src/endor-findings.ts
index f715b8fc1..33710c310 100644
--- a/sdk/typescript/src/endor-findings.ts
+++ b/sdk/typescript/src/endor-findings.ts
@@ -5,14 +5,13 @@ import { isRecord } from "./record.js";
import {
array,
hasSarifExternalPropertyReferences,
- indexSarifRules,
matchesSarifRuleReference,
normalizeRecords,
readScannerJson,
record,
relativePath,
requiredText,
- sarifRunContextSelector,
+ sarifResultContext,
strings,
text,
} from "./scanner-import.js";
@@ -199,23 +198,10 @@ export function normalizeEndorSarif(
hasSarifExternalPropertyReferences(run["externalPropertyFileReferences"])
)
throw new Error("Use a self-contained Endor SARIF report.");
- const rules = array(driver["rules"] ?? [], "Endor rules").map((r) =>
- record(r, "Endor rule"),
- );
- const rulesById = indexSarifRules(rules.entries());
- const { runs: _runs, ...documentContext } = document;
- const { results: _results, tool: _tool, ...runContext } = run;
- const { rules: _rules, ...driverContext } = driver;
- const { driver: _driver, ...toolContext } = tool;
- const selectRunContext = sarifRunContextSelector(runContext);
+ const context = sarifResultContext(document, run, tool, driver, "Endor");
return array(run["results"] ?? [], "Endor results").map((result) => ({
result,
- rulesById,
- selectRunContext,
- documentContext,
- runContext,
- driverContext,
- toolContext,
+ ...context,
}));
});
return normalizeRecords(
diff --git a/sdk/typescript/src/scanner-import.ts b/sdk/typescript/src/scanner-import.ts
index e0a594656..4f9c5cddf 100644
--- a/sdk/typescript/src/scanner-import.ts
+++ b/sdk/typescript/src/scanner-import.ts
@@ -224,6 +224,32 @@ export function indexSarifRules(
return byId;
}
+/** Keep the report and producer context shared by individual SARIF results. */
+export function sarifResultContext(
+ document: Record<string, unknown>,
+ run: Record<string, unknown>,
+ tool: Record<string, unknown>,
+ driver: Record<string, unknown>,
+ producer: "Endor" | "Semgrep",
+) {
+ const rules = array(driver["rules"] ?? [], `${producer} rules`).map((rule) =>
+ record(rule, `${producer} rule`),
+ );
+ const rulesById = indexSarifRules(rules.entries());
+ const { runs: _runs, ...documentContext } = document;
+ const { results: _results, tool: _tool, ...runContext } = run;
+ const { rules: _rules, ...driverContext } = driver;
+ const { driver: _driver, ...toolContext } = tool;
+ return {
+ rulesById,
+ selectRunContext: sarifRunContextSelector(runContext),
+ documentContext,
+ runContext,
+ driverContext,
+ toolContext,
+ };
+}
+
export function matchesSarifRuleReference(
reference: Record<string, unknown>,
ruleId: string,
diff --git a/sdk/typescript/src/semgrep-findings.ts b/sdk/typescript/src/semgrep-findings.ts
index 903d1c805..548375348 100644
--- a/sdk/typescript/src/semgrep-findings.ts
+++ b/sdk/typescript/src/semgrep-findings.ts
@@ -16,14 +16,13 @@ import {
import {
array,
hasSarifExternalPropertyReferences,
- indexSarifRules,
matchesSarifRuleReference,
normalizeRecords,
readScannerJson,
record,
relativePath,
requiredText,
- sarifRunContextSelector,
+ sarifResultContext,
stableId,
text,
} from "./scanner-import.js";
@@ -106,6 +105,16 @@ function sarifArtifactUri(
return base === "%SRCROOT%" ? uri : decodeURIComponent(uri);
}
+function primarySarifLocation(result: Record<string, unknown>) {
+ const locations = array(result["locations"] ?? [], "Semgrep locations");
+ if (locations.length !== 1)
+ throw new Error("Expected Semgrep's single primary result location.");
+ const location = record(locations[0], "Location");
+ const physical = record(location["physicalLocation"], "Physical location");
+ const artifact = record(physical["artifactLocation"], "Artifact location");
+ return { physical, artifact };
+}
+
function sarifIdentity(
value: unknown,
runContext: Record<string, unknown>,
@@ -117,17 +126,8 @@ function sarifIdentity(
],
);
if (supplied) return stableId("semgrep", [supplied]);
- const locations = array(result["locations"] ?? [], "Semgrep locations");
- if (locations.length !== 1)
- throw new Error("Expected Semgrep's single primary result location.");
- const physical = record(
- record(locations[0], "Location")["physicalLocation"],
- "Physical location",
- );
- const uri = sarifArtifactUri(
- record(physical["artifactLocation"], "Artifact location"),
- runContext,
- );
+ const { physical, artifact } = primarySarifLocation(result);
+ const uri = sarifArtifactUri(artifact, runContext);
if (uri === null)
throw new Error(
"Cannot derive a Semgrep CE identity from an indexed or unresolved base-relative artifact URI.",
@@ -228,29 +228,15 @@ export function normalizeSemgrepSarif(
)
throw new Error("Semgrep SARIF contains scan error notifications.");
}
- const rules = array(driver["rules"] ?? [], "Semgrep rules").map((r) =>
- record(r, "Semgrep rule"),
- );
- const rulesById = indexSarifRules(rules.entries());
- const { runs: _runs, ...documentContext } = document;
- const { results: _results, tool: _tool, ...runContext } = run;
- const { rules: _rules, ...driverContext } = driver;
- const { driver: _driver, ...toolContext } = tool;
- const selectRunContext = sarifRunContextSelector(runContext);
+ const context = sarifResultContext(document, run, tool, driver, "Semgrep");
return array(run["results"] ?? [], "Semgrep results").map((result) => ({
result,
- rulesById,
- selectRunContext,
- hasRules: rules.length > 0,
- documentContext,
- runContext,
- driverContext,
- toolContext,
+ ...context,
}));
});
return normalizeRecords(
entries,
- ({ result: value, rulesById, selectRunContext, hasRules, ...context }) => {
+ ({ result: value, rulesById, selectRunContext, ...context }) => {
const result = record(value, "Semgrep result");
const properties = record(
result["properties"] ?? {},
@@ -264,8 +250,8 @@ export function normalizeSemgrepSarif(
"This adapter supports Semgrep SAST; Supply Chain reports need separate qualification.",
);
const ruleId = requiredText(result["ruleId"], "Semgrep ruleId");
- const selectedRule = rulesById.get(ruleId);
- if (selectedRule === null || (hasRules && selectedRule === undefined))
+ const selectedRule = rulesById.get(ruleId) ?? undefined;
+ if (rulesById.size > 0 && !selectedRule)
throw new Error(
"Semgrep result must reference exactly one rule in its run.",
);
@@ -281,17 +267,7 @@ export function normalizeSemgrepSarif(
if (!matchesSarifRuleReference(ref, ruleId, selectedRule))
throw new Error("Inconsistent or external Semgrep rule reference.");
}
- const locations = array(result["locations"] ?? [], "Semgrep locations");
- if (locations.length !== 1)
- throw new Error("Expected Semgrep's single primary result location.");
- const physical = record(
- record(locations[0], "Location")["physicalLocation"],
- "Physical location",
- );
- const artifact = record(
- physical["artifactLocation"],
- "Artifact location",
- );
+ const { physical, artifact } = primarySarifLocation(result);
const rawPath = sarifArtifactUri(artifact, context.runContext);
const region = record(physical["region"], "Semgrep region");
const line = position(region["startLine"], "Start line");