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

fix(imports): preserve Guardian repository identity during replay (#2308)

Michael D'Angelo41a8e15c4703
diff --git a/sdk/typescript/src/external-findings-local.ts b/sdk/typescript/src/external-findings-local.ts
index 245449b67..46400d68a 100644
--- a/sdk/typescript/src/external-findings-local.ts
+++ b/sdk/typescript/src/external-findings-local.ts
@@ -806,17 +806,6 @@ export async function prepareLocalImport(
dependencies,
);
const sources = new Map<string, ExternalSourceRepository>();
- if (
- options.provider === "semgrep" &&
- current.report?.["format"] === guardianSelectionFormat
- ) {
- const url = declaredScannerRepositoryUrl(
- current.report["repositoryUrl"],
- "Guardian",
- "local",
- );
- sources.set(url, { url });
- }
const references = [...readerSources, ...parsedSources];
const inspector = inspectorReportRepository(
options.provider,
@@ -921,6 +910,22 @@ export async function prepareLocalImport(
);
hostedSources.set(source.url, source);
}
+ function addGuardianSource(value: unknown) {
+ if (options.provider !== "semgrep")
+ throw new CodexSecurityError(
+ "Use the semgrep provider for Guardian findings.",
+ );
+ const url = declaredScannerRepositoryUrl(value, "Guardian", "local");
+ const source =
+ hostedSources.get(url) ??
+ semgrepPlatformRepositoryReference(url, readerSources);
+ hostedSources.set(url, source);
+ return source;
+ }
+ const guardianReport =
+ current.report?.["format"] === guardianSelectionFormat
+ ? addGuardianSource(current.report["repositoryUrl"])
+ : undefined;
if (semgrepPlatformRepositoryUrl)
addSemgrepSource(semgrepPlatformRepositoryUrl);
const known = new Set<string>(parsedSources.map(({ url }) => url));
@@ -954,23 +959,38 @@ export async function prepareLocalImport(
addSemgrepSource(semgrep.url);
if (semgrep.sourceData) finding.evidence.source_data = semgrep.sourceData;
if (semgrep.migration) Object.assign(finding.evidence, semgrep.migration);
- } else hasOrdinaryRows = true;
+ }
const { evidence } = finding;
- if (
- options.provider === "semgrep" &&
- evidence.source_data?.["format"] === guardianSelectionFormat
- ) {
- const url = declaredScannerRepositoryUrl(
- evidence.source_data["sourceRepositoryUrl"],
- "Guardian",
- "local",
+ const guardian =
+ evidence.source_data?.["format"] === guardianSelectionFormat;
+ const url = evidence.details?.repository?.url;
+ if (guardian) {
+ const source = addGuardianSource(
+ evidence.source_data!["sourceRepositoryUrl"],
);
- sources.set(url, { url });
+ if (
+ (guardianReport &&
+ !semgrepReferenceMatches(guardianReport, source.url)) ||
+ (url &&
+ !semgrepReferenceMatches(
+ source,
+ declaredScannerRepositoryUrl(url, "Guardian", "local"),
+ ))
+ )
+ throw new CodexSecurityError(
+ "The Guardian finding repository does not match its recorded selection.",
+ );
+ } else if (!semgrep) {
+ hasOrdinaryRows = true;
+ if (url) sources.set(url, { url });
}
- const url = evidence.details?.repository?.url;
- // A selected row's projected URL was already verified against its hosted source.
- if (!semgrep && url) sources.set(url, { url });
- if (hasPlatformEvidence && !semgrep && !retained && !native.length) {
+ if (
+ hasPlatformEvidence &&
+ !semgrep &&
+ !guardian &&
+ !retained &&
+ !native.length
+ ) {
for (const source of sarif) sources.set(source.url, source);
if (!url && !sarif.length) ordinaryRowNeedsReferences = true;
if (!url && references.length) {
diff --git a/sdk/typescript/src/semgrep-findings.ts b/sdk/typescript/src/semgrep-findings.ts
index 204fca298..202cc5d5f 100644
--- a/sdk/typescript/src/semgrep-findings.ts
+++ b/sdk/typescript/src/semgrep-findings.ts
@@ -53,17 +53,20 @@ function sourceRepository(options: Partial<SemgrepImportOptions>): string {
return sourceRepositoryUrl(options?.sourceRepositoryUrl, "Semgrep");
}
+const severityLevels = new Map([
+ ["CRITICAL", "critical"],
+ ["HIGH", "high"],
+ ["MEDIUM", "medium"],
+ ["LOW", "low"],
+ ["ERROR", "high"],
+ ["WARNING", "medium"],
+ ["INFO", "low"],
+]);
+
function severity(value: unknown): string {
- const levels: Record<string, string> = {
- CRITICAL: "critical",
- HIGH: "high",
- MEDIUM: "medium",
- LOW: "low",
- ERROR: "high",
- WARNING: "medium",
- INFO: "low",
- };
- return typeof value === "string" ? (levels[value] ?? "unknown") : "unknown";
+ return typeof value === "string"
+ ? (severityLevels.get(value) ?? "unknown")
+ : "unknown";
}
function identity(
@@ -171,7 +174,11 @@ export function normalizeSemgrepFindings(
if (document["format"] === guardianSelectionFormat) {
if (target !== "local")
throw new Error("Import Guardian selections into the local workbench.");
- return normalizeGuardianSelection(document, options.sourceRepositoryUrl);
+ return normalizeGuardianSelection(
+ document,
+ options.sourceRepositoryUrl,
+ options.sourceRepositories,
+ );
}
if (isSemgrepPlatformFormat(document["format"])) {
return normalizeSemgrepPlatformFindings(
diff --git a/sdk/typescript/src/semgrep-guardian-findings.ts b/sdk/typescript/src/semgrep-guardian-findings.ts
index 63ee75993..d1e7a9753 100644
--- a/sdk/typescript/src/semgrep-guardian-findings.ts
+++ b/sdk/typescript/src/semgrep-guardian-findings.ts
@@ -1,7 +1,5 @@
-import {
- repositoryUrlKey,
- validateExternalEvidence,
-} from "./external-import-contract.js";
+import { validateExternalEvidence } from "./external-import-contract.js";
+import type { ExternalSourceRepository } from "./external-findings.js";
import { requireExternalImports } from "./external-imports-feature.js";
import { isRecord } from "./record.js";
import {
@@ -17,6 +15,7 @@ import { declaredScannerRepositoryUrl } from "./scanner-repository-url.js";
import {
platformLocationPath,
requireSemgrepFullRef,
+ selectionRepository,
} from "./semgrep-platform-findings.js";
export const guardianSelectionFormat = "semgrep-guardian-selection/v1";
@@ -161,6 +160,7 @@ export async function collectSelectedSemgrepGuardianFindings(
export function normalizeGuardianSelection(
document: Record<string, unknown>,
declaredUrl?: string,
+ references: readonly ExternalSourceRepository[] = [],
) {
// Guardian returns a repository name. The caller supplies its verified SCM mapping.
const url = declaredScannerRepositoryUrl(
@@ -168,11 +168,12 @@ export function normalizeGuardianSelection(
"Guardian",
"local",
);
+ const repositoryBinding = selectionRepository(url, references);
if (
declaredUrl !== undefined &&
- repositoryUrlKey(
+ repositoryBinding.key(
declaredScannerRepositoryUrl(declaredUrl, "Guardian", "local"),
- ) !== repositoryUrlKey(url)
+ ) !== repositoryBinding.key(url)
)
throw new Error(
"Guardian selection does not match the declared source repository URL.",
diff --git a/sdk/typescript/tests-ts/semgrep-findings.test.ts b/sdk/typescript/tests-ts/semgrep-findings.test.ts
index fcfa5fbdc..805eea856 100644
--- a/sdk/typescript/tests-ts/semgrep-findings.test.ts
+++ b/sdk/typescript/tests-ts/semgrep-findings.test.ts
@@ -66,6 +66,8 @@ test.each([
["MEDIUM", "medium"],
["LOW", "low"],
["EXPERIMENT", "unknown"],
+ ["constructor", "unknown"],
+ ["__proto__", "unknown"],
] as const)("maps severity %s to %s", (inputLevel, expected) => {
const input = semgrepJson();
input.results[0]!.extra.severity = inputLevel!;
diff --git a/sdk/typescript/tests-ts/semgrep-guardian-findings.test.ts b/sdk/typescript/tests-ts/semgrep-guardian-findings.test.ts
index 4541e1b8d..fb585fc1e 100644
--- a/sdk/typescript/tests-ts/semgrep-guardian-findings.test.ts
+++ b/sdk/typescript/tests-ts/semgrep-guardian-findings.test.ts
@@ -1,4 +1,5 @@
import { expect, mock, test } from "bun:test";
+import { prepareNormalizedLocalImport } from "../src/external-findings-local.js";
import { prepareNormalizedExternalPublication } from "../src/external-findings-publish.js";
import { collectSelectedSemgrepGuardianFindings } from "../src/semgrep-guardian-findings.js";
import { normalizeSemgrepFindings } from "../src/semgrep-findings.js";
@@ -262,3 +263,80 @@ test("empty selections and disabled imports do not call Guardian; tool errors re
}),
).rejects.toThrow("Guardian login expired");
});
+
+test("Guardian binds the collection, saved selection and normalized replay to the same custom Git repository", async () => {
+ const url = "https://git.example.test/example/service";
+ const collected = await collectSelectedSemgrepGuardianFindings(
+ { ...selection, sourceRepositoryUrl: url },
+ {
+ callTool: async () => ({ structuredContent: { findings: [row] } }),
+ environment,
+ },
+ );
+ const other = url + ".git";
+ for (const options of [
+ { sourceRepositoryUrl: other },
+ { sourceRepositories: [{ url: other }] },
+ ]) {
+ expect(() => normalizeSemgrepFindings(collected, options, "local")).toThrow(
+ /does not match/,
+ );
+ }
+ const normalized = normalizeSemgrepFindings(collected, {}, "local");
+ await using fixture = await isolatedGitReportFixture(
+ "guardian.json",
+ { provider: "semgrep", sourceKey: "example/topic" },
+ { remote: other },
+ );
+ await fixture.writeJson(collected);
+ await expect(fixture.prepare()).rejects.toThrow(/configured remote/);
+ await expect(fixture.prepareNormalized(normalized)).rejects.toThrow(
+ /configured remote/,
+ );
+ expect(await fixture.read()).toEqual([]);
+ fixture.git("remote", "set-url", "origin", url);
+ expect((await (await fixture.prepare()).import()).counts.created).toBe(1);
+ expect(
+ (await (await fixture.prepareNormalized(normalized)).import()).counts
+ .unchanged,
+ ).toBe(1);
+
+ await expect(
+ prepareNormalizedLocalImport(
+ normalized,
+ {
+ repository: fixture.repository,
+ sourceKey: fixture.sourceKey,
+ provider: "github",
+ },
+ { environment: fixture.environment },
+ ),
+ ).rejects.toThrow(/semgrep provider/);
+
+ // Multiple configured remotes cannot justify substituting a different retained mapping.
+ fixture.git("remote", "add", "second", other);
+ const changed = structuredClone(normalized);
+ changed.findings[0]!.evidence.source_data!["sourceRepositoryUrl"] = other;
+ await expect(fixture.prepareNormalized(changed)).rejects.toThrow(
+ /recorded selection/,
+ );
+ const wrongProjection = structuredClone(normalized);
+ wrongProjection.findings[0]!.evidence.details!.repository!.url = other;
+ await expect(fixture.prepareNormalized(wrongProjection)).rejects.toThrow(
+ /recorded selection/,
+ );
+});
+
+test("Guardian accepts the clone URL of an independently identified self-managed GitLab project", () => {
+ const url = "https://git.example.test/example/service";
+ const result = normalizeSemgrepFindings(
+ { ...capture, sourceRepositoryUrl: url },
+ {
+ sourceRepositoryUrl: url + ".git",
+ sourceRepositories: [{ url, platform: "gitlab" }],
+ },
+ "local",
+ );
+ expect(result.excluded).toEqual([]);
+ expect(result.findings[0]!.evidence.details?.repository?.url).toBe(url);
+});