From 257e9a2213674c994acbf6f1a54d4fb33498791b Mon Sep 17 00:00:00 2001 From: "Tiago Siebler @ Siebly.io" Date: Wed, 18 Nov 2026 15:12:36 +0000 Subject: [PATCH 1/3] fix(ci): authenticate deployment revision check --- .github/workflows/publish.yml | 8 ++++++- scripts/ci-plan.test.mjs | 40 +++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index b48f7dc9..2b40ac63 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -70,6 +70,7 @@ jobs: runs-on: ubuntu-latest timeout-minutes: 15 permissions: + contents: read pages: write id-token: write environment: @@ -81,8 +82,13 @@ jobs: id: current env: EXPECTED_SHA: ${{ github.sha }} + GH_TOKEN: ${{ github.token }} run: | - current_sha="$(curl --fail --silent --show-error --retry 3 --max-time 30 https://api.github.com/repos/sieblyio/docs/commits/main | jq -er '.sha')" + current_sha="$(curl --fail --silent --show-error --retry 3 --max-time 30 \ + -H "Authorization: Bearer $GH_TOKEN" \ + -H "Accept: application/vnd.github+json" \ + -H "X-GitHub-Api-Version: 2022-11-28" \ + https://api.github.com/repos/sieblyio/docs/commits/main | jq -er '.sha')" if [ "$current_sha" = "$EXPECTED_SHA" ]; then echo 'deploy=true' >> "$GITHUB_OUTPUT" else diff --git a/scripts/ci-plan.test.mjs b/scripts/ci-plan.test.mjs index e4f63bbd..652ad761 100644 --- a/scripts/ci-plan.test.mjs +++ b/scripts/ci-plan.test.mjs @@ -14,3 +14,43 @@ test('only push and manual runs on this repository main branch can deploy', () = { ...main, eventName: 'workflow_dispatch', ref: 'refs/tags/release' }, ]) assert.equal(canDeploy(context), false); }); + +test('deployment revision guard authenticates and fails closed', async () => { + const { readFileSync, mkdtempSync, writeFileSync, rmSync } = await import('node:fs'); + const { tmpdir } = await import('node:os'); + const { join } = await import('node:path'); + const { spawnSync } = await import('node:child_process'); + const workflow = readFileSync(new URL('../.github/workflows/publish.yml', import.meta.url), 'utf8'); + const deploy = workflow.split('\n deploy:\n')[1]; + assert.match(deploy, /permissions:\n\s+contents: read/); + assert.match(deploy, /GH_TOKEN: \$\{\{ github.token \}\}/); + const script = deploy.split(' run: |\n')[1].split(' - name:')[0] + .split('\n').map(line => line.replace(/^ /, '')).join('\n'); + const dir = mkdtempSync(join(tmpdir(), 'docs-deploy-guard-')); + try { + writeFileSync(join(dir, 'curl'), `#!/bin/bash +[[ "$*" == *"Authorization: Bearer test-token"* ]] || exit 90 +if [ "$MOCK_HTTP_FAILURE" = 1 ]; then exit 22; fi +printf '{"sha":"%s"}\\n' "$MOCK_SHA" +`, { mode: 0o755 }); + for (const [sha, failure, expected] of [['current', '0', 'true'], ['newer', '0', 'false'], ['current', '1', null]]) { + const output = join(dir, 'output'); + writeFileSync(output, ''); + const result = spawnSync('bash', ['-e', '-c', script], { + encoding: 'utf8', + env: { ...process.env, PATH: `${dir}:${process.env.PATH}`, GH_TOKEN: 'test-token', + EXPECTED_SHA: 'current', MOCK_SHA: sha, MOCK_HTTP_FAILURE: failure, + GITHUB_OUTPUT: output, GITHUB_STEP_SUMMARY: join(dir, 'summary') }, + }); + if (expected === null) { + assert.notEqual(result.status, 0); + assert.equal(readFileSync(output, 'utf8'), ''); + } else { + assert.equal(result.status, 0, result.stderr); + assert.equal(readFileSync(output, 'utf8'), `deploy=${expected}\n`); + } + } + } finally { + rmSync(dir, { recursive: true, force: true }); + } +}); From 4308b44b6dbdc0f948dde725df5f0af431d90582 Mon Sep 17 00:00:00 2001 From: "Tiago Siebler @ Siebly.io" Date: Wed, 18 Nov 2026 15:22:26 +0000 Subject: [PATCH 2/3] fix(ci): read public main ref without credentials --- .github/workflows/publish.yml | 15 +++++++------- scripts/ci-plan.test.mjs | 38 ++++++++++++++++++++++++++--------- 2 files changed, 36 insertions(+), 17 deletions(-) diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 2b40ac63..f5ee3e3b 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -70,7 +70,6 @@ jobs: runs-on: ubuntu-latest timeout-minutes: 15 permissions: - contents: read pages: write id-token: write environment: @@ -82,13 +81,15 @@ jobs: id: current env: EXPECTED_SHA: ${{ github.sha }} - GH_TOKEN: ${{ github.token }} + GIT_TERMINAL_PROMPT: '0' run: | - current_sha="$(curl --fail --silent --show-error --retry 3 --max-time 30 \ - -H "Authorization: Bearer $GH_TOKEN" \ - -H "Accept: application/vnd.github+json" \ - -H "X-GitHub-Api-Version: 2022-11-28" \ - https://api.github.com/repos/sieblyio/docs/commits/main | jq -er '.sha')" + remote_ref="$(timeout 30s git -c credential.helper= -c http.extraHeader= \ + ls-remote --exit-code https://github.com/sieblyio/docs.git refs/heads/main)" + if [[ ! "$remote_ref" =~ ^([0-9a-f]{40})$'\t'refs/heads/main$ ]]; then + echo 'Invalid main revision response.' >&2 + exit 1 + fi + current_sha="${BASH_REMATCH[1]}" if [ "$current_sha" = "$EXPECTED_SHA" ]; then echo 'deploy=true' >> "$GITHUB_OUTPUT" else diff --git a/scripts/ci-plan.test.mjs b/scripts/ci-plan.test.mjs index 652ad761..3ecbb9fa 100644 --- a/scripts/ci-plan.test.mjs +++ b/scripts/ci-plan.test.mjs @@ -15,31 +15,49 @@ test('only push and manual runs on this repository main branch can deploy', () = ]) assert.equal(canDeploy(context), false); }); -test('deployment revision guard authenticates and fails closed', async () => { +test('deployment revision guard is credential-free and fails closed', async () => { const { readFileSync, mkdtempSync, writeFileSync, rmSync } = await import('node:fs'); const { tmpdir } = await import('node:os'); const { join } = await import('node:path'); const { spawnSync } = await import('node:child_process'); const workflow = readFileSync(new URL('../.github/workflows/publish.yml', import.meta.url), 'utf8'); const deploy = workflow.split('\n deploy:\n')[1]; - assert.match(deploy, /permissions:\n\s+contents: read/); - assert.match(deploy, /GH_TOKEN: \$\{\{ github.token \}\}/); + assert.doesNotMatch(deploy, /contents:|GH_TOKEN|github.token|Authorization:/); + assert.match(deploy, /GIT_TERMINAL_PROMPT: '0'/); const script = deploy.split(' run: |\n')[1].split(' - name:')[0] .split('\n').map(line => line.replace(/^ /, '')).join('\n'); const dir = mkdtempSync(join(tmpdir(), 'docs-deploy-guard-')); + const sha = 'a'.repeat(40); try { - writeFileSync(join(dir, 'curl'), `#!/bin/bash -[[ "$*" == *"Authorization: Bearer test-token"* ]] || exit 90 -if [ "$MOCK_HTTP_FAILURE" = 1 ]; then exit 22; fi -printf '{"sha":"%s"}\\n' "$MOCK_SHA" + writeFileSync(join(dir, 'git'), `#!/bin/bash +[[ "$*" == "-c credential.helper= -c http.extraHeader= ls-remote --exit-code https://github.com/sieblyio/docs.git refs/heads/main" ]] || exit 90 +[[ "$GIT_TERMINAL_PROMPT" == 0 ]] || exit 91 +if [ "$MOCK_FAILURE" = 1 ]; then exit 128; fi +printf '%s' "$MOCK_REF" `, { mode: 0o755 }); - for (const [sha, failure, expected] of [['current', '0', 'true'], ['newer', '0', 'false'], ['current', '1', null]]) { + // Exercise timeout invocation on platforms without GNU timeout as well. + writeFileSync(join(dir, 'timeout'), `#!/bin/bash +[[ "$1" == 30s ]] || exit 92 +shift +if [ "$MOCK_FAILURE" = timeout ]; then exit 124; fi +exec "$@" +`, { mode: 0o755 }); + for (const [ref, failure, expected] of [ + [`${sha}\trefs/heads/main`, '0', 'true'], + [`${'b'.repeat(40)}\trefs/heads/main`, '0', 'false'], + ['', '1', null], + ['', 'timeout', null], + ['', '0', null], + ['malformed', '0', null], + [`${sha}\trefs/heads/other`, '0', null], + [`${sha}\trefs/heads/main\n${sha}\trefs/heads/main`, '0', null], + ]) { const output = join(dir, 'output'); writeFileSync(output, ''); const result = spawnSync('bash', ['-e', '-c', script], { encoding: 'utf8', - env: { ...process.env, PATH: `${dir}:${process.env.PATH}`, GH_TOKEN: 'test-token', - EXPECTED_SHA: 'current', MOCK_SHA: sha, MOCK_HTTP_FAILURE: failure, + env: { ...process.env, PATH: `${dir}:${process.env.PATH}`, GIT_TERMINAL_PROMPT: '0', + EXPECTED_SHA: sha, MOCK_REF: ref, MOCK_FAILURE: failure, GITHUB_OUTPUT: output, GITHUB_STEP_SUMMARY: join(dir, 'summary') }, }); if (expected === null) { From 16093c4ca51216dd368ff3610d618917a13fa423 Mon Sep 17 00:00:00 2001 From: "Tiago Siebler @ Siebly.io" Date: Wed, 18 Nov 2026 15:27:08 +0000 Subject: [PATCH 3/3] fix(ci): keep deployment revision lookup minimal --- .github/workflows/publish.yml | 10 ++---- scripts/ci-plan.test.mjs | 58 ----------------------------------- 2 files changed, 3 insertions(+), 65 deletions(-) diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index f5ee3e3b..d059124b 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -79,17 +79,13 @@ jobs: # No repository checkout or build code runs with deployment permissions. - name: Reject an obsolete main revision id: current + timeout-minutes: 1 env: EXPECTED_SHA: ${{ github.sha }} GIT_TERMINAL_PROMPT: '0' + shell: bash run: | - remote_ref="$(timeout 30s git -c credential.helper= -c http.extraHeader= \ - ls-remote --exit-code https://github.com/sieblyio/docs.git refs/heads/main)" - if [[ ! "$remote_ref" =~ ^([0-9a-f]{40})$'\t'refs/heads/main$ ]]; then - echo 'Invalid main revision response.' >&2 - exit 1 - fi - current_sha="${BASH_REMATCH[1]}" + current_sha="$(git ls-remote --exit-code https://github.com/sieblyio/docs.git refs/heads/main | cut -f1)" if [ "$current_sha" = "$EXPECTED_SHA" ]; then echo 'deploy=true' >> "$GITHUB_OUTPUT" else diff --git a/scripts/ci-plan.test.mjs b/scripts/ci-plan.test.mjs index 3ecbb9fa..e4f63bbd 100644 --- a/scripts/ci-plan.test.mjs +++ b/scripts/ci-plan.test.mjs @@ -14,61 +14,3 @@ test('only push and manual runs on this repository main branch can deploy', () = { ...main, eventName: 'workflow_dispatch', ref: 'refs/tags/release' }, ]) assert.equal(canDeploy(context), false); }); - -test('deployment revision guard is credential-free and fails closed', async () => { - const { readFileSync, mkdtempSync, writeFileSync, rmSync } = await import('node:fs'); - const { tmpdir } = await import('node:os'); - const { join } = await import('node:path'); - const { spawnSync } = await import('node:child_process'); - const workflow = readFileSync(new URL('../.github/workflows/publish.yml', import.meta.url), 'utf8'); - const deploy = workflow.split('\n deploy:\n')[1]; - assert.doesNotMatch(deploy, /contents:|GH_TOKEN|github.token|Authorization:/); - assert.match(deploy, /GIT_TERMINAL_PROMPT: '0'/); - const script = deploy.split(' run: |\n')[1].split(' - name:')[0] - .split('\n').map(line => line.replace(/^ /, '')).join('\n'); - const dir = mkdtempSync(join(tmpdir(), 'docs-deploy-guard-')); - const sha = 'a'.repeat(40); - try { - writeFileSync(join(dir, 'git'), `#!/bin/bash -[[ "$*" == "-c credential.helper= -c http.extraHeader= ls-remote --exit-code https://github.com/sieblyio/docs.git refs/heads/main" ]] || exit 90 -[[ "$GIT_TERMINAL_PROMPT" == 0 ]] || exit 91 -if [ "$MOCK_FAILURE" = 1 ]; then exit 128; fi -printf '%s' "$MOCK_REF" -`, { mode: 0o755 }); - // Exercise timeout invocation on platforms without GNU timeout as well. - writeFileSync(join(dir, 'timeout'), `#!/bin/bash -[[ "$1" == 30s ]] || exit 92 -shift -if [ "$MOCK_FAILURE" = timeout ]; then exit 124; fi -exec "$@" -`, { mode: 0o755 }); - for (const [ref, failure, expected] of [ - [`${sha}\trefs/heads/main`, '0', 'true'], - [`${'b'.repeat(40)}\trefs/heads/main`, '0', 'false'], - ['', '1', null], - ['', 'timeout', null], - ['', '0', null], - ['malformed', '0', null], - [`${sha}\trefs/heads/other`, '0', null], - [`${sha}\trefs/heads/main\n${sha}\trefs/heads/main`, '0', null], - ]) { - const output = join(dir, 'output'); - writeFileSync(output, ''); - const result = spawnSync('bash', ['-e', '-c', script], { - encoding: 'utf8', - env: { ...process.env, PATH: `${dir}:${process.env.PATH}`, GIT_TERMINAL_PROMPT: '0', - EXPECTED_SHA: sha, MOCK_REF: ref, MOCK_FAILURE: failure, - GITHUB_OUTPUT: output, GITHUB_STEP_SUMMARY: join(dir, 'summary') }, - }); - if (expected === null) { - assert.notEqual(result.status, 0); - assert.equal(readFileSync(output, 'utf8'), ''); - } else { - assert.equal(result.status, 0, result.stderr); - assert.equal(readFileSync(output, 'utf8'), `deploy=${expected}\n`); - } - } - } finally { - rmSync(dir, { recursive: true, force: true }); - } -});