Skip to content

Commit 04839ec

Browse files
committed
fix(repo): 添加 picomatch 开发依赖审计豁免
1 parent 154a90e commit 04839ec

2 files changed

Lines changed: 52 additions & 30 deletions

File tree

‎scripts/audit.test.ts‎

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,22 @@ const connectionClosed: AuditResult = {
1111
const success: AuditResult = { exitCode: 0, stdout: "No vulnerabilities found\n", stderr: "" };
1212

1313
function auditSequence(results: AuditResult[]) {
14-
let attempts = 0;
14+
const invocations: (readonly string[])[] = [];
1515
const delays: number[] = [];
1616
const stdout: string[] = [];
1717
const stderr: string[] = [];
1818
return {
1919
get attempts() {
20-
return attempts;
20+
return invocations.length;
2121
},
22+
invocations,
2223
delays,
2324
stdout,
2425
stderr,
2526
dependencies: {
26-
execute: () => {
27-
const result = results[attempts++];
27+
execute: (args: readonly string[]) => {
28+
const result = results[invocations.length];
29+
invocations.push(args);
2830
if (!result) throw new Error("Unexpected audit attempt");
2931
return result;
3032
},
@@ -37,19 +39,18 @@ function auditSequence(results: AuditResult[]) {
3739
};
3840
}
3941

40-
describe("dependency audit retries", () => {
41-
test("keeps successful audits unchanged without waiting", async () => {
42-
const sequence = auditSequence([success]);
42+
describe("dependency audit", () => {
43+
test("audits production before applying the single exception in the full audit", async () => {
44+
const sequence = auditSequence([success, success]);
4345
expect(await runAudit(sequence.dependencies)).toBe(0);
44-
expect(sequence.attempts).toBe(1);
46+
expect(sequence.invocations).toEqual([["--prod"], ["--ignore", "GHSA-fc5q-39xc-vjpf"]]);
4547
expect(sequence.delays).toEqual([]);
46-
expect(sequence.stdout).toEqual([success.stdout]);
4748
});
4849

49-
test("retries the CI ConnectionClosed error with bounded backoff", async () => {
50-
const sequence = auditSequence([connectionClosed, connectionClosed, success]);
50+
test("retries the CI ConnectionClosed error in the full audit with bounded backoff", async () => {
51+
const sequence = auditSequence([success, connectionClosed, connectionClosed, success]);
5152
expect(await runAudit(sequence.dependencies)).toBe(0);
52-
expect(sequence.attempts).toBe(3);
53+
expect(sequence.attempts).toBe(4);
5354
expect(sequence.delays).toEqual([2_000, 5_000]);
5455
expect(sequence.stderr.join("")).toContain(connectionClosed.stderr);
5556
expect(sequence.stderr.join("")).toContain("retrying (3/3)");
@@ -63,15 +64,24 @@ describe("dependency audit retries", () => {
6364
expect(sequence.stderr.join("")).toContain("verification remains failed");
6465
});
6566

66-
test("does not retry vulnerability findings, even after a transient error", async () => {
67-
const finding = { exitCode: 1, stdout: "1 vulnerability (1 high)\n", stderr: "" };
67+
test("blocks the excepted advisory in production, even after a transient error", async () => {
68+
const finding = { exitCode: 1, stdout: "GHSA-fc5q-39xc-vjpf\n1 vulnerability (1 high)\n", stderr: "" };
6869
const sequence = auditSequence([connectionClosed, finding]);
6970
expect(await runAudit(sequence.dependencies)).toBe(1);
70-
expect(sequence.attempts).toBe(2);
71+
expect(sequence.invocations).toEqual([["--prod"], ["--prod"]]);
7172
expect(sequence.delays).toEqual([2_000]);
7273
expect(sequence.stdout).toContain(finding.stdout);
7374
});
7475

76+
test("blocks other findings in the full audit", async () => {
77+
const finding = { exitCode: 1, stdout: "1 vulnerability (1 high)\n", stderr: "" };
78+
const sequence = auditSequence([success, finding]);
79+
expect(await runAudit(sequence.dependencies)).toBe(1);
80+
expect(sequence.attempts).toBe(2);
81+
expect(sequence.delays).toEqual([]);
82+
expect(sequence.stdout).toContain(finding.stdout);
83+
});
84+
7585
test("does not retry unknown errors, auth failures, invalid lockfiles, or interrupted processes", async () => {
7686
for (const result of [
7787
{ exitCode: 2, stdout: "", stderr: "error: invalid lockfile\n" },

‎scripts/audit.ts‎

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ export function isTransientAuditFailure(result: AuditResult): boolean {
3030
return transportError;
3131
}
3232

33-
function executeAudit(): AuditResult {
34-
const result = spawnSync(process.execPath, ["audit"], {
33+
function executeAudit(args: readonly string[]): AuditResult {
34+
const result = spawnSync(process.execPath, ["audit", ...args], {
3535
cwd: root,
3636
encoding: "utf8",
3737
stdio: ["ignore", "pipe", "pipe"],
@@ -49,7 +49,7 @@ function executeAudit(): AuditResult {
4949

5050
export async function runAudit(
5151
dependencies: {
52-
execute?: () => AuditResult;
52+
execute?: (args: readonly string[]) => AuditResult;
5353
sleep?: (delayMs: number) => Promise<void>;
5454
writeStdout?: (output: string) => void;
5555
writeStderr?: (output: string) => void;
@@ -60,20 +60,32 @@ export async function runAudit(
6060
const writeStdout = dependencies.writeStdout ?? ((output) => process.stdout.write(output));
6161
const writeStderr = dependencies.writeStderr ?? ((output) => process.stderr.write(output));
6262

63-
for (let attempt = 0; ; attempt++) {
64-
const result = execute();
65-
writeStdout(result.stdout);
66-
writeStderr(result.stderr);
67-
if (result.errorCode) writeStderr(`Audit process failed (${result.errorCode}).\n`);
68-
if (!isTransientAuditFailure(result)) return result.exitCode;
69-
if (attempt === retryDelaysMs.length) {
70-
writeStderr("Dependency audit could not complete after 3 attempts; verification remains failed.\n");
71-
return result.exitCode;
63+
async function runWithRetries(args: readonly string[]): Promise<number> {
64+
for (let attempt = 0; ; attempt++) {
65+
const result = execute(args);
66+
writeStdout(result.stdout);
67+
writeStderr(result.stderr);
68+
if (result.errorCode) writeStderr(`Audit process failed (${result.errorCode}).\n`);
69+
if (!isTransientAuditFailure(result)) return result.exitCode;
70+
if (attempt === retryDelaysMs.length) {
71+
writeStderr("Dependency audit could not complete after 3 attempts; verification remains failed.\n");
72+
return result.exitCode;
73+
}
74+
const delayMs = retryDelaysMs[attempt];
75+
writeStderr(`Audit request failed temporarily; retrying (${attempt + 2}/3) in ${delayMs / 1000}s.\n`);
76+
await sleep(delayMs);
7277
}
73-
const delayMs = retryDelaysMs[attempt];
74-
writeStderr(`Audit request failed temporarily; retrying (${attempt + 2}/3) in ${delayMs / 1000}s.\n`);
75-
await sleep(delayMs);
7678
}
79+
80+
writeStdout("Auditing production dependencies with all advisories enabled.\n");
81+
const productionExitCode = await runWithRetries(["--prod"]);
82+
if (productionExitCode !== 0) return productionExitCode;
83+
84+
// The locked picomatch versions are only reachable from development tools.
85+
// Remove this exception once bun.lock no longer contains an affected version.
86+
const picomatchAdvisory = "GHSA-fc5q-39xc-vjpf";
87+
writeStdout(`Auditing all dependencies with temporary exception ${picomatchAdvisory}.\n`);
88+
return runWithRetries(["--ignore", picomatchAdvisory]);
7789
}
7890

7991
if (import.meta.main) process.exit(await runAudit());

0 commit comments

Comments
 (0)