Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 25 additions & 15 deletions scripts/audit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,22 @@ const connectionClosed: AuditResult = {
const success: AuditResult = { exitCode: 0, stdout: "No vulnerabilities found\n", stderr: "" };

function auditSequence(results: AuditResult[]) {
let attempts = 0;
const invocations: (readonly string[])[] = [];
const delays: number[] = [];
const stdout: string[] = [];
const stderr: string[] = [];
return {
get attempts() {
return attempts;
return invocations.length;
},
invocations,
delays,
stdout,
stderr,
dependencies: {
execute: () => {
const result = results[attempts++];
execute: (args: readonly string[]) => {
const result = results[invocations.length];
invocations.push(args);
if (!result) throw new Error("Unexpected audit attempt");
return result;
},
Expand All @@ -37,19 +39,18 @@ function auditSequence(results: AuditResult[]) {
};
}

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

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

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

test("blocks other findings in the full audit", async () => {
const finding = { exitCode: 1, stdout: "1 vulnerability (1 high)\n", stderr: "" };
const sequence = auditSequence([success, finding]);
expect(await runAudit(sequence.dependencies)).toBe(1);
expect(sequence.attempts).toBe(2);
expect(sequence.delays).toEqual([]);
expect(sequence.stdout).toContain(finding.stdout);
});

test("does not retry unknown errors, auth failures, invalid lockfiles, or interrupted processes", async () => {
for (const result of [
{ exitCode: 2, stdout: "", stderr: "error: invalid lockfile\n" },
Expand Down
42 changes: 27 additions & 15 deletions scripts/audit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ export function isTransientAuditFailure(result: AuditResult): boolean {
return transportError;
}

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

export async function runAudit(
dependencies: {
execute?: () => AuditResult;
execute?: (args: readonly string[]) => AuditResult;
sleep?: (delayMs: number) => Promise<void>;
writeStdout?: (output: string) => void;
writeStderr?: (output: string) => void;
Expand All @@ -60,20 +60,32 @@ export async function runAudit(
const writeStdout = dependencies.writeStdout ?? ((output) => process.stdout.write(output));
const writeStderr = dependencies.writeStderr ?? ((output) => process.stderr.write(output));

for (let attempt = 0; ; attempt++) {
const result = execute();
writeStdout(result.stdout);
writeStderr(result.stderr);
if (result.errorCode) writeStderr(`Audit process failed (${result.errorCode}).\n`);
if (!isTransientAuditFailure(result)) return result.exitCode;
if (attempt === retryDelaysMs.length) {
writeStderr("Dependency audit could not complete after 3 attempts; verification remains failed.\n");
return result.exitCode;
async function runWithRetries(args: readonly string[]): Promise<number> {
for (let attempt = 0; ; attempt++) {
const result = execute(args);
writeStdout(result.stdout);
writeStderr(result.stderr);
if (result.errorCode) writeStderr(`Audit process failed (${result.errorCode}).\n`);
if (!isTransientAuditFailure(result)) return result.exitCode;
if (attempt === retryDelaysMs.length) {
writeStderr("Dependency audit could not complete after 3 attempts; verification remains failed.\n");
return result.exitCode;
}
const delayMs = retryDelaysMs[attempt];
writeStderr(`Audit request failed temporarily; retrying (${attempt + 2}/3) in ${delayMs / 1000}s.\n`);
await sleep(delayMs);
}
const delayMs = retryDelaysMs[attempt];
writeStderr(`Audit request failed temporarily; retrying (${attempt + 2}/3) in ${delayMs / 1000}s.\n`);
await sleep(delayMs);
}

writeStdout("Auditing production dependencies with all advisories enabled.\n");
const productionExitCode = await runWithRetries(["--prod"]);
if (productionExitCode !== 0) return productionExitCode;

// The locked picomatch versions are only reachable from development tools.
// Remove this exception once bun.lock no longer contains an affected version.
const picomatchAdvisory = "GHSA-fc5q-39xc-vjpf";
writeStdout(`Auditing all dependencies with temporary exception ${picomatchAdvisory}.\n`);
return runWithRetries(["--ignore", picomatchAdvisory]);
}

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