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): match equivalent Python package names in Dependabot alerts (#2453)

Michael D'Angelo613400c9e35c
diff --git a/docs/scanner-imports/github-dependabot.md b/docs/scanner-imports/github-dependabot.md
index 216a8a685..07a67ab99 100644
--- a/docs/scanner-imports/github-dependabot.md
+++ b/docs/scanner-imports/github-dependabot.md
@@ -71,7 +71,7 @@ opens the repository alert. Severity comes from the matched package vulnerabilit
which can differ from the advisory’s overall severity. The native alert retains
GitHub’s state and timestamps. The reader requires
repository-relative manifest paths and rejects conflicting alert identities,
-packages or ranges.
+packages or ranges. For `pip`, it matches [Python distribution names](https://packaging.python.org/en/latest/specifications/name-normalization/) across the three records and retains GitHub’s reported spellings.
Use the saved alert and checkout to check the installed version and trace
whether the vulnerable code is used. An alert alone supplies no affected line
diff --git a/sdk/typescript/src/github-dependabot-findings.ts b/sdk/typescript/src/github-dependabot-findings.ts
index 7a63a0e71..16689fb9f 100644
--- a/sdk/typescript/src/github-dependabot-findings.ts
+++ b/sdk/typescript/src/github-dependabot-findings.ts
@@ -205,6 +205,17 @@ const matchedVulnerability = z.object({
severity: z.enum(REPORTABLE_SEVERITIES),
});
+// Python distribution identity ignores case and treats runs of -, _ and . alike.
+function packageNameKey(ecosystem: string, name: unknown): unknown {
+ if (
+ ecosystem === "pip" &&
+ typeof name === "string" &&
+ /^[a-zA-Z0-9](?:[a-zA-Z0-9._-]*[a-zA-Z0-9])?$/u.test(name)
+ )
+ return name.toLowerCase().replace(/[-_.]+/gu, "-");
+ return name;
+}
+
function evidence(alert: Record<string, unknown>, url: string) {
const dependency = record(alert["dependency"], "Dependabot dependency");
const advisory = record(
@@ -217,12 +228,16 @@ function evidence(alert: Record<string, unknown>, url: string) {
first_patched_version: patch,
severity,
} = matchedVulnerability.parse(alert["security_vulnerability"]);
+ const nameKey = packageNameKey(ecosystem, name);
+ const matchesPackage = (pkg: Record<string, unknown>) =>
+ pkg["ecosystem"] === ecosystem &&
+ packageNameKey(ecosystem, pkg["name"]) === nameKey;
if ("package" in dependency) {
const observed = record(
dependency["package"],
"Dependabot dependency package",
);
- if (observed["name"] !== name || observed["ecosystem"] !== ecosystem)
+ if (!matchesPackage(observed))
throw new Error(
"Dependabot dependency and matched vulnerability packages disagree.",
);
@@ -250,8 +265,7 @@ function evidence(alert: Record<string, unknown>, url: string) {
(entry) =>
isRecord(entry) &&
isRecord(entry["package"]) &&
- entry["package"]["name"] === name &&
- entry["package"]["ecosystem"] === ecosystem &&
+ matchesPackage(entry["package"]) &&
entry["vulnerable_version_range"] === range,
)
)
diff --git a/sdk/typescript/tests-ts/github-dependabot-findings.test.ts b/sdk/typescript/tests-ts/github-dependabot-findings.test.ts
index 7a3488117..167ac3fa7 100644
--- a/sdk/typescript/tests-ts/github-dependabot-findings.test.ts
+++ b/sdk/typescript/tests-ts/github-dependabot-findings.test.ts
@@ -177,6 +177,43 @@ test("uses the matched vulnerability package when the dependency omits it", () =
}
});
+test("matches equivalent Python distribution names and retains the reported spellings", () => {
+ const alert = githubDependabotAlert();
+ alert.dependency.manifest_path = "requirements.txt";
+ alert.dependency.package = { ecosystem: "pip", name: "Example_Pkg" };
+ alert.security_vulnerability.package = {
+ ecosystem: "pip",
+ name: "example-pkg",
+ };
+ alert.security_advisory.vulnerabilities[0]!.package = {
+ ecosystem: "pip",
+ name: "EXAMPLE..pkg",
+ };
+ for (const target of ["local", "cloud"] as const) {
+ const result = normalizeGitHubDependabotAlerts(alert, target);
+ expect(result.excluded).toEqual([]);
+ expect(result.findings[0]!.evidence.packages![0]).toMatchObject(
+ alert.security_vulnerability.package,
+ );
+ expect(result.findings[0]!.evidence.source_data).toEqual(alert);
+ }
+
+ alert.dependency.package.name = "examplepkg";
+ expect(() => normalizeGitHubDependabotAlerts(alert)).toThrow(
+ /packages disagree/,
+ );
+ alert.dependency.package.name = "Example_Pkg";
+ for (const pkg of [
+ alert.dependency.package,
+ alert.security_vulnerability.package,
+ alert.security_advisory.vulnerabilities[0]!.package,
+ ])
+ pkg.ecosystem = "npm";
+ expect(() => normalizeGitHubDependabotAlerts(alert)).toThrow(
+ /packages disagree/,
+ );
+});
+
test.each([
"file:///etc/passwd",
"https://example.test/lock",