From 11b1671ca09375a8701e2dead618fc39dd74ee14 Mon Sep 17 00:00:00 2001 From: Michael D'Angelo Date: Thu, 22 Oct 2026 22:17:17 +0000 Subject: [PATCH] fix(sca): recognize same-manifest Cargo workspace versions --- sdk/typescript/src/sca-osv.ts | 13 ++++- sdk/typescript/tests-ts/sca-osv.test.ts | 78 ++++++++++++++++++++++--- 2 files changed, 82 insertions(+), 9 deletions(-) diff --git a/sdk/typescript/src/sca-osv.ts b/sdk/typescript/src/sca-osv.ts index 76d5b54de..15add8cd8 100644 --- a/sdk/typescript/src/sca-osv.ts +++ b/sdk/typescript/src/sca-osv.ts @@ -730,6 +730,17 @@ export async function discoverScaInputs( } const pkg = manifest?.["package"]; if (record(pkg)) { + const declaredVersion = pkg["version"]; + const workspace = manifest?.["workspace"]; + const workspacePackage = record(workspace) + ? workspace["package"] + : undefined; + const version = + record(declaredVersion) && + declaredVersion["workspace"] === true && + record(workspacePackage) + ? workspacePackage["version"] + : declaredVersion; const packages = parseToml(content.toString("utf8"))["package"]; for ( let index = inspected.references.length - 1; @@ -741,7 +752,7 @@ export async function discoverScaInputs( reference.resolution === "workspace-or-path" && reference.name === pkg["name"] && reference.version !== null && - reference.version === pkg["version"] + reference.version === version ) { const sources = Array.isArray(packages) ? packages.flatMap((candidate) => diff --git a/sdk/typescript/tests-ts/sca-osv.test.ts b/sdk/typescript/tests-ts/sca-osv.test.ts index 30c4eae44..2df6ac71d 100644 --- a/sdk/typescript/tests-ts/sca-osv.test.ts +++ b/sdk/typescript/tests-ts/sca-osv.test.ts @@ -121,17 +121,24 @@ async function scanFixture( ); } -test.each([ - null, - "registry+https://github.com/rust-lang/crates.io-index", - "registry+https://registry.example.test/index", -])( - "excludes only unambiguous first-party Cargo matches and retains raw output (source-bearing twin: %s)", - async (source) => { +test.each( + [ + null, + "registry+https://github.com/rust-lang/crates.io-index", + "registry+https://registry.example.test/index", + ].flatMap((source) => + [false, true].map((inherited) => [source, inherited] as const), + ), +)( + "excludes only unambiguous first-party Cargo matches and retains raw output (source-bearing twin: %p, inherited version: %p)", + async (source, inherited) => { const { repository, output } = await setup(); await writeFile( join(repository, "Cargo.toml"), - '[package]\nname="synthetic-lib"\nversion="1.2.0"\n', + '[package]\nname="synthetic-lib"\n' + + (inherited + ? 'version.workspace=true\n[workspace.package]\nversion="1.2.0"\n' + : 'version="1.2.0"\n[workspace.package]\nversion="9.9.9"\n'), ); await writeFile( join(repository, "Cargo.lock"), @@ -180,6 +187,61 @@ test.each([ }, ); +test.each(["missing", "external", "mismatched"] as const)( + "keeps inherited Cargo version unresolved without a matching same-manifest value: %s", + async (kind) => { + const { root, repository, output } = await setup(); + if (kind === "external") { + await mkdir(join(root, "workspace")); + await writeFile( + join(root, "workspace", "Cargo.toml"), + '[workspace.package]\nversion="1.2.0"\n', + ); + } + await writeFile( + join(repository, "Cargo.toml"), + '[package]\nname="synthetic-lib"\nversion.workspace=true\n' + + (kind === "external" + ? 'workspace="../workspace"\n' + : kind === "mismatched" + ? '[workspace.package]\nversion="1.2.1"\n' + : ""), + ); + await writeFile( + join(repository, "Cargo.lock"), + 'version=4\n[[package]]\nname="synthetic-lib"\nversion="1.2.0"\n', + ); + const vulnerability = advisory("SYNTHETIC-CARGO-1"); + vulnerability.affected[0]!.package.ecosystem = "crates.io"; + const raw = rawOutput("Cargo.lock", [vulnerability]); + raw.results[0]!.packages[0]!.package.ecosystem = "crates.io"; + const serialized = JSON.stringify(raw); + const result = await runOsvScan( + { repositoryPath: repository, outputDir: output }, + { + executable: process.execPath, + runProcess: async (_exe, argv) => ({ + stdout: argv[0] === "--version" ? "2.6.0" : serialized, + stderr: "", + exitCode: argv[0] === "--version" ? 0 : 1, + }), + }, + ); + expect(result.coverage.firstPartyPackages).toEqual([]); + expect(result.coverage.unresolvedPackages).toBe(1); + expect(result.status).toBe("partial"); + expect(result.coverage.status).toBe("partial"); + expect(result.components).toHaveLength(1); + expect(result.matches[0]!.advisoryIds).toEqual(["SYNTHETIC-CARGO-1"]); + expect(result.coverage.limitations.join("\n")).toContain( + "workspace-or-path", + ); + expect(await readFile(result.scanner.rawOutputPath, "utf8")).toBe( + serialized, + ); + }, +); + test("first-party Cargo exclusion preserves other sources, names, versions, and ecosystems", async () => { const { repository, output } = await setup(); await writeFile(