From 7e3fe978a6424e6cbcd704eaeff49512882e49bf Mon Sep 17 00:00:00 2001 From: Vinsy Moyya <186405436+vinsymoyya@users.noreply.github.com> Date: Sat, 14 Nov 2026 16:44:59 +0530 Subject: [PATCH] fix: retry interrupted reads and validate all Action pins --- scripts/validate-foundation.sh | 19 ++++++++++++++-- src/github-api.mjs | 3 ++- test/action-pins.test.mjs | 41 ++++++++++++++++++++++++++++++++++ test/github-api.test.mjs | 32 ++++++++++++++++++++++++++ 4 files changed, 92 insertions(+), 3 deletions(-) create mode 100644 test/action-pins.test.mjs diff --git a/scripts/validate-foundation.sh b/scripts/validate-foundation.sh index 99f7952..e998d7b 100644 --- a/scripts/validate-foundation.sh +++ b/scripts/validate-foundation.sh @@ -33,8 +33,23 @@ fi # major tag the README recommends to every caller. The exemption accepts the # forms YAML allows around that reference — either quote style, and a trailing # comment — so a rewrapped line does not read as an unpinned dependency. -if grep -rnE 'uses:[[:space:]]+[^./][^@[:space:]]+@(main|master|latest|v[0-9]+([.][0-9]+)*)' .github/workflows action.yml \ - | grep -vE "uses:[[:space:]]+['\"]?tesslio/code-review-action@v[0-9]+['\"]?([[:space:]]+#.*)?[[:space:]]*$"; then +unpinned=0 +while IFS= read -r line; do + reference="${line#*uses:}" + reference="${reference#"${reference%%[![:space:]]*}"}" + reference="${reference%%[[:space:]]*}" + reference="${reference#[\"\']}" + reference="${reference%[\"\']}" + if [[ "$reference" == ./* ]] || + [[ "$reference" =~ ^tesslio/code-review-action@v[0-9]+$ ]] || + [[ "$reference" =~ ^[^@[:space:]]+@[[:xdigit:]]{40}$ ]] || + [[ "$reference" =~ ^docker://[^@[:space:]]+@sha256:[[:xdigit:]]{64}$ ]]; then + continue + fi + echo "unpinned Action reference: $reference" >&2 + unpinned=1 +done < <(grep -rhE '^[[:space:]]*(-[[:space:]]+)?uses:' .github/workflows action.yml) +if (( unpinned )); then echo "third-party Actions must be pinned by full commit SHA" >&2 exit 1 fi diff --git a/src/github-api.mjs b/src/github-api.mjs index a3f1639..e15ed44 100644 --- a/src/github-api.mjs +++ b/src/github-api.mjs @@ -135,6 +135,7 @@ export class GitHubCodeReviewApi { for (let attempt = 0; ; attempt++) { const canRetry = method !== 'POST'; let response; + let text; try { response = await this.fetchImpl(`${API_ORIGIN}${path}`, { method, @@ -147,12 +148,12 @@ export class GitHubCodeReviewApi { ...(body === undefined ? {} : { body: JSON.stringify(body) }), signal: AbortSignal.timeout(this.timeoutMs), }); + text = await response.text(); } catch (error) { if (!canRetry || attempt >= this.retryDelaysMs.length) throw error; await this.sleep(this.retryDelaysMs[attempt]); continue; } - const text = await response.text(); if (response.ok) { return { body: text.length === 0 ? undefined : JSON.parse(text), diff --git a/test/action-pins.test.mjs b/test/action-pins.test.mjs new file mode 100644 index 0000000..c1231d5 --- /dev/null +++ b/test/action-pins.test.mjs @@ -0,0 +1,41 @@ +import assert from 'node:assert/strict'; +import { spawnSync } from 'node:child_process'; +import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'; +import { tmpdir } from 'node:os'; +import { join } from 'node:path'; +import test from 'node:test'; + +const validation = readFileSync(new URL('../scripts/validate-foundation.sh', import.meta.url), 'utf8'); +const pinCheck = validation.slice(validation.indexOf('# A reference'), validation.indexOf('node --test')); + +function check(reference) { + const dir = mkdtempSync(join(tmpdir(), 'action-pins-')); + try { + mkdirSync(join(dir, '.github', 'workflows'), { recursive: true }); + writeFileSync(join(dir, 'action.yml'), `steps:\n - uses: ${reference}\n`); + // The checker reads the same paths as the foundation script. + const result = spawnSync('bash', ['-c', pinCheck], { cwd: dir, encoding: 'utf8' }); + assert.ifError(result.error); + return result; + } finally { + rmSync(dir, { recursive: true, force: true }); + } +} + +for (const reference of ['owner/action@release-candidate', 'owner/action@abc1234', 'owner/action@v2-beta', '"owner/action@feature" # mutable']) { + test(`foundation rejects a non-SHA third-party reference: ${reference}`, () => { + assert.equal(check(reference).status, 1); + }); +} + +for (const reference of [ + 'owner/action@' + 'a'.repeat(40), + '"owner/action@' + 'A'.repeat(40) + '" # immutable', + 'docker://example/image@sha256:' + 'a'.repeat(64), + "'tesslio/code-review-action@v1' # own major tag", + './local-action', +]) { + test(`foundation accepts an allowed reference: ${reference}`, () => { + assert.equal(check(reference).status, 0); + }); +} diff --git a/test/github-api.test.mjs b/test/github-api.test.mjs index b325b2b..9016f5a 100644 --- a/test/github-api.test.mjs +++ b/test/github-api.test.mjs @@ -10,6 +10,38 @@ function response(status, body = '') { }); } +test('retries a GET when reading its response body fails', async () => { + let calls = 0; + const delays = []; + const api = new GitHubCodeReviewApi({ + token: 'token', repository: 'acme/widgets', retryDelaysMs: [5], + sleep: async (delay) => delays.push(delay), + fetchImpl: async () => { + calls++; + return calls === 1 + ? new Response(new ReadableStream({ start(controller) { controller.error(new Error('connection reset')); } })) + : response(200, '{"ok":true}'); + }, + }); + assert.deepEqual(await api.request('/test'), { ok: true }); + assert.equal(calls, 2); + assert.deepEqual(delays, [5]); +}); + +test('does not replay a POST when its response body fails', async () => { + let calls = 0; + const api = new GitHubCodeReviewApi({ + token: 'token', repository: 'acme/widgets', retryDelaysMs: [0], + sleep: async () => {}, + fetchImpl: async () => { + calls++; + return new Response(new ReadableStream({ start(controller) { controller.error(new Error('connection reset')); } })); + }, + }); + await assert.rejects(api.request('/test', { method: 'POST', body: {} }), /connection reset/); + assert.equal(calls, 1); +}); + test('uses a timeout and retries bounded transient failures', async () => { const calls = []; const delays = [];