diff --git a/scripts/check-installer-hash.sh b/scripts/check-installer-hash.sh index 2a34a3e5058..a4d1b07f4ff 100755 --- a/scripts/check-installer-hash.sh +++ b/scripts/check-installer-hash.sh @@ -6,7 +6,7 @@ # still match the immutable upstream checksum manifests. # # Checked artifacts: -# 1. OpenShell archives — scripts/install-openshell.sh release-asset table +# 1. OpenShell archives/formula — scripts/install-openshell.sh release-asset table # 2. Brev OpenShell CLI — scripts/brev-launchable-ci-cpu.sh release-asset table # # Usage: @@ -88,11 +88,11 @@ sha256_file() { check_openshell_release_assets() { local installer="${REPO_ROOT}/scripts/install-openshell.sh" local brev_installer="${REPO_ROOT}/scripts/brev-launchable-ci-cpu.sh" - local release_base workspace manifests spec manifest expected actual source asset pinned upstream + local release_base workspace manifests spec manifest expected actual source asset pinned upstream formula_asset local matches required_manifest required_matches local pin_records parser_error parser_errors parsed_version release_version="" record_extra local allowlist_entry allowlist_version allowlist_extra - local count=0 brev_count=0 published_count=0 failures=0 + local count=0 brev_count=0 published_count=0 expected_published_count=0 failures=0 local -a manifest_specs=() workspace=$(mktemp -d) manifests="${workspace}/published-sha256.txt" @@ -147,8 +147,11 @@ check_openshell_release_assets() { esac done <<<"$pin_records" - if [[ "$count" -ne 8 ]]; then - echo " STALE: expected 8 pinned OpenShell v${release_version:-unknown} assets, found ${count}." + # Transitional trust anchor: accept the current archive set and the same set + # plus openshell.rb. Tighten this to nine assets when the formula consumer + # lands. The base-trusted parser still rejects every other asset set. + if [[ "$count" -ne 8 && "$count" -ne 9 ]]; then + echo " STALE: expected 8 or 9 pinned OpenShell v${release_version:-unknown} assets, found ${count}." failures=$((failures + 1)) fi if [[ "$brev_count" -ne 2 ]]; then @@ -224,6 +227,29 @@ check_openshell_release_assets() { done while IFS=$'\t' read -r parsed_version source asset pinned record_extra; do + if [[ "$asset" == "openshell.rb" ]]; then + formula_asset="${workspace}/${asset}" + if ! fetch_file "${release_base}/${asset}" "$formula_asset"; then + echo " STALE: unable to download ${source} ${asset}." + failures=$((failures + 1)) + continue + fi + if ! actual=$(sha256_file "$formula_asset"); then + echo " STALE: unable to hash ${source} ${asset}." + failures=$((failures + 1)) + continue + fi + if [[ "$actual" == "$pinned" ]]; then + published_count=$((published_count + 1)) + echo " OK: ${source} ${asset} (${pinned})" + else + echo " STALE: ${source} ${asset} digest does not match the pinned v${release_version} release asset." + echo " pinned: ${pinned}" + echo " upstream: ${actual}" + failures=$((failures + 1)) + fi + continue + fi matches=$(awk -v asset="$asset" '$2 == asset { count++ } END { print count + 0 }' "$manifests") upstream=$(awk -v asset="$asset" '$2 == asset { print $1; exit }' "$manifests") if [[ "$matches" -eq 1 && "$pinned" == "$upstream" ]]; then @@ -238,8 +264,9 @@ check_openshell_release_assets() { fi done <<<"$pin_records" - if [[ "$published_count" -ne 10 ]]; then - echo " STALE: expected all 10 pinned asset references in the v${release_version} checksum manifests, matched ${published_count}." + expected_published_count=$((count + brev_count)) + if [[ "$published_count" -ne "$expected_published_count" ]]; then + echo " STALE: expected all ${expected_published_count} pinned asset references for v${release_version}, matched ${published_count}." failures=$((failures + 1)) fi return "$failures" diff --git a/scripts/checks/extract-installer-pins.mts b/scripts/checks/extract-installer-pins.mts index bddc0163d33..f5a0626ee1a 100644 --- a/scripts/checks/extract-installer-pins.mts +++ b/scripts/checks/extract-installer-pins.mts @@ -47,10 +47,22 @@ const MAX_INSTALLER_INPUT_BYTES = 1024 * 1024; // only in a prerequisite trust-anchor PR that keeps the currently selected // release; the later pin PR may then change release data without authorizing // any operational installer change. A mismatch reports the candidate hash. -const TRUSTED_INSTALLER_TEMPLATE_SHA256 = +// This transition accepts the current installer and the reviewed formula +// installer. Tighten this back to the formula installer hash when its consumer +// lands; accepting both is necessary because base-trusted CI validates both +// the prerequisite branch and the dependent pull request. Each trusted hash is +// coupled below to the exact asset set consumed by that template. +const TRUSTED_ARCHIVE_INSTALLER_TEMPLATE_SHA256 = "a101f002bd8e02aa7b38960ddcb76c9fca419bc3766f6870446f6a7e99e14d78"; -const TRUSTED_BREV_TEMPLATE_SHA256 = - "c0a4ddf25a02a9fe02b2df53a60942ea887610f04d4ce16a121b6e79a5aeff1a"; +const TRUSTED_FORMULA_INSTALLER_TEMPLATE_SHA256 = + "2b6a6195241d6b946fe29503d8d2d99d5b864864458f510ca129e3396248ac58"; +const TRUSTED_INSTALLER_TEMPLATE_SHA256_ALLOWLIST = [ + TRUSTED_ARCHIVE_INSTALLER_TEMPLATE_SHA256, + TRUSTED_FORMULA_INSTALLER_TEMPLATE_SHA256, +] as const; +const TRUSTED_BREV_TEMPLATE_SHA256_ALLOWLIST = [ + "c0a4ddf25a02a9fe02b2df53a60942ea887610f04d4ce16a121b6e79a5aeff1a", +] as const; const EXPECTED_INSTALLER_ASSETS = [ "openshell-x86_64-unknown-linux-musl.tar.gz", "openshell-aarch64-unknown-linux-musl.tar.gz", @@ -61,6 +73,14 @@ const EXPECTED_INSTALLER_ASSETS = [ "openshell-sandbox-x86_64-unknown-linux-gnu.tar.gz", "openshell-sandbox-aarch64-unknown-linux-gnu.tar.gz", ] as const; +const TRANSITIONAL_INSTALLER_ASSET = "openshell.rb"; +const EXPECTED_INSTALLER_ASSETS_BY_TEMPLATE_SHA256 = new Map([ + [TRUSTED_ARCHIVE_INSTALLER_TEMPLATE_SHA256, EXPECTED_INSTALLER_ASSETS], + [ + TRUSTED_FORMULA_INSTALLER_TEMPLATE_SHA256, + [...EXPECTED_INSTALLER_ASSETS, TRANSITIONAL_INSTALLER_ASSET], + ], +]); const EXPECTED_BREV_ASSETS = [ "openshell-x86_64-unknown-linux-musl.tar.gz", "openshell-aarch64-unknown-linux-musl.tar.gz", @@ -164,6 +184,14 @@ function assertExactAssetSet( } } +function assertInstallerAssetSet(pins: InstallerPin[], templateSha256: string): void { + // extractInstallerPins rejects duplicate assets before this transition check. + const expectedAssets = + EXPECTED_INSTALLER_ASSETS_BY_TEMPLATE_SHA256.get(templateSha256) ?? + fail(`installer template ${templateSha256} has no trusted asset contract`); + assertExactAssetSet(pins, expectedAssets, "installer pin table"); +} + // invalidState: the blueprint and stable runtime selectors request a newer // OpenShell release while both embedded hash tables still name an older, // independently valid release, so separate dependency and hash checks pass but @@ -655,9 +683,9 @@ function assertTrustedTemplate( source: string, functionNames: readonly string[], selectorPatterns: readonly RegExp[], - expectedSha256: string, + expectedSha256: readonly string[], label: string, -): void { +): string { const normalized = normalizeTrustedInstallerTemplate( source, functionNames, @@ -665,12 +693,13 @@ function assertTrustedTemplate( label, ); const actualSha256 = createHash("sha256").update(normalized).digest("hex"); - if (actualSha256 !== expectedSha256) { + if (!expectedSha256.includes(actualSha256)) { fail( `${label} operational template is not base-trusted; ` + - `expected_sha256=${expectedSha256}, actual_sha256=${actualSha256}`, + `expected_sha256=[${expectedSha256.join(", ")}], actual_sha256=${actualSha256}`, ); } + return actualSha256; } function skipSeparators(tokens: Token[], start: number): number { @@ -931,7 +960,6 @@ function runCli(): void { functionName: "openshell_cli_pinned_sha256", sourceLabel: "Brev launchable", }); - assertExactAssetSet(installerPins, EXPECTED_INSTALLER_ASSETS, "installer pin table"); assertExactAssetSet(brevPins, EXPECTED_BREV_ASSETS, "Brev pin table"); const pins = [...installerPins, ...brevPins]; const releaseVersions = [...new Set(pins.map((pin) => pin.releaseVersion))].sort(); @@ -947,7 +975,7 @@ function runCli(): void { `pinned_sandbox_build_version must contain at least one digest for release ${releaseVersion}`, ); } - assertTrustedTemplate( + const installerTemplateSha256 = assertTrustedTemplate( installerSource, ["openshell_pinned_sha256", "pinned_sandbox_build_version"], [ @@ -955,14 +983,15 @@ function runCli(): void { /^MAX_VERSION="([0-9]+\.[0-9]+\.[0-9]+)"$/gm, /^DEV_MIN_VERSION="([0-9]+\.[0-9]+\.[0-9]+)"$/gm, ], - TRUSTED_INSTALLER_TEMPLATE_SHA256, + TRUSTED_INSTALLER_TEMPLATE_SHA256_ALLOWLIST, "installer", ); + assertInstallerAssetSet(installerPins, installerTemplateSha256); assertTrustedTemplate( brevInstallerSource, ["openshell_cli_pinned_sha256"], [/^\s*stable\s*\|\s*auto\)\s*OPENSHELL_VERSION="v([0-9]+\.[0-9]+\.[0-9]+)"\s*;;\s*$/gm], - TRUSTED_BREV_TEMPLATE_SHA256, + TRUSTED_BREV_TEMPLATE_SHA256_ALLOWLIST, "Brev launchable", ); for (const [label, runtimeVersion] of [ diff --git a/test/installer-hash-check.test.ts b/test/installer-hash-check.test.ts index 194cf5d53c7..603e934e436 100644 --- a/test/installer-hash-check.test.ts +++ b/test/installer-hash-check.test.ts @@ -52,8 +52,15 @@ const ASSET_DIGESTS = new Map([ "openshell-sandbox-aarch64-unknown-linux-gnu.tar.gz", "2cf62cbd651e55d0f8750804e2b4025e0d6c8eea4564c87cda47a2c922941db0", ], + ["openshell.rb", "f53c62777fed23b42427822d231670451ee4358efeb2660c41a7a38919211b23"], ]); -const ASSETS = [...ASSET_DIGESTS.keys()]; +const FORMULA_ASSET = "openshell.rb"; +const FORMULA_DIGEST = ASSET_DIGESTS.get(FORMULA_ASSET)!; +const ASSETS = [...ASSET_DIGESTS.keys()].filter((asset) => asset !== FORMULA_ASSET); +const ARCHIVE_INSTALLER_TEMPLATE_SHA256 = + "a101f002bd8e02aa7b38960ddcb76c9fca419bc3766f6870446f6a7e99e14d78"; +const FORMULA_INSTALLER_TEMPLATE_SHA256 = + "2b6a6195241d6b946fe29503d8d2d99d5b864864458f510ca129e3396248ac58"; const UNPUBLISHED_ASSET = "openshell-sandbox-aarch64-unknown-linux-gnu-unpublished.tar.gz"; const OFFICIAL_UNEXPECTED_INSTALLER_ASSET = "openshell-driver-vm-x86_64-unknown-linux-gnu.tar.gz"; const OFFICIAL_UNEXPECTED_INSTALLER_DIGEST = @@ -80,7 +87,9 @@ type FixtureMode = | "brev-sha-command-bypass" | "complete" | "duplicate-brev-pin" + | "duplicate-installer-pin" | "failure" + | "formula-mismatch" | "incomplete-trusted-allowlist" | "installer-max-version-drift" | "installer-bypassed-comparison" @@ -218,6 +227,15 @@ const mutateSandboxBuildFunction = ( }; const INSTALLER_MUTATIONS: Partial string>> = { + "duplicate-installer-pin": (source) => { + const asset = ASSETS[0]; + const digest = ASSET_DIGESTS.get(asset ?? "") ?? "missing"; + const arm = ` v0.0.72:${asset}) + printf '%s\\n' "${digest}" + ;;`; + assert.ok(source.includes(arm), "installer duplicate-pin fixture arm must exist"); + return source.replace(arm, `${arm}\n${arm}`); + }, "installer-bypassed-comparison": (source) => source.replace('[ "$release_sha" = "$expected_sha" ]', "true"), "installer-changed-asset": (source) => @@ -529,9 +547,23 @@ function renderBrevTemplate(openshellVersion: string, pinFunction: string): stri ); } +function mapFixtureTemplateToFormulaContract(source: string): string { + // The fixture renders the current archive-only installer. Rebind the hashes + // in the copied parser so this test exercises the formula contract without + // copying the dependent PR's installer implementation into this branch. + const withoutArchiveContract = source.replace(ARCHIVE_INSTALLER_TEMPLATE_SHA256, "0".repeat(64)); + const formulaContract = withoutArchiveContract.replace( + FORMULA_INSTALLER_TEMPLATE_SHA256, + ARCHIVE_INSTALLER_TEMPLATE_SHA256, + ); + assert.notEqual(formulaContract, source, "trusted parser formula contract fixture must change"); + return formulaContract; +} + function createFixture( openshellVersion = "0.0.72", formatting: PinFormatting = "canonical", + includeFormula = false, ): string { const fixtureRoot = fs.mkdtempSync(path.join(os.tmpdir(), "nemoclaw-installer-hash-")); const scriptsDir = path.join(fixtureRoot, "scripts"); @@ -561,7 +593,12 @@ function createFixture( path.join(scriptsDir, "install-openshell.sh"), renderInstallerTemplate( openshellVersion, - renderPinFunction("openshell_pinned_sha256", ASSETS, openshellVersion, formatting), + renderPinFunction( + "openshell_pinned_sha256", + includeFormula ? [...ASSETS, FORMULA_ASSET] : ASSETS, + openshellVersion, + formatting, + ), ), ); fs.writeFileSync( @@ -613,6 +650,9 @@ case "$url" in openshell-sandbox-checksums-sha256.txt) printf '%s' '${CHECKSUM_MANIFESTS.get("openshell-sandbox-checksums-sha256.txt")}' >"$output" ;; + openshell.rb) + printf '%s\n' 'class Openshell < Formula; end' >"$output" + ;; esac ;; *) exit 22 ;; @@ -620,6 +660,28 @@ esac `, ); fs.chmodSync(path.join(binDir, "curl"), 0o755); + fs.writeFileSync( + path.join(binDir, "sha256sum"), + `#!/usr/bin/env bash +set -euo pipefail +case "\${1:-}" in + */openshell.rb) + case "\${NEMOCLAW_TEST_CURL_MODE:-}" in + formula-mismatch) digest='${"0".repeat(64)}' ;; + *) digest='${FORMULA_DIGEST}' ;; + esac + printf '%s %s\\n' "$digest" "$1" + ;; + *) + case "$(uname -s)" in + Darwin) /usr/bin/shasum -a 256 "$@" ;; + *) /usr/bin/sha256sum "$@" ;; + esac + ;; +esac +`, + ); + fs.chmodSync(path.join(binDir, "sha256sum"), 0o755); return fixtureRoot; } @@ -628,8 +690,10 @@ function runFixture( openshellVersion?: string, trustedChecker = false, formatting: PinFormatting = "canonical", + includeFormula = false, + formulaTemplate = false, ) { - const fixtureRoot = createFixture(openshellVersion, formatting); + const fixtureRoot = createFixture(openshellVersion, formatting, includeFormula); const targetChecker = path.join(fixtureRoot, "scripts", "check-installer-hash.sh"); const trustedRoot = fs.mkdtempSync(path.join(os.tmpdir(), "nemoclaw-trusted-hash-check-")); const trustedCheckerPath = path.join(trustedRoot, "scripts", "check-installer-hash.sh"); @@ -642,9 +706,15 @@ function runFixture( tempDirs.push(trustedRoot); fs.mkdirSync(path.dirname(trustedParserPath), { recursive: true }); fs.copyFileSync(path.join(REPO_ROOT, "scripts", "check-installer-hash.sh"), trustedCheckerPath); - fs.copyFileSync( + const trustedParserSource = fs.readFileSync( path.join(REPO_ROOT, "scripts", "checks", "extract-installer-pins.mts"), + "utf8", + ); + fs.writeFileSync( trustedParserPath, + formulaTemplate + ? mapFixtureTemplateToFormulaContract(trustedParserSource) + : trustedParserSource, ); fs.writeFileSync( targetChecker, @@ -695,6 +765,42 @@ describe("installer hash verification", () => { expect(result.stdout).toContain("All installer hashes are current"); }); + it("verifies the reviewed Homebrew formula during the trust-anchor transition", () => { + const result = runFixture("complete", undefined, true, "canonical", true, true); + + expect(result.status).toBe(0); + expect(result.stdout).toContain(`OK: installer ${FORMULA_ASSET} (${FORMULA_DIGEST})`); + expect(result.stdout).toContain("All installer hashes are current"); + }); + + it("fails closed when the Homebrew formula digest does not match", () => { + const result = runFixture("formula-mismatch", undefined, true, "canonical", true, true); + + expect(result.status).toBe(1); + expect(result.stdout).toContain( + "STALE: installer openshell.rb digest does not match the pinned v0.0.72 release asset", + ); + expect(result.stdout).not.toContain("All installer hashes are current"); + }); + + it("rejects a formula pin when the installer keeps the archive-only template", () => { + const result = runFixture("complete", undefined, true, "canonical", true); + + expect(result.status).toBe(1); + expect(result.stdout).toContain("unable to extract the OpenShell installer pin tables"); + expect(result.stdout).toContain("unexpected=[openshell.rb]"); + expect(result.stdout).not.toContain("All installer hashes are current"); + }); + + it("rejects the formula installer template when its formula pin is missing", () => { + const result = runFixture("complete", undefined, true, "canonical", false, true); + + expect(result.status).toBe(1); + expect(result.stdout).toContain("unable to extract the OpenShell installer pin tables"); + expect(result.stdout).toContain("missing=[openshell.rb]"); + expect(result.stdout).not.toContain("All installer hashes are current"); + }); + it("derives the release version from matching static installer pin tables", () => { const result = runFixture("complete", undefined, true); @@ -934,6 +1040,17 @@ describe("installer hash verification", () => { expect(result.stdout).not.toContain("All installer hashes are current"); }); + it("fails closed when the installer pin table contains a duplicate asset", () => { + const result = runFixture("duplicate-installer-pin", undefined, true); + + expect(result.status).toBe(1); + expect(result.stdout).toContain("unable to extract the OpenShell installer pin tables"); + expect(result.stdout).toContain( + `openshell_pinned_sha256 contains duplicate assets: ${ASSETS[0]}`, + ); + expect(result.stdout).not.toContain("All installer hashes are current"); + }); + it("does not let a pull request replace the trusted verifier with a success stub", () => { const result = runFixture("pr-checker-bypass", undefined, true);