Skip to content

Commit 8ce43bb

Browse files
authored
ci(release): bind standalone checksums before upload (#2377)
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Co-authored-by: B'Elanna (Squad) <tamirdresher@users.noreply.github.com> Copilot-Session: 995eea17-c356-4912-90ea-5e5b144e7ba9
1 parent a3aa081 commit 8ce43bb

5 files changed

Lines changed: 474 additions & 11 deletions

File tree

‎.github/workflows/squad-release.yml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ jobs:
296296
NPM_RESULT: ${{ needs.npm.result }}
297297
NPM_OUTCOME: ${{ needs.npm.outputs.outcome }}
298298
STANDALONE_RESULT: ${{ needs.standalone.result }}
299+
STANDALONE_CHECKSUM_BINDING: ${{ needs.standalone.outputs.checksum_binding }}
299300
BUILD_STANDALONE: ${{ inputs.build_standalone }}
300301
PUBLISH_STANDALONE: ${{ inputs.publish_standalone }}
301302
PUBLISH_PACKAGE_MANAGERS: ${{ inputs.publish_package_managers }}
@@ -310,6 +311,11 @@ jobs:
310311
echo "::error::selected standalone/package-manager channels did not complete (job=$STANDALONE_RESULT)"; exit 1;
311312
}
312313
fi
314+
if [ "$PUBLISH_STANDALONE" = true ]; then
315+
[ "$STANDALONE_CHECKSUM_BINDING" = VERIFIED ] || {
316+
echo "::error::standalone archives were not bound to validation records and SHA256SUMS"; exit 1;
317+
}
318+
fi
313319
314320
publish-release:
315321
name: Publish GitHub release after selected channels complete

‎.github/workflows/squad-standalone-release.yml‎

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ on:
5555
WINGET_CREATE_GITHUB_TOKEN:
5656
description: 'Classic public_repo PAT for tamirdresher/winget-pkgs and upstream PRs'
5757
required: false
58+
outputs:
59+
checksum_binding:
60+
description: 'VERIFIED when every uploaded standalone archive is bound to its validation record and SHA256SUMS'
61+
value: ${{ jobs.publish.outputs.checksum_binding }}
5862
workflow_dispatch:
5963
inputs:
6064
node_version:
@@ -354,6 +358,8 @@ jobs:
354358
timeout-minutes: 10
355359
permissions:
356360
contents: write
361+
outputs:
362+
checksum_binding: ${{ steps.verify-checksum-binding.outputs.binding }}
357363
steps:
358364
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 #v7
359365
with:
@@ -376,7 +382,7 @@ jobs:
376382
pattern: squad-*-${{ inputs.source_sha }}
377383
merge-multiple: true
378384

