diff --git a/scripts/installer_build/launcherindex.py b/scripts/installer_build/launcherindex.py index 0e1e2a320..5ceffae3b 100644 --- a/scripts/installer_build/launcherindex.py +++ b/scripts/installer_build/launcherindex.py @@ -8,14 +8,13 @@ import json from pathlib import Path from core.paths import REPO_ROOT -from installer_build.pins import installer_sha256 +from installer_build.pins import installer_sha256, pinned_sha256 from installer_build.port import ( BATOCERA_ZIP, MUOS_APP, TOOLS_ZIP, packs_table, archive_members, - pinned_installer_sha256, ) @@ -55,7 +54,7 @@ def _member_bytes(entry: dict, root: Path) -> bytes: def build_index(root: Path = REPO_ROOT) -> dict: """Every launcher a card can carry, from the archives' members.""" actual = installer_sha256(root) - if actual != pinned_installer_sha256(root): + if actual != pinned_sha256("install.sh", root): raise ValueError("install.py is not the installer install.sh pins: re-pin the " "bootstraps before writing the launcher list") places = {place.key: place for place in _install.APP_PLACES.values()} diff --git a/scripts/installer_build/pins.py b/scripts/installer_build/pins.py index 48778c297..067c02a4f 100644 --- a/scripts/installer_build/pins.py +++ b/scripts/installer_build/pins.py @@ -10,15 +10,11 @@ from core.paths import REPO_ROOT INSTALLER = "install.py" -# Each bootstrap and its one pin line, the value between the two groups. +# Each bootstrap and its one pin line: the text before the value, the +# value, the text after it. PINS: dict[str, re.Pattern[str]] = { - "install.sh": re.compile(r'^(DEFAULT_INSTALL_SHA256=")[0-9a-f]{64}(")', re.M), - "install.ps1": re.compile(r'^(\s*\$defaultInstallSha256 = ")[0-9a-f]{64}(")', re.M), -} -# The value of that pin line, by bootstrap. -PIN: dict[str, re.Pattern[str]] = { - "install.sh": re.compile(r'^DEFAULT_INSTALL_SHA256="([0-9a-f]{64})"', re.M), - "install.ps1": re.compile(r'^\s*\$defaultInstallSha256 = "([0-9a-f]{64})"', re.M), + "install.sh": re.compile(r'^(DEFAULT_INSTALL_SHA256=")([0-9a-f]{64})(")', re.M), + "install.ps1": re.compile(r'^(\s*\$defaultInstallSha256 = ")([0-9a-f]{64})(")', re.M), } COMMAND = "python scripts/pin_installer.py" @@ -27,17 +23,20 @@ def installer_sha256(root: Path = REPO_ROOT) -> str: return file_digest(root / INSTALLER, "sha256") +def pinned_sha256(name: str, root: Path = REPO_ROOT) -> str: + """The SHA-256 the bootstrap *name* accepts for install.py. Exactly one + pin line: sh and PowerShell keep the last assignment, so a second line + would be the value the bootstrap runs with.""" + found = PINS[name].findall((root / name).read_bytes().decode("utf-8")) + if len(found) != 1: + raise ValueError(f"{name}: {len(found)} installer pin lines, expected 1") + return found[0][1] + + def stale_pins(root: Path = REPO_ROOT) -> list[str]: """The bootstraps whose pin is not the SHA-256 of install.py.""" digest = installer_sha256(root) - stale = [] - for name, pin in PINS.items(): - found = pin.search((root / name).read_bytes().decode("utf-8")) - if found is None: - raise ValueError(f"{name}: no installer pin line") - if found.group(0) != f"{found.group(1)}{digest}{found.group(2)}": - stale.append(name) - return stale + return [name for name in PINS if pinned_sha256(name, root) != digest] def write_pins(root: Path = REPO_ROOT) -> list[str]: @@ -47,7 +46,7 @@ def write_pins(root: Path = REPO_ROOT) -> list[str]: for name in stale_pins(root): path = root / name text = path.read_bytes().decode("utf-8") - new, count = PINS[name].subn(rf"\g<1>{digest}\g<2>", text) + new, count = PINS[name].subn(rf"\g<1>{digest}\g<3>", text) if count != 1: raise ValueError(f"{name}: {count} installer pin lines, expected 1") path.write_bytes(new.encode("utf-8")) diff --git a/scripts/installer_build/port.py b/scripts/installer_build/port.py index 8a7eca88d..f697deef4 100644 --- a/scripts/installer_build/port.py +++ b/scripts/installer_build/port.py @@ -6,7 +6,7 @@ import zipfile from pathlib import Path from core.paths import REPO_ROOT -from installer_build.pins import PIN, installer_sha256 +from installer_build.pins import installer_sha256, pinned_sha256 PORT_DIR = "retrobios" @@ -21,14 +21,6 @@ ARCHIVES = (PORT_ZIP, BATOCERA_ZIP, MUOS_APP, TOOLS_ZIP) PACKS_TABLE = "packs.txt" -def pinned_installer_sha256(root: Path = REPO_ROOT) -> str: - """The SHA-256 install.sh accepts for install.py.""" - found = PIN["install.sh"].search((root / "install.sh").read_text(encoding="utf-8")) - if found is None: - raise ValueError("install.sh pins no installer SHA-256") - return found.group(1) - - def _engine(folder: str, root: Path, mapping: bool = True) -> list[tuple[str, Path | None]]: """What every launcher runs: the installer, the interface launcher, the event reader, the licenses, and the gptokeyb mapping where one is read.""" @@ -134,7 +126,7 @@ def packs_table(root: Path) -> bytes: def build_archive(name: str, output: Path, root: Path = REPO_ROOT) -> Path: """Write the launcher archive *name* to *output* and return it.""" - pinned = pinned_installer_sha256(root) + pinned = pinned_sha256("install.sh", root) actual = installer_sha256(root) if actual != pinned: raise ValueError( diff --git a/scripts/installer_build/pypi.py b/scripts/installer_build/pypi.py index e001bb230..e3e0a2ad8 100644 --- a/scripts/installer_build/pypi.py +++ b/scripts/installer_build/pypi.py @@ -11,7 +11,7 @@ import tempfile from pathlib import Path from core.paths import REPO_ROOT -from installer_build.pins import PIN +from installer_build.pins import pinned_sha256 PACKAGE_DIR = REPO_ROOT / "packaging" / "pypi" @@ -58,8 +58,7 @@ def installer_bytes() -> bytes: """install.py, refused unless it is the one install.sh pins: the package then runs the installer the one-line command runs, under the same id.""" data = (REPO_ROOT / "install.py").read_bytes() - pins = PIN["install.sh"].findall((REPO_ROOT / "install.sh").read_text(encoding="utf-8")) - if pins != [hashlib.sha256(data).hexdigest()]: + if pinned_sha256("install.sh", REPO_ROOT) != hashlib.sha256(data).hexdigest(): raise ValueError("install.py is not the installer install.sh pins: recompute the pin") return data diff --git a/scripts/sitegen/bootstrap.py b/scripts/sitegen/bootstrap.py index 918c6abaf..c1aa4ae8c 100644 --- a/scripts/sitegen/bootstrap.py +++ b/scripts/sitegen/bootstrap.py @@ -20,7 +20,7 @@ from pathlib import Path from core.artifacts import write_if_changed from model.guides import PS1_NAME, SH_NAME -from installer_build.pins import PIN, installer_sha256 +from installer_build.pins import installer_sha256, pinned_sha256 from sitegen.config import build_commit REPO_RAW = "https://raw.githubusercontent.com/Abdess/retrobios" @@ -57,12 +57,10 @@ def served_text(name: str, root: Path, commit: str) -> str: pair served.""" source, line = SOURCES[name] text = (root / source).read_text(encoding="utf-8") - pinned = PIN[source].findall(text) - if len(pinned) != 1: - raise ValueError(f"{source}: {len(pinned)} installer pins, expected one") + pinned = pinned_sha256(source, root) actual = installer_sha256(root) - if pinned[0] != actual: - raise ValueError(f"{source} pins {pinned[0][:12]}, install.py is {actual[:12]}: " + if pinned != actual: + raise ValueError(f"{source} pins {pinned[:12]}, install.py is {actual[:12]}: " "re-pin the bootstrap before the site serves it") served, count = line.subn( lambda m: f"{m.group(1)}{installer_url(commit)}{m.group(2)}", text diff --git a/tests/test_handheld_launchers.py b/tests/test_handheld_launchers.py index eb332c287..56ec8f069 100644 --- a/tests/test_handheld_launchers.py +++ b/tests/test_handheld_launchers.py @@ -33,7 +33,7 @@ from pathlib import Path import yaml -from installer_build import port +from installer_build import pins, port REPO_ROOT = Path(__file__).resolve().parent.parent PORTS = REPO_ROOT / "ports" @@ -334,7 +334,7 @@ class TheArchives(unittest.TestCase): self.assertEqual(inside, []) def test_each_carries_the_pinned_installer(self): - pinned = port.pinned_installer_sha256() + pinned = pins.pinned_sha256("install.sh") for name, (_script, folder) in self.LAYOUT.items(): with self.subTest(archive=name): with zipfile.ZipFile(port.build_archive(name, self.out / name)) as zf: diff --git a/tests/test_install.py b/tests/test_install.py index bda67bcb4..d72801373 100644 --- a/tests/test_install.py +++ b/tests/test_install.py @@ -2537,6 +2537,25 @@ class TestAvailablePlatforms(unittest.TestCase): self.assertIn(digest, changed[0]) self.assertEqual((root / "install.sh").stat().st_mode, sh_mode) + def test_a_second_pin_line_is_refused(self): + """sh keeps the last assignment: a second pin line after the right one + is the value the bootstrap runs with, so the check refuses it.""" + from installer_build import pins + + with tempfile.TemporaryDirectory(dir=REPO_ROOT / "tmp" / "tests") as directory: + root = Path(directory) + for name in ("install.py", "install.sh", "install.ps1"): + shutil.copy2(REPO_ROOT / name, root / name) + text = (root / "install.sh").read_text(encoding="utf-8") + pin = f'DEFAULT_INSTALL_SHA256="{pins.installer_sha256(root)}"\n' + self.assertIn(pin, text) + doubled = text.replace(pin, f'{pin}DEFAULT_INSTALL_SHA256="{"0" * 64}"\n') + (root / "install.sh").write_text(doubled, encoding="utf-8") + with self.assertRaisesRegex(ValueError, "2 installer pin lines"): + pins.stale_pins(root) + with self.assertRaisesRegex(ValueError, "2 installer pin lines"): + pins.pinned_sha256("install.sh", root) + def test_shell_one_liner_downloads_verifies_and_forwards_arguments(self): scratch = REPO_ROOT / "tmp" / "tests" scratch.mkdir(parents=True, exist_ok=True) diff --git a/tests/test_port.py b/tests/test_port.py index 4bd29aaa8..be6212b25 100644 --- a/tests/test_port.py +++ b/tests/test_port.py @@ -25,6 +25,7 @@ import zipfile from pathlib import Path from installer_build import port as installer_build_port +from installer_build.pins import pinned_sha256 REPO_ROOT = Path(__file__).resolve().parent.parent PORT = REPO_ROOT / "ports" / "RetroBIOS" @@ -107,7 +108,7 @@ class ThePortArchive(unittest.TestCase): def test_bundled_installer_is_the_pinned_one(self): with zipfile.ZipFile(self._build()) as zf: bundled = hashlib.sha256(zf.read("retrobios/install.py")).hexdigest() - self.assertEqual(bundled, installer_build_port.pinned_installer_sha256()) + self.assertEqual(bundled, pinned_sha256("install.sh")) install_sh = (REPO_ROOT / "install.sh").read_text(encoding="utf-8") self.assertIn(f'DEFAULT_INSTALL_SHA256="{bundled}"', install_sh) diff --git a/tests/test_site_handheld.py b/tests/test_site_handheld.py index c5c6c3e13..fc282c2f5 100644 --- a/tests/test_site_handheld.py +++ b/tests/test_site_handheld.py @@ -69,7 +69,7 @@ class TheSiteServesTheLaunchers(unittest.TestCase): """build_port refuses an install.py the bootstraps do not pin: served, that launcher would refuse its own installer.""" with mock.patch.dict(sitehandheld._built, clear=True), \ - mock.patch.object(port, "pinned_installer_sha256", return_value="0" * 64), \ + mock.patch.object(port, "pinned_sha256", return_value="0" * 64), \ self.assertRaisesRegex(ValueError, "re-pin the bootstraps"): sitehandheld.write_launchers(self.tmp / "public", REPO_ROOT) self.assertFalse(list((self.tmp / "public").rglob("RetroBIOS*")))