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(sdk): index SARIF artifact URIs once per run (#2191)

Michael D'Angeloa1558f24dc6f
diff --git a/sdk/typescript/src/endor-findings.ts b/sdk/typescript/src/endor-findings.ts
index 3c0c6ba91..62bfffde1 100644
--- a/sdk/typescript/src/endor-findings.ts
+++ b/sdk/typescript/src/endor-findings.ts
@@ -11,7 +11,7 @@ import {
record,
relativePath,
requiredText,
- sarifRunContextForResult,
+ sarifRunContextSelector,
strings,
text,
} from "./scanner-import.js";
@@ -206,9 +206,11 @@ export function normalizeEndorSarif(
const { results: _results, tool: _tool, ...runContext } = run;
const { rules: _rules, ...driverContext } = driver;
const { driver: _driver, ...toolContext } = tool;
+ const selectRunContext = sarifRunContextSelector(runContext);
return array(run["results"] ?? [], "Endor results").map((result) => ({
result,
rulesById,
+ selectRunContext,
documentContext,
runContext,
driverContext,
@@ -217,7 +219,7 @@ export function normalizeEndorSarif(
});
return normalizeRecords(
entries,
- ({ result: value, rulesById, ...context }) => {
+ ({ result: value, rulesById, selectRunContext, ...context }) => {
const result = record(value, "Endor result");
const properties = record(
result["properties"],
@@ -320,8 +322,7 @@ export function normalizeEndorSarif(
],
source_data: structuredClone({
...context,
- runContext: sarifRunContextForResult(
- context.runContext,
+ runContext: selectRunContext(
result,
rule,
context.documentContext,
diff --git a/sdk/typescript/src/scanner-import.ts b/sdk/typescript/src/scanner-import.ts
index 7b1da57d3..ce214decc 100644
--- a/sdk/typescript/src/scanner-import.ts
+++ b/sdk/typescript/src/scanner-import.ts
@@ -120,13 +120,7 @@ const sarifArtifactLocationFields = new Set([
"displayBase",
]);
-/** Keep original artifact indexes; omit only inventory unrelated to this finding. */
-export function sarifRunContextForResult(
- runContext: Record<string, unknown>,
- ...selectedEvidence: unknown[]
-): Record<string, unknown> {
- const { artifacts, ...context } = runContext;
- if (!Array.isArray(artifacts) || artifacts.length === 0) return runContext;
+function sarifArtifactReferences(evidence: unknown) {
const locations: Record<string, unknown>[] = [];
let hasArtifactLink = false;
function visit(value: unknown): void {
@@ -151,28 +145,56 @@ export function sarifRunContextForResult(
visit(child);
}
}
- visit([context, ...selectedEvidence]);
- if (hasArtifactLink) return runContext;
- const unresolvedUri = /[%:\\]|(?:^|\/)\.\.?(?:\/|$)/u;
- for (const location of locations) {
- if (typeof location["index"] === "number" && location["index"] >= 0)
- return runContext;
- if (typeof location["uri"] !== "string") continue;
- for (const artifact of artifacts) {
- if (!isRecord(artifact) || !isRecord(artifact["location"])) continue;
- const candidate = artifact["location"];
- // Keep URI spellings that need resolution; evidence is never rewritten.
- if (
- location["uriBaseId"] !== candidate["uriBaseId"] ||
- unresolvedUri.test(location["uri"]) ||
- (typeof candidate["uri"] === "string" &&
- unresolvedUri.test(candidate["uri"])) ||
- location["uri"] === candidate["uri"]
- )
- return runContext;
- }
+ visit(evidence);
+ return { locations, hasArtifactLink };
+}
+
+const unresolvedUri = /[%:\\]|(?:^|\/)\.\.?(?:\/|$)/u;
+
+function artifactUriMatcher(artifacts: unknown[]) {
+ const uris = new Set<string>();
+ let hasLocation = false;
+ let base: unknown;
+ for (const artifact of artifacts) {
+ if (!isRecord(artifact) || !isRecord(artifact["location"])) continue;
+ const { uri, uriBaseId } = artifact["location"];
+ // Mixed bases or a URI that needs resolution can match any direct reference.
+ if (
+ (hasLocation && uriBaseId !== base) ||
+ (typeof uri === "string" && unresolvedUri.test(uri))
+ )
+ return () => true;
+ hasLocation = true;
+ base = uriBaseId;
+ if (typeof uri === "string") uris.add(uri);
}
- return context;
+ return (uri: string, uriBaseId: unknown) =>
+ hasLocation &&
+ (uriBaseId !== base || unresolvedUri.test(uri) || uris.has(uri));
+}
+
+/** Keep original artifact indexes; omit only inventory unrelated to this finding. */
+export function sarifRunContextSelector(runContext: Record<string, unknown>) {
+ const { artifacts, ...context } = runContext;
+ if (!Array.isArray(artifacts) || artifacts.length === 0)
+ return () => runContext;
+ let matchesUri: ReturnType<typeof artifactUriMatcher> | undefined;
+ return (...selectedEvidence: unknown[]) => {
+ const { locations, hasArtifactLink } = sarifArtifactReferences([
+ context,
+ ...selectedEvidence,
+ ]);
+ if (hasArtifactLink) return runContext;
+ for (const location of locations) {
+ const { index, uri, uriBaseId } = location;
+ if (typeof index === "number" && index >= 0) return runContext;
+ if (typeof uri === "string") {
+ matchesUri ??= artifactUriMatcher(artifacts);
+ if (matchesUri(uri, uriBaseId)) return runContext;
+ }
+ }
+ return context;
+ };
}
/** Duplicate IDs cannot identify a single rule in this run. */
diff --git a/sdk/typescript/src/semgrep-findings.ts b/sdk/typescript/src/semgrep-findings.ts
index b4af2f253..95f1b3665 100644
--- a/sdk/typescript/src/semgrep-findings.ts
+++ b/sdk/typescript/src/semgrep-findings.ts
@@ -22,7 +22,7 @@ import {
record,
relativePath,
requiredText,
- sarifRunContextForResult,
+ sarifRunContextSelector,
stableId,
text,
} from "./scanner-import.js";
@@ -235,9 +235,11 @@ export function normalizeSemgrepSarif(
const { results: _results, tool: _tool, ...runContext } = run;
const { rules: _rules, ...driverContext } = driver;
const { driver: _driver, ...toolContext } = tool;
+ const selectRunContext = sarifRunContextSelector(runContext);
return array(run["results"] ?? [], "Semgrep results").map((result) => ({
result,
rulesById,
+ selectRunContext,
hasRules: rules.length > 0,
documentContext,
runContext,
@@ -247,7 +249,7 @@ export function normalizeSemgrepSarif(
});
return normalizeRecords(
entries,
- ({ result: value, rulesById, hasRules, ...context }) => {
+ ({ result: value, rulesById, selectRunContext, hasRules, ...context }) => {
const result = record(value, "Semgrep result");
const properties = record(
result["properties"] ?? {},
@@ -357,8 +359,7 @@ export function normalizeSemgrepSarif(
},
source_data: structuredClone({
...context,
- runContext: sarifRunContextForResult(
- context.runContext,
+ runContext: selectRunContext(
result,
rule,
context.driverContext,
diff --git a/sdk/typescript/src/snyk-findings.ts b/sdk/typescript/src/snyk-findings.ts
index 95db390c2..a351f4ca6 100644
--- a/sdk/typescript/src/snyk-findings.ts
+++ b/sdk/typescript/src/snyk-findings.ts
@@ -9,7 +9,7 @@ import {
readScannerJson,
record,
requiredText,
- sarifRunContextForResult,
+ sarifRunContextSelector,
stableId,
strings,
text,
@@ -280,11 +280,12 @@ export function normalizeSnykSarif(
);
runContext["tool"] = toolContext;
const { runs: _runs, ...documentContext } = document;
+ const selectRunContext = sarifRunContextSelector(runContext);
return array(run["results"], "SARIF results").map((result) => ({
result,
rulesById,
project,
- runContext,
+ selectRunContext,
driverContext,
documentContext,
}));
@@ -294,7 +295,7 @@ export function normalizeSnykSarif(
({
result: value,
rulesById,
- runContext,
+ selectRunContext,
driverContext,
documentContext,
}) => {
@@ -393,13 +394,7 @@ export function normalizeSnykSarif(
locations: [],
source_data: structuredClone({
document: documentContext,
- run: sarifRunContextForResult(
- runContext,
- result,
- rule,
- driverContext,
- documentContext,
- ),
+ run: selectRunContext(result, rule, driverContext, documentContext),
driver: driverContext,
rule,
result,
diff --git a/sdk/typescript/tests-ts/scanner-import.test.ts b/sdk/typescript/tests-ts/scanner-import.test.ts
index 5fc83c379..56650d17b 100644
--- a/sdk/typescript/tests-ts/scanner-import.test.ts
+++ b/sdk/typescript/tests-ts/scanner-import.test.ts
@@ -7,7 +7,7 @@ import {
readScannerJson,
readScannerText,
relativePath,
- sarifRunContextForResult,
+ sarifRunContextSelector,
} from "../src/scanner-import.js";
import { createTemporaryDirectories } from "./support/temporary-directories.js";
@@ -123,7 +123,8 @@ test("SARIF findings omit unrelated inventory without mutating shared run contex
{ physicalLocation: { artifactLocation: { uri: "src/main.ts" } } },
],
};
- expect(sarifRunContextForResult(context, direct)).toEqual({
+ const selectContext = sarifRunContextSelector(context);
+ expect(selectContext(direct)).toEqual({
versionControlProvenance: provenance,
});
expect(context.artifacts).toBe(artifacts);
@@ -138,9 +139,7 @@ test("SARIF findings omit unrelated inventory without mutating shared run contex
],
};
const before = structuredClone(indexed);
- expect(sarifRunContextForResult(context, indexed)["artifacts"]).toBe(
- artifacts,
- );
+ expect(selectContext(indexed)["artifacts"]).toBe(artifacts);
expect(indexed).toEqual(before);
});
@@ -184,7 +183,7 @@ test.each([
},
];
const context = { artifacts };
- expect(sarifRunContextForResult(context, selected)["artifacts"]).toBe(
+ expect(sarifRunContextSelector(context)(selected)["artifacts"]).toBe(
artifacts,
);
},
@@ -215,7 +214,7 @@ test.each([
const result = {
locations: [{ physicalLocation: { artifactLocation: location } }],
};
- expect(sarifRunContextForResult(context, result)["artifacts"]).toBe(
+ expect(sarifRunContextSelector(context)(result)["artifacts"]).toBe(
artifacts,
);
},
@@ -229,5 +228,5 @@ test("unrelated SARIF property-bag indexes do not require artifact inventory", (
{ physicalLocation: { artifactLocation: { uri: "src/main.ts" } } },
],
};
- expect(sarifRunContextForResult(context, result)).toEqual({});
+ expect(sarifRunContextSelector(context)(result)).toEqual({});
});
diff --git a/sdk/typescript/tests-ts/semgrep-findings.test.ts b/sdk/typescript/tests-ts/semgrep-findings.test.ts
index 8e970550a..ffef6d1ef 100644
--- a/sdk/typescript/tests-ts/semgrep-findings.test.ts
+++ b/sdk/typescript/tests-ts/semgrep-findings.test.ts
@@ -187,6 +187,32 @@ test("a small SARIF finding does not copy an unrelated run artifact inventory",
expect(input).toEqual(original);
});
+test("SARIF retains the current run inventory only for results that reference it", () => {
+ const input = semgrepSarif();
+ const run = input.runs[0]!;
+ const first = run.results[0]!;
+ const second = structuredClone(first);
+ second.fingerprints["matchBasedId/v1"] = "another-fingerprint";
+ second.locations[0]!.physicalLocation.artifactLocation.uri = "src/other.ts";
+ const artifact = {
+ location: { uri: "src/other.ts", uriBaseId: "%SRCROOT%" },
+ };
+ const report = {
+ ...input,
+ runs: [{ ...run, results: [first, second], artifacts: [artifact] }],
+ };
+ function contexts() {
+ const { findings, excluded } = normalizeSemgrepSarif(report);
+ expect(excluded).toEqual([]);
+ return findings.map(({ evidence }) => evidence.source_data?.["runContext"]);
+ }
+ const context = { invocations: run.invocations };
+ expect(contexts()).toEqual([context, { ...context, artifacts: [artifact] }]);
+ artifact.location.uri =
+ first.locations[0]!.physicalLocation.artifactLocation.uri;
+ expect(contexts()).toEqual([{ ...context, artifacts: [artifact] }, context]);
+});
+
test.each(["primary URI", "trace index", "tool index", "invocation index"])(
"SARIF preserves artifact inventory needed by the %s",
(reference) => {