mirror of
https://github.com/openai/codex-security.git
synced 2026-11-26 18:48:07 +08:00
fix(sca): recognize same-manifest Cargo workspace versions
This commit is contained in:
@@ -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) =>
|
||||
|
||||
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user