Skip to content

armgit / ECC

Public Imported from GitHub
0

The agent harness performance optimization system. Skills, instincts, memory, security, and research-first development for Claude Code, Codex, Opencode, Cursor and beyond.

fix: enforce signed release provenance gates

Viggo Phillips1e4576e685f4
diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml
index bdad0d483..80fe58991 100644
--- a/.github/workflows/release.yml
+++ b/.github/workflows/release.yml
@@ -5,6 +5,8 @@ on:
tags: ['v*']
permissions:
+ actions: read
+ checks: read
contents: read
jobs:
@@ -43,6 +45,12 @@ jobs:
node-version: '20.x'
registry-url: 'https://registry.npmjs.org'
+ - name: Verify signed tag and exact-SHA CI gates
+ env:
+ GITHUB_TOKEN: ${{ github.token }}
+ RELEASE_TAG: ${{ github.ref_name }}
+ run: RELEASE_SHA="$(git rev-parse HEAD)" node scripts/ci/verify-release-gates.js
+
- name: Install dependencies
run: npm ci --ignore-scripts
diff --git a/.github/workflows/reusable-release.yml b/.github/workflows/reusable-release.yml
index b038b1b8c..0ee5001a8 100644
--- a/.github/workflows/reusable-release.yml
+++ b/.github/workflows/reusable-release.yml
@@ -18,6 +18,8 @@ on:
type: string
permissions:
+ actions: read
+ checks: read
contents: read
jobs:
@@ -57,6 +59,12 @@ jobs:
node-version: '20.x'
registry-url: 'https://registry.npmjs.org'
+ - name: Verify signed tag and exact-SHA CI gates
+ env:
+ GITHUB_TOKEN: ${{ github.token }}
+ RELEASE_TAG: ${{ inputs.tag }}
+ run: RELEASE_SHA="$(git rev-parse HEAD)" node scripts/ci/verify-release-gates.js
+
- name: Install dependencies
run: npm ci --ignore-scripts
diff --git a/scripts/ci/verify-release-gates.js b/scripts/ci/verify-release-gates.js
new file mode 100644
index 000000000..698c302c5
--- /dev/null
+++ b/scripts/ci/verify-release-gates.js
@@ -0,0 +1,151 @@
+'use strict';
+
+const API_VERSION = '2022-11-28';
+const DEFAULT_ATTEMPTS = 20;
+const DEFAULT_DELAY_MS = 30_000;
+
+function requiredEnvironment(env = process.env) {
+ const values = {
+ repository: env.GITHUB_REPOSITORY,
+ releaseSha: env.RELEASE_SHA,
+ releaseTag: env.RELEASE_TAG,
+ token: env.GITHUB_TOKEN,
+ };
+ for (const [name, value] of Object.entries(values)) {
+ if (!value) throw new Error(`Missing required release gate input: ${name}`);
+ }
+ if (!/^[0-9a-f]{40}$/.test(values.releaseSha)) {
+ throw new Error('RELEASE_SHA must be a full lowercase commit SHA');
+ }
+ if (!/^v[0-9]+\.[0-9]+\.[0-9]+(?:-[0-9A-Za-z.-]+)?$/.test(values.releaseTag)) {
+ throw new Error('RELEASE_TAG is not a supported version tag');
+ }
+ return values;
+}
+
+async function githubApi(path, { repository, token }, fetchImpl = fetch) {
+ const response = await fetchImpl(`https://api.github.com/repos/${repository}${path}`, {
+ headers: {
+ Accept: 'application/vnd.github+json',
+ Authorization: `Bearer ${token}`,
+ 'X-GitHub-Api-Version': API_VERSION,
+ },
+ });
+ if (!response.ok) {
+ throw new Error(`GitHub API ${path} failed with status ${response.status}`);
+ }
+ return response.json();
+}
+
+async function verifySignedAnnotatedTag(inputs, fetchImpl = fetch) {
+ const reference = await githubApi(
+ `/git/ref/tags/${encodeURIComponent(inputs.releaseTag)}`,
+ inputs,
+ fetchImpl
+ );
+ if (reference.object.type !== 'tag') {
+ throw new Error('Release tag must be annotated; lightweight tags are rejected');
+ }
+ const tagObject = await githubApi(`/git/tags/${reference.object.sha}`, inputs, fetchImpl);
+ if (tagObject.verification.verified !== true) {
+ const reason = tagObject.verification.reason || 'unknown';
+ throw new Error(`Release tag signature is not verified: ${reason}`);
+ }
+ if (tagObject.object.type !== 'commit' || tagObject.object.sha !== inputs.releaseSha) {
+ throw new Error('Verified release tag does not point at the checked-out commit');
+ }
+}
+
+function assessExactShaGates(runs, checks, releaseSha) {
+ const latestCi = runs
+ .filter(run => run.head_sha === releaseSha && run.name === 'CI')
+ .sort((left, right) => Number(right.id || 0) - Number(left.id || 0))[0];
+ const latestCodeql = latestByName(
+ checks.filter(check => check.head_sha === releaseSha && /codeql/i.test(check.name || ''))
+ );
+ if (latestCi?.status === 'completed' && latestCi.conclusion !== 'success') {
+ return { state: 'failed', reason: `CI concluded ${latestCi.conclusion}` };
+ }
+ const failedCodeql = latestCodeql.find(
+ check => check.status === 'completed' && check.conclusion !== 'success'
+ );
+ if (failedCodeql) {
+ return { state: 'failed', reason: `${failedCodeql.name} concluded ${failedCodeql.conclusion}` };
+ }
+ const ciPassed = latestCi?.status === 'completed' && latestCi.conclusion === 'success';
+ const codeqlPassed =
+ latestCodeql.length > 0 &&
+ latestCodeql.every(
+ check => check.status === 'completed' && check.conclusion === 'success'
+ );
+ return ciPassed && codeqlPassed
+ ? { state: 'passed' }
+ : { state: 'pending', reason: 'waiting for successful CI and CodeQL on the release SHA' };
+}
+
+function latestByName(checks) {
+ const latest = new Map();
+ for (const check of checks) {
+ const prior = latest.get(check.name);
+ if (!prior || Number(check.id || 0) > Number(prior.id || 0)) latest.set(check.name, check);
+ }
+ return [...latest.values()];
+}
+
+async function waitForExactShaGates(inputs, fetchImpl = fetch, sleep = defaultSleep) {
+ const attempts = positiveInteger(process.env.RELEASE_GATE_ATTEMPTS, DEFAULT_ATTEMPTS);
+ const delayMs = positiveInteger(process.env.RELEASE_GATE_DELAY_MS, DEFAULT_DELAY_MS);
+ for (let attempt = 1; attempt <= attempts; attempt += 1) {
+ const [workflowPayload, checkPayload] = await Promise.all([
+ githubApi(
+ `/actions/runs?head_sha=${inputs.releaseSha}&event=push&per_page=100`,
+ inputs,
+ fetchImpl
+ ),
+ githubApi(`/commits/${inputs.releaseSha}/check-runs?per_page=100`, inputs, fetchImpl),
+ ]);
+ const assessment = assessExactShaGates(
+ workflowPayload.workflow_runs || [],
+ checkPayload.check_runs || [],
+ inputs.releaseSha
+ );
+ if (assessment.state === 'passed') return;
+ if (assessment.state === 'failed') throw new Error(assessment.reason);
+ if (attempt < attempts) await sleep(delayMs);
+ }
+ throw new Error('Timed out waiting for successful exact-SHA CI and CodeQL checks');
+}
+
+function positiveInteger(value, fallback) {
+ if (value === undefined) return fallback;
+ const parsed = Number(value);
+ if (!Number.isSafeInteger(parsed) || parsed <= 0) {
+ throw new Error('Release gate retry settings must be positive integers');
+ }
+ return parsed;
+}
+
+function defaultSleep(delayMs) {
+ return new Promise(resolve => setTimeout(resolve, delayMs));
+}
+
+async function main() {
+ const inputs = requiredEnvironment();
+ await verifySignedAnnotatedTag(inputs);
+ await waitForExactShaGates(inputs);
+ console.log('Verified signed annotated tag and successful exact-SHA CI/CodeQL gates.');
+}
+
+if (require.main === module) {
+ main().catch(error => {
+ console.error(`Release gate verification failed: ${error.message}`);
+ process.exitCode = 1;
+ });
+}
+
+module.exports = {
+ assessExactShaGates,
+ requiredEnvironment,
+ verifySignedAnnotatedTag,
+ waitForExactShaGates,
+};
diff --git a/tests/ci/release-packed-artifact-workflow.test.js b/tests/ci/release-packed-artifact-workflow.test.js
index 4ec23ffc4..3659ff51a 100644
--- a/tests/ci/release-packed-artifact-workflow.test.js
+++ b/tests/ci/release-packed-artifact-workflow.test.js
@@ -9,6 +9,7 @@ const workflowPaths = [
'.github/workflows/release.yml',
'.github/workflows/reusable-release.yml',
];
+const { assessExactShaGates } = require('../../scripts/ci/verify-release-gates.js');
const lifecycleRunnerSource = load('tests/ci/packed-artifact-lifecycle.js');
let passed = 0;
@@ -49,6 +50,20 @@ console.log('\n=== Testing packed-artifact release workflows ===\n');
for (const workflowPath of workflowPaths) {
const source = load(workflowPath);
+ test(`${workflowPath} verifies signed tags and exact-SHA CI gates before building`, () => {
+ const verify = jobBlock(source, 'verify', 'lifecycle');
+ const gateIndex = verify.indexOf('name: Verify signed tag and exact-SHA CI gates');
+ const installIndex = verify.indexOf('name: Install dependencies');
+
+ assert.ok(gateIndex >= 0, 'missing release provenance gate');
+ assert.ok(installIndex > gateIndex, 'release provenance must be verified before dependencies run');
+ assert.match(verify, /node scripts\/ci\/verify-release-gates\.js/);
+ assert.match(verify, /RELEASE_SHA(?:=|:)/);
+ assert.match(verify, /RELEASE_TAG:/);
+ assert.match(source, /actions:\s*read/);
+ assert.match(source, /checks:\s*read/);
+ });
+
test(`${workflowPath} packs once and exports the package name and SHA-256`, () => {
assert.strictEqual(
(source.match(/npm pack --json/g) || []).length,
@@ -151,6 +166,40 @@ for (const workflowPath of workflowPaths) {
});
}
+test('release gate verifier requires a signed annotated tag, CI, and CodeQL', () => {
+ const verifierPath = path.join(repoRoot, 'scripts/ci/verify-release-gates.js');
+ assert.ok(fs.existsSync(verifierPath), 'missing release gate verifier');
+ const source = load('scripts/ci/verify-release-gates.js');
+ assert.match(source, /verification\.verified/);
+ assert.match(source, /object\.type[^\n]+tag/);
+ assert.match(source, /run\.name === 'CI'/);
+ assert.match(source, /codeql/i);
+ assert.match(source, /head_sha === releaseSha/);
+});
+
+test('release gate verifier accepts only successful checks for the exact SHA', () => {
+ const releaseSha = 'a'.repeat(40);
+ const passed = assessExactShaGates(
+ [{ name: 'CI', head_sha: releaseSha, status: 'completed', conclusion: 'success' }],
+ [{ name: 'CodeQL', head_sha: releaseSha, status: 'completed', conclusion: 'success' }],
+ releaseSha
+ );
+ const wrongSha = assessExactShaGates(
+ [{ name: 'CI', head_sha: 'b'.repeat(40), status: 'completed', conclusion: 'success' }],
+ [{ name: 'CodeQL', head_sha: releaseSha, status: 'completed', conclusion: 'success' }],
+ releaseSha
+ );
+ const failedCodeql = assessExactShaGates(
+ [{ name: 'CI', head_sha: releaseSha, status: 'completed', conclusion: 'success' }],
+ [{ name: 'CodeQL', head_sha: releaseSha, status: 'completed', conclusion: 'failure' }],
+ releaseSha
+ );
+
+ assert.strictEqual(passed.state, 'passed');
+ assert.strictEqual(wrongSha.state, 'pending');
+ assert.strictEqual(failedCodeql.state, 'failed');
+});
+
test('reusable release requires its input to resolve through the tag namespace', () => {
const source = load('.github/workflows/reusable-release.yml');
const verify = jobBlock(source, 'verify', 'lifecycle');