Файли
openidea/e2e/nugetAudit.spec.ts
T
LU XIANGRUIтаwildcatDownstairs 3fe4e12252 feat(nuget): audit packages for vulnerabilities and deprecations (#7448)
After a successful restore, packages are audited for known vulnerabilities, deprecations and newer versions; findings show as markers in project and Directory.Packages.props files and as Problems entries.

Audits distinguish resolved versions, combine diagnostics for shared central package files, compute multi-line UTF-16 spans correctly, and run only after the actual target restored.

Closes #7439

Co-authored-by: wildcatDownstairs <[email protected]>
2026-12-02 21:18:25 +08:00

166 рядки
7.6 KiB
TypeScript

/**
* The package audit in the NuGet Packages window and the Problems view (#7439) over a stubbed
* `dotnet` host (`dotnetHost.ts`): the spec checks the markers, the Problems entries and the exact
* commands it starts; `src/features/dotnet/audit.cli.test.ts` runs the real CLI against a local v3 feed.
*/
import type { Page } from "@playwright/test";
import { expect, test } from "./fixtures";
import { dotnetRuns, installDotnetHost, openNuget, PROJECT, shot } from "./dotnetHost";
const PROPS = "/dev/acme/Directory.Packages.props";
const CSPROJ = `<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<PackageReference Include="Demo.Pkg" Version="1.0.0" />
<PackageReference Include="Old.Pkg" Version="1.0.0" />
</ItemGroup>
</Project>`;
const PROPS_TEXT = `<Project>
<ItemGroup>
<PackageVersion Include="Demo.Pkg" Version="1.0.0" />
<PackageVersion Include="Old.Pkg" Version="1.0.0" />
</ItemGroup>
</Project>`;
async function stubAudit(page: Page, central: boolean) {
await installDotnetHost(page);
await page.evaluate(
({ central, csproj, propsText, project, propsPath }) => {
window.__ipc.handlers.fs_read_text = (args) => {
if (args.path === project) return central ? csproj.replace(/ Version="1.0.0"/g, "") : csproj;
if (args.path === propsPath) return propsText;
throw { kind: "notFound", message: String(args.path) };
};
const json = (value: unknown) => JSON.stringify(value);
const framework = (extra: object) => json({ projects: [{ frameworks: [{ framework: "net9.0", ...extra }] }] });
const demo = { id: "Demo.Pkg", requestedVersion: "1.0.0", resolvedVersion: "1.0.0" };
window.__dotnetOut = (args) => {
if (args[0] === "msbuild") {
if (!args.some((a) => a.includes("ManagePackageVersionsCentrally")))
return json({ Properties: { MSBuildProjectName: "App", TargetFramework: "net9.0" }, Items: {} });
return json({
Properties: { ManagePackageVersionsCentrally: central ? "true" : "" },
Items: {
PackageVersion: [
{ Identity: "Demo.Pkg", Version: "1.0.0", DefiningProjectFullPath: propsPath },
{ Identity: "Old.Pkg", Version: "1.0.0", DefiningProjectFullPath: propsPath },
],
PackageReference: [],
},
});
}
if (args[0] !== "list") return "ok\n";
if (args.includes("--vulnerable"))
return framework({
topLevelPackages: [
{
...demo,
vulnerabilities: [{ severity: "High", advisoryurl: "https://github.com/advisories/GHSA-demo" }],
},
],
transitivePackages: [
{
id: "Child.Pkg",
resolvedVersion: "2.0.0",
vulnerabilities: [{ severity: "Moderate", advisoryurl: "https://github.com/advisories/GHSA-child" }],
},
],
});
if (args.includes("--deprecated"))
return framework({
topLevelPackages: [
{
id: "Old.Pkg",
requestedVersion: "1.0.0",
resolvedVersion: "1.0.0",
deprecationReasons: ["Legacy"],
alternativePackage: { id: "New.Pkg", versionRange: ">= 0.0.0" },
},
],
});
if (args.includes("--outdated")) return framework({ topLevelPackages: [{ ...demo, latestVersion: "1.1.0" }] });
return framework({
topLevelPackages: [demo, { id: "Old.Pkg", requestedVersion: "1.0.0", resolvedVersion: "1.0.0" }],
transitivePackages: [{ id: "Child.Pkg", resolvedVersion: "2.0.0" }],
});
};
},
{ central, csproj: CSPROJ, propsText: PROPS_TEXT, project: PROJECT, propsPath: PROPS },
);
await openNuget(page);
if (central)
await page.evaluate(async () => {
const central = "/src/features/dotnet/central.ts";
const { refreshCentral } = await import(/* @vite-ignore */ central);
await refreshCentral();
});
}
const row = (page: Page, id: string) => page.getByRole("listitem").filter({ hasText: id }).last();
async function problems(page: Page) {
await page.getByRole("button", { name: "Problems", exact: true }).click();
const pane = page.locator('[data-toolwindow="problems"]');
await pane.getByRole("tab", { name: /Project Errors/ }).click();
return pane.getByRole("tree", { name: "Problems" });
}
test("Audit marks vulnerable, deprecated and outdated packages with their severity, advisory and alternative", async ({
workbench: page,
}) => {
await stubAudit(page, false);
await page.getByRole("button", { name: "Audit packages" }).click();
await expect(row(page, "Demo.Pkg").getByRole("link", { name: "Vulnerable (High)" })).toHaveAttribute(
"href",
"https://github.com/advisories/GHSA-demo",
);
await expect(row(page, "Child.Pkg").getByRole("link", { name: "Vulnerable (Moderate)" })).toBeVisible();
await expect(row(page, "Old.Pkg")).toContainText("Deprecated: Legacy, use New.Pkg");
await expect(row(page, "Demo.Pkg")).toContainText("Newer version available: 1.1.0");
await shot(page, "d3-markers", row(page, "Demo.Pkg"));
// One command per filter, vulnerabilities with transitive packages, then the outdated check.
const runs = (await dotnetRuns(page)).filter((a) => a[0] === "list" && a.some((x) => /^--(vuln|depr|outd)/.test(x)));
expect(runs.map((a) => a.filter((x) => x.startsWith("--") && x !== "--format").join(" "))).toEqual([
"--vulnerable --include-transitive",
"--deprecated",
"--outdated",
]);
});
test("Problems lists the audit at the PackageReference lines of the project", async ({ workbench: page }) => {
await stubAudit(page, false);
await page.getByRole("button", { name: "Audit packages" }).click();
await expect(row(page, "Demo.Pkg").getByRole("link", { name: "Vulnerable (High)" })).toBeVisible();
const tree = await problems(page);
await expect(tree.getByText(/Package Demo\.Pkg 1\.0\.0 has a known High severity vulnerability/)).toBeVisible();
await expect(tree).toContainText("App.csproj");
await expect(tree).toContainText("GHSA-demo:3");
if (process.env.E2E_SHOTS)
await page.screenshot({ path: `${process.env.E2E_SHOTS}/d3-problems.png`, animations: "disabled" });
});
test("under central package management Problems points at Directory.Packages.props", async ({ workbench: page }) => {
await stubAudit(page, true);
await page.getByRole("button", { name: "Audit packages" }).click();
await expect(row(page, "Demo.Pkg").getByRole("link", { name: "Vulnerable (High)" })).toBeVisible();
const tree = await problems(page);
await expect(tree).toContainText("Directory.Packages.props");
await expect(tree.getByText(/Package Demo\.Pkg 1\.0\.0 has a known High severity vulnerability/)).toBeVisible();
// Line 3 of the props file (the Demo.Pkg PackageVersion); the unpinned transitive package stays on the project.
await expect(tree).toContainText("GHSA-demo:3");
await expect(tree).toContainText("Directory.Packages.props1 error");
});
test("a failing audit shows the CLI message and no markers", async ({ workbench: page }) => {
await stubAudit(page, false);
await page.evaluate(() => {
const ok = window.__dotnetOut;
window.__dotnetOut = (args) => {
if (args.includes("--vulnerable")) throw new Error("error: Unable to load the service index for source");
return ok(args);
};
});
await page.getByRole("button", { name: "Audit packages" }).click();
await expect(page.getByRole("alert")).toContainText("Unable to load the service index");
await expect(page.getByRole("link", { name: /Vulnerable/ })).toHaveCount(0);
});