|
| 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