379-
- name: Upload to release
385+
- name: Prepare release assets and SHA256SUMS
380386
env:
381387
GH_TOKEN: ${{ github.token }}
382388
TAG: ${{ inputs.release_tag || (github.ref_type == 'tag' && github.ref_name) }}
@@ -426,11 +432,11 @@ jobs:
426432
cat ./*.sha256 > SHA256SUMS.txt
427433
rm -f ./*.sha256
428434
435+
: > upload-paths.txt
429436
if [ "${#upload_paths[@]}" -gt 0 ]; then
430-
node ../scripts/release/assert-publish-identity.mjs --repo .. --source-sha "${{ inputs.source_sha }}" --dispatch-sha "${{ github.sha }}" --workflow-sha "${{ github.workflow_sha }}" --tag "${TAG}"
431-
gh release upload "${TAG}" "${upload_paths[@]}" --repo "${GITHUB_REPOSITORY}"
437+
printf '%s\n' "${upload_paths[@]}" > upload-paths.txt
432438
fi
433-
439+
printf '%s\n' "${reused_count}" > reused-count.txt
434440
checksum_changed=true
435441
if printf '%s\n' "${existing_assets[@]}" | grep -Fxq SHA256SUMS.txt; then
436442
gh release download "${TAG}" \
@@ -442,6 +448,39 @@ jobs:
442448
checksum_changed=false
443449
fi
444450
fi
451+
printf '%s\n' "${checksum_changed}" > checksum-changed.txt
452+
453+
- name: Verify archive, validation and SHA256SUMS binding
454+
id: verify-checksum-binding
455+
working-directory: artifacts
456+
env:
457+
SOURCE_SHA: ${{ inputs.source_sha }}
458+
run: |
459+
set -euo pipefail
460+
cli_version="$(node -p 'require("../packages/squad-cli/package.json").version')"
461+
node ../scripts/release/verify-standalone-checksums.mjs \
462+
--artifacts . \
463+
--checksums SHA256SUMS.txt \
464+
--expected-source-sha "${SOURCE_SHA}" \
465+
--expected-cli "${cli_version}" \
466+
--expected-node "${NODE_VERSION}" \
467+
--output standalone-checksum-binding.json
468+
echo "binding=VERIFIED" >> "${GITHUB_OUTPUT}"
469+
470+
- name: Upload to release
471+
env:
472+
GH_TOKEN: ${{ github.token }}
473+
TAG: ${{ inputs.release_tag || (github.ref_type == 'tag' && github.ref_name) }}
474+
working-directory: artifacts
475+
run: |
476+
set -euo pipefail
477+
mapfile -t upload_paths < upload-paths.txt
478+
reused_count="$(cat reused-count.txt)"
479+
checksum_changed="$(cat checksum-changed.txt)"
480+
if [ "${#upload_paths[@]}" -gt 0 ] && [ -n "${upload_paths[0]}" ]; then
481+
node ../scripts/release/assert-publish-identity.mjs --repo .. --source-sha "${{ inputs.source_sha }}" --dispatch-sha "${{ github.sha }}" --workflow-sha "${{ github.workflow_sha }}" --tag "${TAG}"
482+
gh release upload "${TAG}" "${upload_paths[@]}" --repo "${GITHUB_REPOSITORY}"
483+
fi
445484
if [ "${checksum_changed}" = "true" ]; then
446485
node ../scripts/release/assert-publish-identity.mjs --repo .. --source-sha "${{ inputs.source_sha }}" --dispatch-sha "${{ github.sha }}" --workflow-sha "${{ github.workflow_sha }}" --tag "${TAG}"
447486
gh release upload "${TAG}" SHA256SUMS.txt --clobber --repo "${GITHUB_REPOSITORY}"
@@ -456,7 +495,9 @@ jobs:
456495
- uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 #v7
457496
with:
458497
name: standalone-release-checksums-${{ inputs.source_sha }}
459-
path: artifacts/SHA256SUMS.txt
498+
path: |
499+
artifacts/SHA256SUMS.txt
500+
artifacts/standalone-checksum-binding.json
460501
retention-days: 30
461502
if-no-files-found: error
462503

Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
1+
#!/usr/bin/env node
2+
import { createHash } from 'node:crypto';
3+
import {
4+
createReadStream,
5+
existsSync,
6+
readFileSync,
7+
readdirSync,
8+
unlinkSync,
9+
writeFileSync,
10+
} from 'node:fs';
11+
import path from 'node:path';
12+
import { fileURLToPath } from 'node:url';
13+
import {
14+
assertNodeSecurityFloor,
15+
parseNodeVersion,
16+
} from './node-security-floor.mjs';
17+
18+
export const EXPECTED_TARGETS = [
19+
'darwin-x64',
20+
'darwin-arm64',
21+
'linux-x64',
22+
'linux-arm64',
23+
'win32-x64',
24+
'win32-arm64',
25+
];
26+
27+
const ARCHIVE_BY_TARGET = Object.fromEntries(
28+
EXPECTED_TARGETS.map((target) => [
29+
target,
30+
`squad-${target}.${target.startsWith('win32-') ? 'zip' : 'tar.gz'}`,
31+
]),
32+
);
33+
const SHA256 = /^[0-9a-f]{64}$/;
34+
const SOURCE_SHA = /^[0-9a-f]{40}$/;
35+
const VERSION = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-(?:preview|insider)\.(0|[1-9]\d*))?$/;
36+
37+
function fail(message) {
38+
throw new Error(`Standalone checksum binding failed: ${message}`);
39+
}
40+
41+
function exactNames(actual, expected, kind) {
42+
const actualSorted = [...actual].sort();
43+
const expectedSorted = [...expected].sort();
44+
if (
45+
actualSorted.length !== expectedSorted.length
46+
|| actualSorted.some((name, index) => name !== expectedSorted[index])
47+
) {
48+
fail(
49+
`${kind} must be exactly [${expectedSorted.join(', ')}], got [${actualSorted.join(', ')}]`,
50+
);
51+
}
52+
}
53+
54+
function parseChecksums(checksumsPath) {
55+
const lines = readFileSync(checksumsPath, 'utf8').split(/\r?\n/).filter(Boolean);
56+
if (lines.length !== EXPECTED_TARGETS.length) {
57+
fail(`SHA256SUMS must contain exactly ${EXPECTED_TARGETS.length} non-empty lines`);
58+
}
59+
60+
const entries = new Map();
61+
for (const line of lines) {
62+
const match = line.match(/^([0-9a-f]{64}) (squad-[A-Za-z0-9._-]+\.(?:tar\.gz|zip))$/);
63+
if (!match) fail(`malformed SHA256SUMS line ${JSON.stringify(line)}`);
64+
const [, sha256, name] = match;
65+
if (entries.has(name)) fail(`duplicate SHA256SUMS entry for ${name}`);
66+
entries.set(name, sha256);
67+
}
68+
exactNames(entries.keys(), Object.values(ARCHIVE_BY_TARGET), 'SHA256SUMS archives');
69+
return entries;
70+
}
71+
72+
function readValidation(validationPath, expectedTarget) {
73+
let validation;
74+
try {
75+
validation = JSON.parse(readFileSync(validationPath, 'utf8'));
76+
} catch (error) {
77+
fail(
78+
`${path.basename(validationPath)} is malformed JSON: ${
79+
error instanceof Error ? error.message : String(error)
80+
}`,
81+
);
82+
}
83+
if (!validation || typeof validation !== 'object' || Array.isArray(validation)) {
84+
fail(`${path.basename(validationPath)} must contain a JSON object`);
85+
}
86+
if (validation.schemaVersion !== 1) fail(`${expectedTarget} has unsupported validation schema`);
87+
if (validation.execution !== 'EXECUTED') fail(`${expectedTarget} status is not EXECUTED`);
88+
if (validation.target !== expectedTarget) {
89+
fail(`${path.basename(validationPath)} records target ${JSON.stringify(validation.target)}`);
90+
}
91+
return validation;
92+
}
93+
94+
async function hashFile(filePath) {
95+
const hash = createHash('sha256');
96+
for await (const chunk of createReadStream(filePath)) hash.update(chunk);
97+
return hash.digest('hex');
98+
}
99+
100+
export async function verifyStandaloneChecksums({
101+
artifactsDir,
102+
checksumsPath,
103+
expectedSourceSha,
104+
expectedCliVersion,
105+
expectedNodeVersion,
106+
outputPath,
107+
}) {
108+
const resolvedArtifacts = path.resolve(artifactsDir);
109+
const resolvedChecksums = path.resolve(checksumsPath);
110+
const resolvedOutput = path.resolve(outputPath);
111+
if (existsSync(resolvedOutput)) unlinkSync(resolvedOutput);
112+
if (!SOURCE_SHA.test(expectedSourceSha)) fail('expected source SHA must be lowercase 40-hex');
113+
if (!VERSION.test(expectedCliVersion)) fail(`invalid expected Squad version ${expectedCliVersion}`);
114+
115+
const normalizedNode = parseNodeVersion(expectedNodeVersion).normalized;
116+
const floor = assertNodeSecurityFloor(normalizedNode);
117+
if (!existsSync(resolvedArtifacts)) fail(`missing artifacts directory ${resolvedArtifacts}`);
118+
if (!existsSync(resolvedChecksums)) fail(`missing SHA256SUMS file ${resolvedChecksums}`);
119+
120+
const names = readdirSync(resolvedArtifacts);
121+
const archiveNames = names.filter((name) => /^squad-.+\.(?:tar\.gz|zip)$/.test(name));
122+
const validationNames = names.filter((name) => /^squad-.+-validation\.json$/.test(name));
123+
const expectedArchiveNames = Object.values(ARCHIVE_BY_TARGET);
124+
const expectedValidationNames = EXPECTED_TARGETS.map(
125+
(target) => `squad-${target}-validation.json`,
126+
);
127+
exactNames(archiveNames, expectedArchiveNames, 'archives');
128+
exactNames(validationNames, expectedValidationNames, 'validation records');
129+
130+
const checksums = parseChecksums(resolvedChecksums);
131+
const targets = [];
132+
for (const target of EXPECTED_TARGETS) {
133+
const archiveName = ARCHIVE_BY_TARGET[target];
134+
const archivePath = path.join(resolvedArtifacts, archiveName);
135+
const validationName = `squad-${target}-validation.json`;
136+
const validation = readValidation(path.join(resolvedArtifacts, validationName), target);
137+
138+
if (validation.sourceSha !== expectedSourceSha) {
139+
fail(`${target} sourceSha ${JSON.stringify(validation.sourceSha)} does not match expected SHA`);
140+
}
141+
if (validation.squadVersion !== expectedCliVersion) {
142+
fail(
143+
`${target} Squad version ${JSON.stringify(validation.squadVersion)} does not match ${
144+
expectedCliVersion
145+
}`,
146+
);
147+
}
148+
if (validation.processVersion !== `v${normalizedNode}`) {
149+
fail(
150+
`${target} processVersion ${JSON.stringify(validation.processVersion)} does not match vendored v${
151+
normalizedNode
152+
}`,
153+
);
154+
}
155+
assertNodeSecurityFloor(validation.processVersion);
156+
if (
157+
!validation.archive
158+
|| validation.archive.name !== archiveName
159+
|| !SHA256.test(validation.archive.sha256 ?? '')
160+
) {
161+
fail(`${target} has malformed archive validation metadata`);
162+
}
163+
164+
const computedSha256 = await hashFile(archivePath);
165+
const manifestSha256 = checksums.get(archiveName);
166+
if (
167+
computedSha256 !== validation.archive.sha256
168+
|| computedSha256 !== manifestSha256
169+
) {
170+
fail(
171+
`${target} SHA-256 mismatch: computed=${computedSha256} validation=${
172+
validation.archive.sha256
173+
} SHA256SUMS=${manifestSha256}`,
174+
);
175+
}
176+
targets.push({
177+
target,
178+
archive: archiveName,
179+
validation: validationName,
180+
sha256: computedSha256,
181+
execution: validation.execution,
182+
processVersion: validation.processVersion,
183+
squadVersion: validation.squadVersion,
184+
});
185+
}
186+
187+
const report = {
188+
schemaVersion: 1,
189+
status: 'VERIFIED',
190+
sourceSha: expectedSourceSha,
191+
squadVersion: expectedCliVersion,
192+
nodeVersion: normalizedNode,
193+
nodeSecurityFloor: floor.minimum,
194+
checksums: {
195+
name: path.basename(resolvedChecksums),
196+
sha256: await hashFile(resolvedChecksums),
197+
},
198+
targets,
199+
};
200+
writeFileSync(resolvedOutput, `${JSON.stringify(report, null, 2)}\n`);
201+
return report;
202+
}
203+
204+
function arg(name) {
205+
const index = process.argv.indexOf(name);
206+
const value = index >= 0 ? process.argv[index + 1] : '';
207+
if (!value) throw new Error(`Missing required ${name}`);
208+
return value;
209+
}
210+
211+
const isMain = process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url);
212+
if (isMain) {
213+
const report = await verifyStandaloneChecksums({
214+
artifactsDir: arg('--artifacts'),
215+
checksumsPath: arg('--checksums'),
216+
expectedSourceSha: arg('--expected-source-sha'),
217+
expectedCliVersion: arg('--expected-cli'),
218+
expectedNodeVersion: arg('--expected-node'),
219+
outputPath: arg('--output'),
220+
});
221+
console.log(
222+
`VERIFIED ${report.targets.length} standalone archives at ${report.sourceSha}: `
223+
+ `Squad ${report.squadVersion}, Node v${report.nodeVersion}`,
224+
);
225+
}

0 commit comments

Comments
 (0)