CodeIssuesDiscussionsWikiPull RequestsProjectsCommitsActionsReleasesContributorsPulse● GatesSecuritySettingsDeploymentsPipelineInsightsAgents✨ Explain✨ Ask AI✨ Workspace✨ Spec✨ Tests▓ Debt Map✨ NL Search🏛 Archaeology
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 | /**
* Dependency CVE Scanner — unit tests.
*
* Tests the pure parsing helpers, the OSV result mapper, and issue-body
* renderers without touching the DB or network.
*/
import { describe, it, expect } from "bun:test";
import { __internal } from "../lib/dependency-scanner";
import app from "../app";
const {
parsePackageJson,
parseRequirementsTxt,
parseCargoToml,
parseGoMod,
parseGemfile,
mapSeverity,
extractFixVersion,
osvResultsToFindings,
renderVulnIssueBody,
renderDigestBody,
WEEKLY_DIGEST_MARKER,
FILE_ECOSYSTEM,
DEPENDENCY_FILES,
} = __internal;
// ---------------------------------------------------------------------------
// parsePackageJson
// ---------------------------------------------------------------------------
describe("parsePackageJson", () => {
it("parses dependencies and devDependencies", () => {
const json = JSON.stringify({
dependencies: { lodash: "^4.17.11", express: "^4.18.0" },
devDependencies: { jest: "^29.0.0" },
});
const result = parsePackageJson(json, "npm");
expect(result.length).toBe(3);
expect(result.map((p) => p.name)).toContain("lodash");
expect(result.map((p) => p.name)).toContain("express");
expect(result.map((p) => p.name)).toContain("jest");
});
it("strips leading ^ ~ from version specs", () => {
const json = JSON.stringify({ dependencies: { react: "^18.2.0" } });
const result = parsePackageJson(json, "npm");
expect(result[0].version).toBe("18.2.0");
});
it("returns [] on invalid JSON", () => {
expect(parsePackageJson("not json", "npm")).toEqual([]);
});
it("sets correct ecosystem", () => {
const json = JSON.stringify({ dependencies: { foo: "1.0.0" } });
const result = parsePackageJson(json, "npm");
expect(result[0].ecosystem).toBe("npm");
});
it("handles empty dependency sections", () => {
const json = JSON.stringify({ name: "my-app" });
expect(parsePackageJson(json, "npm")).toEqual([]);
});
});
// ---------------------------------------------------------------------------
// parseRequirementsTxt
// ---------------------------------------------------------------------------
describe("parseRequirementsTxt", () => {
it("parses pinned versions", () => {
const content = "requests==2.28.0\nDjango>=4.1\nflask\n";
const result = parseRequirementsTxt(content, "PyPI");
expect(result.map((p) => p.name)).toContain("requests");
expect(result.find((p) => p.name === "requests")?.version).toBe("2.28.0");
expect(result.map((p) => p.name)).toContain("Django");
expect(result.map((p) => p.name)).toContain("flask");
});
it("skips comments and -r flags", () => {
const content = "# comment\n-r base.txt\nnumpy==1.24.0\n";
const result = parseRequirementsTxt(content, "PyPI");
expect(result.length).toBe(1);
expect(result[0].name).toBe("numpy");
});
it("returns [] for empty content", () => {
expect(parseRequirementsTxt("", "PyPI")).toEqual([]);
});
});
// ---------------------------------------------------------------------------
// parseCargoToml
// ---------------------------------------------------------------------------
describe("parseCargoToml", () => {
it("parses simple string versions", () => {
const content = `[dependencies]\nserde = "1.0.163"\ntokio = "1.28.0"\n`;
const result = parseCargoToml(content, "crates.io");
expect(result.map((p) => p.name)).toContain("serde");
expect(result.find((p) => p.name === "serde")?.version).toBe("1.0.163");
expect(result.find((p) => p.name === "tokio")?.version).toBe("1.28.0");
});
it("parses table-style versions", () => {
const content = `[dependencies]\nactix-web = { version = "4.3.0", features = ["macros"] }\n`;
const result = parseCargoToml(content, "crates.io");
expect(result.find((p) => p.name === "actix-web")?.version).toBe("4.3.0");
});
it("skips [package] and other sections", () => {
const content = `[package]\nname = "my-crate"\n\n[dependencies]\nhyper = "0.14.27"\n`;
const result = parseCargoToml(content, "crates.io");
expect(result.length).toBe(1);
expect(result[0].name).toBe("hyper");
});
it("returns [] for empty content", () => {
expect(parseCargoToml("", "crates.io")).toEqual([]);
});
});
// ---------------------------------------------------------------------------
// parseGoMod
// ---------------------------------------------------------------------------
describe("parseGoMod", () => {
it("parses require lines", () => {
const content = `module example.com/myapp\n\nrequire (\n\tgithub.com/gin-gonic/gin v1.9.0\n\tgolang.org/x/net v0.10.0\n)\n`;
const result = parseGoMod(content, "Go");
expect(result.map((p) => p.name)).toContain("github.com/gin-gonic/gin");
expect(result.find((p) => p.name === "github.com/gin-gonic/gin")?.version).toBe("1.9.0");
});
it("strips leading v from version", () => {
const content = `require github.com/foo/bar v2.1.0\n`;
const result = parseGoMod(content, "Go");
expect(result[0].version).toBe("2.1.0");
});
it("returns [] for content with no require lines", () => {
expect(parseGoMod("module foo\n\ngo 1.21\n", "Go")).toEqual([]);
});
});
// ---------------------------------------------------------------------------
// parseGemfile
// ---------------------------------------------------------------------------
describe("parseGemfile", () => {
it("parses gem declarations", () => {
const content = `source 'https://rubygems.org'\ngem 'rails', '~> 7.0'\ngem 'pg'\n`;
const result = parseGemfile(content, "RubyGems");
expect(result.map((p) => p.name)).toContain("rails");
expect(result.map((p) => p.name)).toContain("pg");
});
it("returns [] for non-gem lines", () => {
expect(parseGemfile("source 'https://rubygems.org'\n", "RubyGems")).toEqual([]);
});
});
// ---------------------------------------------------------------------------
// mapSeverity
// ---------------------------------------------------------------------------
describe("mapSeverity", () => {
it("returns 'critical' for CVSS score >= 9.0", () => {
expect(
mapSeverity({ id: "x", severity: [{ type: "CVSS_V3", score: "9.5" }] })
).toBe("critical");
});
it("returns 'high' for CVSS score >= 7.0", () => {
expect(
mapSeverity({ id: "x", severity: [{ type: "CVSS_V3", score: "8.1" }] })
).toBe("high");
});
it("returns 'medium' for CVSS score >= 4.0", () => {
expect(
mapSeverity({ id: "x", severity: [{ type: "CVSS_V3", score: "5.3" }] })
).toBe("medium");
});
it("returns 'low' for CVSS score < 4.0", () => {
expect(
mapSeverity({ id: "x", severity: [{ type: "CVSS_V3", score: "2.1" }] })
).toBe("low");
});
it("handles string CRITICAL severity", () => {
expect(
mapSeverity({ id: "x", severity: [{ type: "CVSS_V3", score: "CRITICAL" }] })
).toBe("critical");
});
it("handles string HIGH severity", () => {
expect(
mapSeverity({ id: "x", severity: [{ type: "CVSS_V3", score: "HIGH" }] })
).toBe("high");
});
it("returns 'medium' when no severity data", () => {
expect(mapSeverity({ id: "x" })).toBe("medium");
expect(mapSeverity({ id: "x", severity: [] })).toBe("medium");
});
});
// ---------------------------------------------------------------------------
// extractFixVersion
// ---------------------------------------------------------------------------
describe("extractFixVersion", () => {
it("extracts fix version from affected ranges", () => {
const vuln = {
id: "CVE-2021-44228",
affected: [
{
ranges: [
{
type: "SEMVER",
events: [{ introduced: "2.0.0" }, { fixed: "2.15.0" }],
},
],
},
],
};
expect(extractFixVersion(vuln)).toBe("2.15.0");
});
it("returns undefined when no fix is available", () => {
const vuln = {
id: "CVE-XXXX",
affected: [{ ranges: [{ type: "SEMVER", events: [{ introduced: "0" }] }] }],
};
expect(extractFixVersion(vuln)).toBeUndefined();
});
it("returns undefined for vulns with no affected data", () => {
expect(extractFixVersion({ id: "x" })).toBeUndefined();
});
});
// ---------------------------------------------------------------------------
// osvResultsToFindings
// ---------------------------------------------------------------------------
describe("osvResultsToFindings", () => {
it("converts OSV results to VulnFindings", () => {
const packages = [{ name: "lodash", version: "4.17.11", ecosystem: "npm" }];
const results = [
{
vulns: [
{
id: "GHSA-p6mc-m468-83gw",
summary: "Prototype pollution in lodash",
severity: [{ type: "CVSS_V3", score: "9.8" }],
affected: [
{
ranges: [
{ type: "SEMVER", events: [{ introduced: "0" }, { fixed: "4.17.12" }] },
],
},
],
},
],
},
];
const findings = osvResultsToFindings(packages, results);
expect(findings).toHaveLength(1);
expect(findings[0].packageName).toBe("lodash");
expect(findings[0].installedVersion).toBe("4.17.11");
expect(findings[0].severity).toBe("critical");
expect(findings[0].cveId).toBe("GHSA-p6mc-m468-83gw");
expect(findings[0].fixVersion).toBe("4.17.12");
});
it("returns [] when no vulns in results", () => {
const packages = [{ name: "safe-pkg", version: "1.0.0", ecosystem: "npm" }];
const results = [{ vulns: [] }];
expect(osvResultsToFindings(packages, results)).toEqual([]);
});
it("handles mismatched results length gracefully", () => {
const packages = [{ name: "pkg-a", version: "1.0.0", ecosystem: "npm" }];
const findings = osvResultsToFindings(packages, []);
expect(findings).toEqual([]);
});
});
// ---------------------------------------------------------------------------
// renderVulnIssueBody
// ---------------------------------------------------------------------------
describe("renderVulnIssueBody", () => {
const finding = {
packageName: "lodash",
installedVersion: "4.17.11",
severity: "critical" as const,
cveId: "GHSA-p6mc-m468-83gw",
description: "Prototype pollution.",
fixVersion: "4.17.12",
};
it("includes package name and version", () => {
const body = renderVulnIssueBody(finding);
expect(body).toContain("lodash");
expect(body).toContain("4.17.11");
});
it("includes CVE ID with OSV link", () => {
const body = renderVulnIssueBody(finding);
expect(body).toContain("GHSA-p6mc-m468-83gw");
expect(body).toContain("https://osv.dev/vulnerability/GHSA-p6mc-m468-83gw");
});
it("includes fix version in remediation", () => {
const body = renderVulnIssueBody(finding);
expect(body).toContain("4.17.12");
});
it("mentions 'No fix' when fixVersion is absent", () => {
const body = renderVulnIssueBody({ ...finding, fixVersion: undefined });
expect(body).toContain("No fix version");
});
it("includes CRITICAL urgency text for critical severity", () => {
const body = renderVulnIssueBody(finding);
expect(body).toContain("CRITICAL");
});
});
// ---------------------------------------------------------------------------
// renderDigestBody
// ---------------------------------------------------------------------------
describe("renderDigestBody", () => {
const findings = [
{
packageName: "minimist",
installedVersion: "1.2.0",
severity: "medium" as const,
cveId: "CVE-2020-7598",
description: "Prototype pollution.",
fixVersion: "1.2.2",
},
];
it("includes the weekly digest marker", () => {
expect(renderDigestBody(findings)).toContain(WEEKLY_DIGEST_MARKER);
});
it("includes package name and CVE in the table", () => {
const body = renderDigestBody(findings);
expect(body).toContain("minimist");
expect(body).toContain("CVE-2020-7598");
});
it("returns valid markdown table", () => {
const body = renderDigestBody(findings);
expect(body).toContain("| Package |");
expect(body).toContain("|---|");
});
});
// ---------------------------------------------------------------------------
// Constants
// ---------------------------------------------------------------------------
describe("constants", () => {
it("DEPENDENCY_FILES contains the five expected files", () => {
expect(DEPENDENCY_FILES).toContain("package.json");
expect(DEPENDENCY_FILES).toContain("requirements.txt");
expect(DEPENDENCY_FILES).toContain("Cargo.toml");
expect(DEPENDENCY_FILES).toContain("go.mod");
expect(DEPENDENCY_FILES).toContain("Gemfile");
});
it("FILE_ECOSYSTEM maps each file to an ecosystem", () => {
expect(FILE_ECOSYSTEM["package.json"]).toBe("npm");
expect(FILE_ECOSYSTEM["requirements.txt"]).toBe("PyPI");
expect(FILE_ECOSYSTEM["Cargo.toml"]).toBe("crates.io");
expect(FILE_ECOSYSTEM["go.mod"]).toBe("Go");
expect(FILE_ECOSYSTEM["Gemfile"]).toBe("RubyGems");
});
});
// ---------------------------------------------------------------------------
// Route auth smoke tests
// ---------------------------------------------------------------------------
describe("GET /:owner/:repo/security/vulnerabilities — route auth", () => {
it("returns 404 for non-existent owner", async () => {
const res = await app.request(
"/nonexistent-owner-99999/my-repo/security/vulnerabilities"
);
expect(res.status).toBe(404);
});
});
|