diff --git a/README.md b/README.md index bca8fda7e..d96f2cff7 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,9 @@ The public installer looks up signed releases for Linux x86_64/aarch64 and macOS The maintainer release DID is `did:key:z6MkgwHd2BCWe1jHMXPiR6H1q1RFPcv1YzhMbK5G1kBarbfe`. Compare the installer's `Maintainer DID:` line with this complete DID. Stop if they differ. +The published installer has this DID built in and trusts no other. It refuses a +different DID from `ELASTOS_MAINTAINER_DID` or `--maintainer-did` before it +downloads anything, so this comparison is the check. The maintainer changed the release signing key because the previous key was stored on the public server. diff --git a/docs/INSTALL.md b/docs/INSTALL.md index 902afae43..7b94735f3 100644 --- a/docs/INSTALL.md +++ b/docs/INSTALL.md @@ -369,6 +369,8 @@ The maintainer release DID is `did:key:z6MkgwHd2BCWe1jHMXPiR6H1q1RFPcv1YzhMbK5G1kBarbfe`. Compare the installer's `Maintainer DID:` line with the complete DID in the repository [README](../README.md#install-from-the-publisher). +The published installer has no override for this DID; the README says how it +refuses one. An existing Home with the old source pin refuses a release signed by the new maintainer. On the Runtimes that still trust the old key, `elastos update` diff --git a/scripts/install-bootstrap-test.py b/scripts/install-bootstrap-test.py index eb41a6795..ec4298466 100644 --- a/scripts/install-bootstrap-test.py +++ b/scripts/install-bootstrap-test.py @@ -74,8 +74,8 @@ def sysctl_mock(answers): FIXTURE_DID = "did:key:z6MkvDqGT54cXesYGvABpF1UapVNwjCqRcafi4Px6Thv5T3Z" -def unixfs_v0_cid(data): - """CIDv0 from scripts/release-signer.py, the publisher's implementation.""" +def release_signer(): + """scripts/release-signer.py, the publisher's implementation.""" global _SIGNER_CID if _SIGNER_CID is None: import importlib.util @@ -84,7 +84,20 @@ def unixfs_v0_cid(data): module = importlib.util.module_from_spec(spec) spec.loader.exec_module(module) _SIGNER_CID = module - return _SIGNER_CID.unixfs_metadata_cid(data) + return _SIGNER_CID + + +def unixfs_v0_cid(data): + """CIDv0 from scripts/release-signer.py, the publisher's implementation.""" + return release_signer().unixfs_metadata_cid(data) + + +def rendered_installer(did): + """The actual install.sh as the release signer stamps it for publication.""" + stamps = {"MAINTAINER_DID": did, "SOURCE_CONNECT_TICKET": "fixture-ticket", + "PUBLISHER_GATEWAY": "https://test.invalid", "PUBLISHER_NODE_ID": "fixture-node", + "IPNS_NAME": ""} + return release_signer().render_installer(INSTALLER.read_bytes(), stamps, did) _SIGNER_CID = None @@ -645,6 +658,8 @@ def __init__(self, head, release, did, system="Linux", machine="x86_64"): # Release publication stamps the reviewed source route into install.sh. self.installer.write_text(SOURCE.replace("__SOURCE_CONNECT_TICKET__", "fixture-ticket", 1) .replace("__PUBLISHER_NODE_ID__", "fixture-node", 1)) + # The repository template takes its DID and publisher URL from flags. + self.stamped = False self.home = self.root / "home" self.data = self.home / "xdg-data/elastos" self.binary = self.home / ".local/bin/elastos" @@ -695,6 +710,11 @@ def __init__(self, head, release, did, system="Linux", machine="x86_64"): def __enter__(self): return self + def use_rendered_installer(self): + """Run the published form: every trust anchor stamped, no trust flags.""" + self.installer.write_bytes(rendered_installer(self.did)) + self.stamped = True + def __exit__(self, *exc): self.directory.cleanup() @@ -730,9 +750,15 @@ def hide_commands(self, *names): (system / entry).symlink_to(target) self.path_tail = system - def run(self, *options, umask="0022", head_cid=""): + def run(self, *options, umask="0022", head_cid="", env=None, did_flag=None): + """did_flag: pass --maintainer-did DID (default: only to the unstamped template).""" seen = len(self.requests()) - result = shell('umask "$6"\n' + ''' + trust = [] if self.stamped else ["--publisher-gateway", "https://test.invalid"] + if (not self.stamped) if did_flag is None else did_flag: + trust = ["--maintainer-did", self.did, *trust] + extra = "unset ELASTOS_MAINTAINER_DID\n" + "".join( + f"export {name}={shlex.quote(value)}\n" for name, value in (env or {}).items()) + result = shell('umask "$6"\n' + extra + ''' export HOME="$1/home" XDG_DATA_HOME="$1/home/xdg-data" TMPDIR="$8" FIXTURES="$1" PATH="$1/mocks:${9:-$PATH}" # Apple Python otherwise adds bytecode caches to HOME during file-preservation checks. export PYTHONDONTWRITEBYTECODE=1 @@ -742,8 +768,7 @@ def run(self, *options, umask="0022", head_cid=""): # Feed the complete stamped script through stdin, as curl | bash does. cat "$3" | "$2" --noprofile --norc -s -- "${@:10}" ''', self.root, OPTIONS.bash, self.installer, self.system, self.machine, umask, head_cid, - self.tmpdir, self.path_tail or "", - "--maintainer-did", self.did, "--publisher-gateway", "https://test.invalid", *options) + self.tmpdir, self.path_tail or "", *trust, *options) return result, self.requests()[seen:] def runtime_calls(self): @@ -1039,6 +1064,82 @@ def test_installer_refuses_a_maintainer_did_with_control_text_before_printing_it self.assertNotIn("Maintainer DID:", result.stdout) self.assertNotIn("\x1b]", result.stdout + result.stderr) + def test_rendered_installer_installs_with_its_built_in_did_and_no_trust_flags(self): + did, head, release = installable_fixture() + with InstallerSandbox(head, release, did) as sandbox: + sandbox.use_rendered_installer() + self.assertNotIn("__MAINTAINER" + "_DID__", sandbox.installer.read_text()) + self.existing_installation(sandbox) + sandbox.respond("binary", RUNTIME_STUB) + self.assert_clean_install(sandbox, did, head, release, sandbox.home_state()) + + def test_rendered_installer_refuses_another_did_from_environment_or_flag_before_any_download(self): + # #434: the published installer has one trust root, the DID the signer + # stamped. No environment variable or flag can replace it, and the + # placeholder or the stamp variable's own name cannot make it look unstamped. + did, head, release = installable_fixture() + other = "did:key:z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK" + placeholder = "__MAINTAINER" + "_DID__" + cases = { + "environment": ({"ELASTOS_MAINTAINER_DID": other}, []), + "flag": ({}, ["--maintainer-did", other]), + "flag same, environment other": ({"ELASTOS_MAINTAINER_DID": other}, ["--maintainer-did", did]), + "environment same, flag other": ({"ELASTOS_MAINTAINER_DID": did}, ["--maintainer-did", other]), + "placeholder in environment": ({"ELASTOS_MAINTAINER_DID": placeholder}, []), + "placeholder as flag": ({}, ["--maintainer-did", placeholder]), + "stamp variable in environment": ({"MAINTAINER_DID_STAMP": placeholder, "ELASTOS_MAINTAINER_DID": other}, []), + "control text": ({"ELASTOS_MAINTAINER_DID": did + "\x1b]0;owned\x07"}, []), + "uninstall": ({"ELASTOS_MAINTAINER_DID": other}, ["--uninstall"]), + } + for name, (env, flags) in cases.items(): + with self.subTest(name), InstallerSandbox(head, release, did) as sandbox: + sandbox.use_rendered_installer() + sandbox.respond("binary", RUNTIME_STUB) + self.existing_installation(sandbox) + before = sandbox.home_state() + result, requests = sandbox.run(*flags, "--install-only", env=env, did_flag=False) + self.assertEqual(result.returncode, 1, result.stdout + result.stderr) + self.assertEqual(requests, []) + self.assertEqual(sandbox.downloads(), {}) + self.assert_no_installation_effects(sandbox, before, result) + self.assertIn("This installer has its maintainer DID built in.", result.stderr) + self.assertIn('"Maintainer DID:" line. Compare that line with the DID in the ElastOS README.', result.stderr) + self.assertNotIn("Maintainer DID: did", result.stdout) + self.assertNotIn("Check this computer", result.stdout) + self.assertNotIn(other, result.stdout + result.stderr) + self.assertNotIn("\x1b]", result.stdout + result.stderr) + + def test_rendered_installer_accepts_a_request_for_its_own_did(self): + # The launcher passes --maintainer-did with the DID it pins; the same DID changes nothing. + did, head, release = installable_fixture() + for env, flags in (({"ELASTOS_MAINTAINER_DID": did}, []), ({}, ["--maintainer-did", did]), + ({"ELASTOS_MAINTAINER_DID": did}, ["--maintainer-did", did])): + with self.subTest(env=env, flags=flags), InstallerSandbox(head, release, did) as sandbox: + sandbox.use_rendered_installer() + result, requests = sandbox.run(*flags, "--install-only", env=env, did_flag=False) + self.assertNotEqual(result.returncode, 0) + self.assertIn("artifact-request-blocked", result.stderr) + self.assertEqual(requests, self.REQUESTS) + self.assertIn(f"Maintainer DID: {did}", result.stdout) + + def test_unstamped_template_takes_its_did_from_the_flag_then_the_environment(self): + # Repo smokes and tests run the template; it still needs the DID from outside. + did, head, release = installable_fixture() + other = "did:key:z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK" + for env, did_flag, shown in (({"ELASTOS_MAINTAINER_DID": did}, False, did), + ({}, True, did), + ({"ELASTOS_MAINTAINER_DID": other}, True, did)): + with self.subTest(env=env, did_flag=did_flag), InstallerSandbox(head, release, did) as sandbox: + result, requests = sandbox.run("--install-only", env=env, did_flag=did_flag) + self.assertIn("artifact-request-blocked", result.stderr) + self.assertEqual(requests, self.REQUESTS) + self.assertIn(f"Maintainer DID: {shown}", result.stdout) + with InstallerSandbox(head, release, did) as sandbox: + result, requests = sandbox.run("--install-only", did_flag=False) + self.assertEqual(result.returncode, 1) + self.assertEqual(requests, []) + self.assertIn("MAINTAINER_DID not set. This repository copy of install.sh has no built-in DID.", result.stderr) + def test_stamped_head_cid_must_match_the_verified_head(self): did, head, release = installable_fixture() verified = unixfs_v0_cid(head) diff --git a/scripts/install.sh b/scripts/install.sh index 6891483ec..fc2e2b6f3 100755 --- a/scripts/install.sh +++ b/scripts/install.sh @@ -7,12 +7,15 @@ # ./scripts/install.sh --publisher-gateway https:// --maintainer-did did:key:z6Mk... # ./scripts/install.sh --help # -# Required (stamped into published installers): +# Required (stamped into published installers by render_installer in +# scripts/release-signer.py): # ELASTOS_PUBLISHER_GATEWAY env var or --publisher-gateway -# ELASTOS_MAINTAINER_DID env var or --maintainer-did -# -# Trust anchors can be provided via env vars or CLI flags. In the canonical -# bootstrap flow, they should already be stamped into install.sh. +# The maintainer DID. A published (stamped) installer has it built in, and +# ELASTOS_MAINTAINER_DID or --maintainer-did with a different DID is refused +# before any download. Compare its "Maintainer DID:" line with the DID in +# the README ("Install from the publisher"). Only this unstamped repository +# copy, which repo smokes and tests run, takes the DID from +# ELASTOS_MAINTAINER_DID or --maintainer-did . # # Downloads the signed release head, release envelope, and Runtime binary. # Runtime setup fetches component metadata over Carrier. @@ -40,13 +43,12 @@ set -euo pipefail # ── Trust anchors (baked in by publish-release.sh) ──────────────────── # These placeholders are replaced with real values at publish time. -# Override via env vars or CLI flags if needed. -MAINTAINER_DID="${ELASTOS_MAINTAINER_DID:-__MAINTAINER_DID__}" -MAINTAINER_DID_PLACEHOLDER="__MAINTAINER""_DID__" -if [[ "$MAINTAINER_DID" == "$MAINTAINER_DID_PLACEHOLDER" ]]; then - MAINTAINER_DID="" -fi +# The trust root. The stamp reads no environment: select_maintainer_did +# decides from this baked value alone whether the installer is stamped. +MAINTAINER_DID_STAMP="__MAINTAINER_DID__" +MAINTAINER_DID_FROM_FLAG="" +MAINTAINER_DID="" # Publisher stamp. A frozen installer leaves this empty, and an empty stamp # stays empty so a fresh install stores no head CID. A non-empty stamp must # equal the UnixFS CIDv0 of the verified release-head.json bytes. A stamp that @@ -118,7 +120,10 @@ show_help() { echo " ./scripts/install.sh [options]" echo "" echo -e "${BOLD}Options:${NC}" - echo " --maintainer-did DID Override maintainer DID trust anchor" + echo " --maintainer-did DID Repository copy only: the maintainer DID to trust." + echo " A published installer has its DID built in and refuses" + echo " a different one. Compare its \"Maintainer DID:\" line" + echo " with the DID in the ElastOS README." echo " --publisher-gateway URL Bootstrap publisher URL (stamped for normal installs)" echo " --install-dir PATH Binary install directory (default: ~/.local/bin)" echo " --install-only Install Runtime without setup or opening Home" @@ -157,6 +162,25 @@ die() { echo -e "${RED:-}Error:${NC:-} $*" >&2; exit 1; } die_exact() { printf '%bError:%b %s\n' "${RED:-}" "${NC:-}" "$*" >&2; exit 1; } # The staged Runtime must stay the file whose SHA-256 was checked. RUNTIME_CHANGED="The downloaded Runtime changed on this computer after its check. Nothing was installed. Run the installer again." +# A stamped installer has one trust root, its built-in maintainer DID. +MAINTAINER_DID_BUILT_IN="This installer has its maintainer DID built in. It does not accept a different DID from ELASTOS_MAINTAINER_DID or --maintainer-did. The installer stopped before it downloaded or changed anything. Remove that setting and run the installer again. When the installer runs, it shows the built-in DID on its \"Maintainer DID:\" line. Compare that line with the DID in the ElastOS README." + +# The DID that verifies the release. STAMP is the value render_installer baked +# in; the split placeholder below survives stamping, so only the unstamped +# repository template matches it. That template takes the flag, then the +# environment. A stamped installer keeps its stamp and refuses a request for +# any other DID; a request for the same DID changes nothing. +select_maintainer_did() { + local stamp="$1" from_env="$2" from_flag="$3" requested + if [[ "$stamp" == "__MAINTAINER""_DID__" ]]; then + MAINTAINER_DID="${from_flag:-$from_env}" + return 0 + fi + for requested in "$from_env" "$from_flag"; do + [[ -z "$requested" || "$requested" == "$stamp" ]] || die "$MAINTAINER_DID_BUILT_IN" + done + MAINTAINER_DID="$stamp" +} # Every read of a verified envelope must see the SHA-256 its signature check saw. RELEASE_CHANGED="The release files changed on this computer after they were checked. Nothing was installed. Run the installer again." @@ -1264,7 +1288,7 @@ while [[ $# -gt 0 ]]; do --help|-h) show_help ;; --maintainer-did) [[ -z "${2:-}" ]] && die "Usage: --maintainer-did did:key:z6Mk..." - MAINTAINER_DID="$2"; shift 2 ;; + MAINTAINER_DID_FROM_FLAG="$2"; shift 2 ;; --publisher-gateway) [[ -z "${2:-}" ]] && die "Usage: --publisher-gateway https://" PUBLISHER_GATEWAY="${2%/}"; shift 2 ;; @@ -1278,6 +1302,8 @@ while [[ $# -gt 0 ]]; do *) die "Unknown option: $1. Run --help for usage." ;; esac done +# Before any other step, so a refused DID request downloads and changes nothing. +select_maintainer_did "$MAINTAINER_DID_STAMP" "${ELASTOS_MAINTAINER_DID:-}" "$MAINTAINER_DID_FROM_FLAG" if [[ "$REMOVE_DATA" == true && "$UNINSTALL" != true ]]; then die "--remove-data deletes your data and works only with --uninstall" @@ -1304,7 +1330,7 @@ fi PG="${PUBLISHER_GATEWAY%/}" if [[ -z "$MAINTAINER_DID" ]]; then - die "MAINTAINER_DID not set. Either:\n 1. Set ELASTOS_MAINTAINER_DID env var, or\n 2. Pass --maintainer-did ." + die "MAINTAINER_DID not set. This repository copy of install.sh has no built-in DID. Either:\n 1. Set ELASTOS_MAINTAINER_DID env var, or\n 2. Pass --maintainer-did ." fi # The DID reaches the terminal before its signature check, so only base58 did:key text passes. if [[ ! "$MAINTAINER_DID" =~ ^did:key:z[1-9A-HJ-NP-Za-km-z]{1,128}$ ]]; then diff --git a/scripts/release-signer-test.py b/scripts/release-signer-test.py index 050e40c0c..649a935f8 100644 --- a/scripts/release-signer-test.py +++ b/scripts/release-signer-test.py @@ -933,6 +933,19 @@ def test_template_hash_tree_mode_and_injection_are_refused(self): with self.assertRaises(ValueError): S.render_installer(self.template, {**self.stamps, "HEAD_CID": "circular-head"}, DID) + def test_rendered_public_installer_keeps_the_did_stamp_free_of_the_environment(self): + # #434: render the actual scripts/install.sh. The DID lands in the one + # stamp line, which reads no environment, and the split placeholder the + # installer compares the stamp with survives, so it knows it is stamped. + # install-bootstrap-test.py runs this rendered form against overrides. + template = SOURCE.with_name("install.sh").read_bytes() + text = S.render_installer(template, self.stamps, DID).decode() + self.assertEqual(text.count(f'MAINTAINER_DID_STAMP="{DID}"\n'), 1) + self.assertNotIn("__MAINTAINER_DID__", text) + self.assertIn('"__MAINTAINER""_DID__"', text) + self.assertNotIn("ELASTOS_MAINTAINER_DID:-did:key", text) + self.assertEqual(text.count(DID), 1) + def test_unbound_artifact_is_refused(self): extra = b"unadvertised" (self.root / "extra").write_bytes(extra) diff --git a/scripts/update-hop-compare-test.py b/scripts/update-hop-compare-test.py index cba68aaf7..a366ce4d6 100644 --- a/scripts/update-hop-compare-test.py +++ b/scripts/update-hop-compare-test.py @@ -960,6 +960,12 @@ def test_stamped_real_installer_defaults_are_read_without_execution(self): installer.write_text(stamped + '\nMAINTAINER_DID="${ELASTOS_MAINTAINER_DID:-substituted}"\n') with self.assertRaisesRegex(ValueError, "literal trust default"): observer.cli_installer_metadata(installer, {}) + # A frozen installer from before #434 keeps the environment default form. + legacy = stamped.replace(f'MAINTAINER_DID_STAMP="{self.manifest["signer_did"]}"', + f'MAINTAINER_DID="${{ELASTOS_MAINTAINER_DID:-{self.manifest["signer_did"]}}}"') + self.assertNotEqual(legacy, stamped) + installer.write_text(legacy) + self.assertEqual(observer.cli_installer_metadata(installer, {}), [self.manifest["signer_did"], ""]) def test_cleanup_does_not_signal_recycled_historic_process_groups(self): class Exited: diff --git a/scripts/update-hop-compare.py b/scripts/update-hop-compare.py index f8b6ac255..936522602 100755 --- a/scripts/update-hop-compare.py +++ b/scripts/update-hop-compare.py @@ -640,12 +640,16 @@ def cli_environment(home_path): def cli_installer_metadata(installer, env): # The stamped defaults live inside the executable guard; sourcing skips it. # Read the two literal defaults without running any installer action. + # Installers before #434 default MAINTAINER_DID from the environment; later + # ones keep the stamp in MAINTAINER_DID_STAMP and read no environment for it. text = installer.read_text() values = [] for variable in ("MAINTAINER_DID", "HEAD_CID"): need("ELASTOS_" + variable not in env, "fixture trust override is unavailable") matches = re.findall(r'^' + variable + r'="\$\{ELASTOS_' + variable + r':-([^$\\"\r\n}]*)\}"$', text, re.M) + if variable == "MAINTAINER_DID": + matches += re.findall(r'^MAINTAINER_DID_STAMP="([^$\\"\r\n}]*)"$', text, re.M) need(len(matches) == 1, "frozen installer literal trust default unavailable") value = matches[0] values.append("" if value == "__" + variable + "__" else value)