fix(sca): recognize same-manifest Cargo workspace versions

This commit is contained in:
Michael D'Angelo
2026-10-22 22:20:42 +00:00
parent 33f2f30cca
commit 11b1671ca0
2 changed files with 82 additions and 9 deletions
+12 -1
View File
@@ -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) =>
+70 -8
View File
@@ -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(