Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(ci): a registry outage in bun audit is audit_registry_unavailable, never advisories
garrytanca4725e520ff
diff --git a/scripts/ci-dependency-audit.sh b/scripts/ci-dependency-audit.shindex 23ffe02b64..2972cef4a4 100755--- a/scripts/ci-dependency-audit.sh+++ b/scripts/ci-dependency-audit.sh@@ -3,18 +3,69 @@# lockfiles; fail only when the run is blocking (test.yml dependency-audit# scope step; env BLOCKING=true|false, REASON). Otherwise a found advisory is# a ::warning:: naming why this run does not block (GBRA-47 B11).+#+# A run that never got an answer is not an advisory: a registry or network+# error (bun's "error: POST <url> - 503", a refused connection, an attempt past+# its time limit) is retried with backoff inside AUDIT_DEADLINE_S, then reported+# as audit_registry_unavailable. Any other failure without bun's advisory report+# is audit_incomplete. Both block a blocking run, because nothing was checked.set -uo pipefail-found=0-bun audit || found=1-(cd admin && bun audit) || found=1-if [ "$found" -eq 0 ]; then+deadline=$(( SECONDS + ${AUDIT_DEADLINE_S:-420} ))+read -r -a delays <<< "${AUDIT_RETRY_DELAYS:-15 45}"+found=0 unavailable="" incomplete=""++audit() {+ local dir=$1 attempt=0 out rc remaining+ while :; do+ remaining=$(( deadline - SECONDS ))+ if [ "$remaining" -lt 10 ]; then unavailable+="${unavailable:+; }$dir: not attempted, AUDIT_DEADLINE_S spent on earlier attempts"; return 0; fi+ out=$(cd "$dir" && timeout "$(( remaining < ${AUDIT_ATTEMPT_TIMEOUT_S:-120} ? remaining : ${AUDIT_ATTEMPT_TIMEOUT_S:-120} ))" bun audit 2>&1); rc=$?+ printf '%s\n' "$out"+ [ "$rc" -eq 0 ] && return 0+ if grep -Eq '^[0-9]+ vulnerabilit(y|ies) \(' <<< "$out"; then found=1; return 0; fi+ local why+ if [ "$rc" -eq 124 ]; then why="no answer within the attempt's time limit"+ else why=$(grep -E '^error: ' <<< "$out" | tail -1)+ if ! grep -Eiq '^error: (GET|POST) https?://[^ ]+ - [0-9]{3}$|connection|ECONN|timed? ?out|network|DNS|resolve host' <<< "$why"; then+ incomplete+="${incomplete:+; }$dir: ${why:-bun audit exited $rc without an advisory report}"; return 0+ fi+ fi+ local delay=${delays[$attempt]:-}+ if [ -z "$delay" ] || [ $(( deadline - SECONDS - delay )) -lt 10 ]; then+ unavailable+="${unavailable:+; }$dir: $why (attempt $(( attempt + 1 )))"; return 0+ fi+ echo "bun audit in $dir got no answer ($why); retrying in ${delay}s"+ sleep "$delay"; attempt=$(( attempt + 1 ))+ done+}++audit .+audit admin++blocking=true; [ "${BLOCKING:-true}" = false ] && blocking=false+problems=0+if [ -n "$unavailable" ]; then+ problems=1+ msg="audit_registry_unavailable: bun audit got no answer from the registry's advisory endpoint, so no advisory was checked ($unavailable). Why: a registry or network outage is not an advisory and is never reported as one. Fix: rerun the dependency-audit job once the registry answers (https://status.npmjs.org)."+ if $blocking; then echo "::error::$msg (blocking: ${REASON:-unknown scope})"; else echo "::warning::$msg (advisory-only: ${REASON:-})"; fi+fi+if [ -n "$incomplete" ]; then+ problems=1+ msg="audit_incomplete: bun audit failed without an advisory report ($incomplete). Why: a failed audit checked nothing, which is not the same as finding advisories. Fix: run bun audit locally in that directory and fix what it reports (lockfile or install state)."+ if $blocking; then echo "::error::$msg (blocking: ${REASON:-unknown scope})"; else echo "::warning::$msg (advisory-only: ${REASON:-})"; fi+fi+if [ "$found" -eq 1 ]; then+ problems=1+ if $blocking; then+ echo "::error::bun audit found advisories (blocking: ${REASON:-unknown scope}). Why: this run changes dependencies or protects master, so a known-vulnerable package must not land. Fix: bump the package (bun update <pkg>) or add an overrides entry, then push."+ else+ echo "::warning::bun audit found advisories; advisory-only on this pull request (${REASON:-}). Why: an upstream advisory must not turn every open PR red; pushes and the nightly run block on it. Fix: none needed here; to block on it, add the dependency-audit label and push."+ fi+fi+if [ "$problems" -eq 0 ]; thenecho "bun audit: no advisories."exit 0fi-if [ "${BLOCKING:-true}" != false ]; then- echo "::error::bun audit found advisories (blocking: ${REASON:-unknown scope}). Why: this run changes dependencies or protects master, so a known-vulnerable package must not land. Fix: bump the package (bun update <pkg>) or add an overrides entry, then push."- exit 1-fi-echo "::warning::bun audit found advisories; advisory-only on this pull request (${REASON:-}). Why: an upstream advisory must not turn every open PR red; pushes and the nightly run block on it. Fix: none needed here; to block on it, add the dependency-audit label and push."+$blocking && exit 1exit 0diff --git a/test/scripts/ci-dependency-audit.test.ts b/test/scripts/ci-dependency-audit.test.tsnew file mode 100644index 0000000000..28b54f32d4--- /dev/null+++ b/test/scripts/ci-dependency-audit.test.ts@@ -0,0 +1,66 @@+/**+ * scripts/ci-dependency-audit.sh: a registry that never answered is not an advisory. Master v0.60.361.0 went red when the+ * advisories endpoint returned 503 and the script reported "bun audit found advisories". Forced probe: a local stub+ * registry (BUN_CONFIG_REGISTRY) answers 503, then advisories, then nothing found; the real `bun audit` runs against it.+ */+import { afterAll, beforeAll, expect, test } from 'bun:test';+import { join } from 'node:path';++const ROOT = join(import.meta.dir, '..', '..');+let plan: number[] = [];+let calls = 0;+let server: ReturnType<typeof Bun.serve>;+beforeAll(() => {+ server = Bun.serve({ port: 0, hostname: '127.0.0.1', async fetch(req) {+ if (!new URL(req.url).pathname.endsWith('/-/npm/v1/security/advisories/bulk')) return new Response('not found', { status: 404 });+ const status = plan[calls++] ?? plan.at(-1)!;+ if (status !== 200 && status !== 299) return new Response('Service Unavailable', { status });+ if (status === 200) return Response.json({});+ const raw = new Uint8Array(await req.arrayBuffer());+ let text: string; try { text = new TextDecoder().decode(Bun.gunzipSync(raw)); } catch { text = new TextDecoder().decode(raw); }+ const [name] = Object.keys(JSON.parse(text) as Record<string, string[]>);+ return Response.json({ [name!]: [{ id: 1, url: 'https://github.com/advisories/GHSA-0000-0000-0000', title: 'Synthetic advisory', severity: 'high', vulnerable_versions: '*', cwe: [], cvss: { score: 7.5, vectorString: null } }] });+ } });+});+afterAll(() => server.stop(true));++/** 299 stands for "answers with an advisory for the first package". */+async function audit(statuses: number[], env: Record<string, string> = {}) {+ plan = statuses; calls = 0;+ // Asynchronous: the stub registry answers on this test's event loop.+ const child = Bun.spawn(['bash', 'scripts/ci-dependency-audit.sh'], { cwd: ROOT, stdout: 'pipe', stderr: 'pipe',+ env: { ...process.env, BUN_CONFIG_REGISTRY: `http://127.0.0.1:${server.port}/`, AUDIT_RETRY_DELAYS: '0 0', REASON: 'test', ...env } });+ const [out, err, status] = await Promise.all([new Response(child.stdout).text(), new Response(child.stderr).text(), child.exited]);+ return { status, out: out + err, calls };+}++test('a registry that keeps answering 503 is audit_registry_unavailable after bounded retries, never advisories', async () => {+ const run = await audit([503]);+ expect(run.status).toBe(1);+ expect(run.out).toContain('::error::audit_registry_unavailable');+ expect(run.out).toContain('- 503 (attempt 3)');+ expect(run.out).not.toContain('found advisories');+ expect(run.calls).toBe(6);+});++test('a 503 that recovers on retry is a clean audit', async () => {+ const run = await audit([503, 200]);+ expect(run.status).toBe(0);+ expect(run.out).toContain('retrying in 0s');+ expect(run.out).toContain('bun audit: no advisories.');+ expect(run.out).not.toContain('audit_registry_unavailable');+});++test('a real advisory still blocks a blocking run as advisories', async () => {+ const run = await audit([299]);+ expect(run.status).toBe(1);+ expect(run.out).toContain('::error::bun audit found advisories');+ expect(run.out).not.toContain('audit_registry_unavailable');+});++test('an unavailable registry on an advisory-only run warns without failing', async () => {+ const run = await audit([503], { BLOCKING: 'false' });+ expect(run.status).toBe(0);+ expect(run.out).toContain('::warning::audit_registry_unavailable');+ expect(run.out).not.toContain('found advisories');+});