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 evidence projection for Semgrep and Endor (#2233)
Michael D'Angelo09138bcb1b3c
diff --git a/sdk/typescript/src/endor-findings.ts b/sdk/typescript/src/endor-findings.tsindex 97a288926..c65ed60e6 100644--- a/sdk/typescript/src/endor-findings.ts+++ b/sdk/typescript/src/endor-findings.ts@@ -10,7 +10,7 @@ import {record,relativePath,requiredText,- sarifResultContext,+ sarifRunResults,strings,text,validateSarifRuleReference,@@ -198,15 +198,11 @@ export function normalizeEndorSarif(hasSarifExternalPropertyReferences(run["externalPropertyFileReferences"]))throw new Error("Use a self-contained Endor SARIF report.");- const context = sarifResultContext(document, run, tool, driver, "Endor");- return array(run["results"] ?? [], "Endor results").map((result) => ({- result,- ...context,- }));+ return sarifRunResults(document, run, tool, driver, "Endor");});return normalizeRecords(entries,- ({ result: value, rulesById, selectRunContext, ...context }) => {+ ({ result: value, context }) => {const result = record(value, "Endor result");const properties = record(result["properties"],@@ -214,7 +210,7 @@ export function normalizeEndorSarif();requiredText(properties["project-uuid"], "Endor project-uuid");const ruleId = requiredText(result["ruleId"], "Endor ruleId");- const selectedRule = rulesById.get(ruleId);+ const selectedRule = context.rulesById.get(ruleId);if (!selectedRule)throw new Error("Endor result must reference exactly one rule in its run.",@@ -291,18 +287,7 @@ export function normalizeEndorSarif(locations.map((location) => [JSON.stringify(location), location]),).values(),],- source_data: structuredClone({- ...context,- runContext: selectRunContext(- result,- rule,- context.documentContext,- context.driverContext,- context.toolContext,- ),- rule,- result,- }),+ source_data: context.sourceData(result, rule),},target,);diff --git a/sdk/typescript/src/scanner-import.ts b/sdk/typescript/src/scanner-import.tsindex 3e6dc7d3e..2dc6bb71e 100644--- a/sdk/typescript/src/scanner-import.ts+++ b/sdk/typescript/src/scanner-import.ts@@ -226,8 +226,8 @@ export function indexSarifRules(return byId;}-/** Keep the report and producer context shared by individual SARIF results. */-export function sarifResultContext(+/** Pair each result with its shared run context and original evidence. */+export function sarifRunResults(document: Record<string, unknown>,run: Record<string, unknown>,tool: Record<string, unknown>,@@ -242,14 +242,33 @@ export function sarifResultContext(const { results: _results, tool: _tool, ...runContext } = run;const { rules: _rules, ...driverContext } = driver;const { driver: _driver, ...toolContext } = tool;- return {+ const selectRunContext = sarifRunContextSelector(runContext);+ // JS callers may supply lazy metadata. Preserve each adapter's original traversal order.+ const metadataInReadOrder =+ producer === "Semgrep"+ ? [driverContext, toolContext, documentContext]+ : [documentContext, driverContext, toolContext];+ const context = {rulesById,- selectRunContext: sarifRunContextSelector(runContext),- documentContext,runContext,- driverContext,- toolContext,+ sourceData(+ result: Record<string, unknown>,+ rule?: Record<string, unknown>,+ ) {+ return structuredClone({+ documentContext,+ runContext: selectRunContext(result, rule, ...metadataInReadOrder),+ driverContext,+ toolContext,+ ...(rule ? { rule } : {}),+ result,+ });+ },};+ return array(run["results"] ?? [], `${producer} results`).map((result) => ({+ result,+ context,+ }));}/** The top-level index and nested descriptor must agree with the selected driver rule. */diff --git a/sdk/typescript/src/semgrep-findings.ts b/sdk/typescript/src/semgrep-findings.tsindex c53d7501e..073b4d5fa 100644--- a/sdk/typescript/src/semgrep-findings.ts+++ b/sdk/typescript/src/semgrep-findings.ts@@ -21,7 +21,7 @@ import {record,relativePath,requiredText,- sarifResultContext,+ sarifRunResults,stableId,text,validateSarifRuleReference,@@ -228,15 +228,11 @@ export function normalizeSemgrepSarif()throw new Error("Semgrep SARIF contains scan error notifications.");}- const context = sarifResultContext(document, run, tool, driver, "Semgrep");- return array(run["results"] ?? [], "Semgrep results").map((result) => ({- result,- ...context,- }));+ return sarifRunResults(document, run, tool, driver, "Semgrep");});return normalizeRecords(entries,- ({ result: value, rulesById, selectRunContext, ...context }) => {+ ({ result: value, context }) => {const result = record(value, "Semgrep result");const properties = record(result["properties"] ?? {},@@ -250,8 +246,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) ?? undefined;- if (rulesById.size > 0 && !selectedRule)+ const selectedRule = context.rulesById.get(ruleId) ?? undefined;+ if (context.rulesById.size > 0 && !selectedRule)throw new Error("Semgrep result must reference exactly one rule in its run.",);@@ -319,23 +315,12 @@ export function normalizeSemgrepSarif(? "suppressed": null,},- source_data: structuredClone({- ...context,- runContext: selectRunContext(- result,- rule,- context.driverContext,- context.toolContext,- context.documentContext,- ),- ...(rule ? { rule } : {}),- result,- }),+ source_data: context.sourceData(result, rule),},target,);},- ({ result, runContext }) => sarifIdentity(result, runContext),+ ({ result, context }) => sarifIdentity(result, context.runContext),target,);}diff --git a/sdk/typescript/tests-ts/semgrep-findings.test.ts b/sdk/typescript/tests-ts/semgrep-findings.test.tsindex ffef6d1ef..f4f3af57a 100644--- a/sdk/typescript/tests-ts/semgrep-findings.test.ts+++ b/sdk/typescript/tests-ts/semgrep-findings.test.ts@@ -2,7 +2,7 @@ import { expect, test } from "bun:test";import {normalizeSemgrepFindings as normalizeJson,normalizeSemgrepSarif as normalizeSarif,-} from "../src/semgrep-findings.js";+} from "../src/index.js";import {semgrepJson,semgrepSarif,@@ -213,6 +213,42 @@ test("SARIF retains the current run inventory only for results that reference itexpect(contexts()).toEqual([{ ...context, artifacts: [artifact] }, context]);});+test("lazy SDK metadata retains the artifact named by the Semgrep document", () => {+ const input = semgrepSarif();+ const run = input.runs[0]!;+ const artifacts = [+ { location: { uri: "src/generated.ts", uriBaseId: "%SRCROOT%" } },+ ];+ Object.assign(run, { artifacts });+ let driverReady = false;+ const description = {+ get text() {+ driverReady = true;+ return "Loaded";+ },+ };+ const invocation = {+ executionSuccessful: true,+ get workingDirectory() {+ return driverReady ? { index: 0 } : {};+ },+ };+ Object.assign(run.tool.driver, { shortDescription: description });+ Object.assign(input, {+ inlineExternalProperties: [{ invocations: [invocation] }],+ });+ const output = normalizeSemgrepSarif(input);+ expect(output.excluded).toEqual([]);+ expect(output.findings[0]!.evidence.source_data).toMatchObject({+ documentContext: {+ inlineExternalProperties: [+ { invocations: [{ workingDirectory: { index: 0 } }] },+ ],+ },+ runContext: { artifacts },+ });+});+test.each(["primary URI", "trace index", "tool index", "invocation index"])("SARIF preserves artifact inventory needed by the %s",(reference) => {