diff --git a/changelog.d/1768-uk-chronicle-rebuild-emudhra.fixed.md b/changelog.d/1768-uk-chronicle-rebuild-emudhra.fixed.md new file mode 100644 index 0000000000..f6d50a4f04 --- /dev/null +++ b/changelog.d/1768-uk-chronicle-rebuild-emudhra.fixed.md @@ -0,0 +1 @@ +The UK Chronicle rebuild's Modal image (`tools/modal_rebuild_uk_chronicle_artifact.py`) stops trusting emSign ECC Root CA - G3, the eMudhra CA certificate that certifi and Debian distrust over eMudhra's disclosed intermediate keys, as the US Modal image does (`emudhra-ca-trust.fixed.md`). Before any other download it upgrades `ca-certificates` and replaces the cached `debian_slim` base's pip 25.3 with a hash-pinned pip 26.2.2; it then installs uv 0.13.4 instead of 0.11.7 and requires certifi 2026.10.19 or later beside `huggingface_hub`. So neither the packages an upload runs with `HF_TOKEN` nor the upload's connection to the Hub depends on that root. uv 0.13.4 installs Python 3.14.8 for the rebuild, where uv 0.11.7 installed 3.14.4. diff --git a/docs/uk-cut-anyone.md b/docs/uk-cut-anyone.md index ce82d62e4e..5abf38d029 100644 --- a/docs/uk-cut-anyone.md +++ b/docs/uk-cut-anyone.md @@ -235,6 +235,14 @@ the private repositories, is attached only with ephemeral secret for that run; no named workspace secret keeps it. A rebuild without `--upload` sends no credential at all. +As the US Modal image does, the rebuild's image stops trusting emSign ECC Root +CA - G3, the eMudhra root that certifi and Debian distrust over eMudhra's +disclosed intermediate keys, before it downloads anything else. It upgrades +`ca-certificates`, replaces the base's pip with a hash-pinned pip 26.2.2, and +installs uv 0.13.4 and certifi 2026.10.19 or later beside `huggingface_hub`. +uv 0.13.4 installs Python 3.14.8, where the app's earlier uv 0.11.7 installed +3.14.4; the receipt records the one used. + ### The reference build A cut checks and certifies its candidate against the current build of the diff --git a/packages/microcosm-build/tests/engine_free/uk/test_modal_rebuild_uk_chronicle_artifact.py b/packages/microcosm-build/tests/engine_free/uk/test_modal_rebuild_uk_chronicle_artifact.py new file mode 100644 index 0000000000..f6cd686f59 --- /dev/null +++ b/packages/microcosm-build/tests/engine_free/uk/test_modal_rebuild_uk_chronicle_artifact.py @@ -0,0 +1,98 @@ +"""tools/modal_rebuild_uk_chronicle_artifact.py's image, against a stub ``modal``. + +The app's credential tests are in ``test_modal_uk_cut.py``; these check the +image the rebuild runs in. Nothing here builds an image or contacts Modal. +""" + +from __future__ import annotations + +import importlib.util +import sys +from pathlib import Path +from unittest.mock import MagicMock + +ROOT = Path(__file__).resolve().parents[5] +REBUILD_TOOL = ROOT / "tools" / "modal_rebuild_uk_chronicle_artifact.py" +US_PLAN_TOOL = ROOT / "tools" / "modal_us_stage_plan.py" + + +def _load_rebuild_app(monkeypatch): + """The rebuild app as the launcher sees it: ``modal.is_local()`` is true.""" + + modal_stub = MagicMock(name="modal") + modal_stub.is_local.return_value = True + modal_stub.App.return_value.function.side_effect = lambda **options: ( + lambda function: setattr(function, "modal_options", options) or function + ) + modal_stub.App.return_value.local_entrypoint.side_effect = lambda **_: ( + lambda function: function + ) + monkeypatch.setitem(sys.modules, "modal", modal_stub) + spec = importlib.util.spec_from_file_location( + "modal_rebuild_uk_chronicle_artifact_under_test", REBUILD_TOOL + ) + module = importlib.util.module_from_spec(spec) + assert spec.loader is not None + spec.loader.exec_module(module) + return module, modal_stub + + +def _load_us_plan(monkeypatch): + """The US Modal stage runner's plan module, loaded by path like the tools.""" + + spec = importlib.util.spec_from_file_location( + "modal_us_stage_plan_beside_chronicle_rebuild", US_PLAN_TOOL + ) + module = importlib.util.module_from_spec(spec) + assert spec.loader is not None + # dataclasses resolve string annotations through sys.modules. + monkeypatch.setitem(sys.modules, spec.name, module) + spec.loader.exec_module(module) + return module + + +def _image_chain(modal_stub): + """Each image-definition call as (step name, arguments, options), in order.""" + + return [ + (call_name.rsplit(".", 1)[-1], call_args, call_options) + for call_name, call_args, call_options in modal_stub.mock_calls + if call_name.startswith("Image.debian_slim") + ] + + +def test_the_rebuild_image_trusts_no_compromised_certificate_authority(monkeypatch): + """As the US Modal image does, before anything else is downloaded. + + emSign ECC Root CA - G3, the eMudhra root distrusted over eMudhra's + disclosed intermediate keys: uv before 0.13.4 compiles it in, the cached + base's pip 25.3 bundles it, and the base's CA store, which git's clone of + Chronicle and the standard library read, holds it until it is upgraded. + The upload's Hub client checks the Hub's certificate against certifi on + the image's own Python. + """ + + us_plan = _load_us_plan(monkeypatch) + module, modal_stub = _load_rebuild_app(monkeypatch) + chain = _image_chain(modal_stub) + assert [step_name for step_name, _, _ in chain] == [ + "debian_slim", + "apt_install", + "run_commands", + "pip_install", + "run_commands", + "env", + ] + assert "ca-certificates" in chain[1][1] + # The hash-pinned pip replaces the base's before pip downloads anything else. + assert chain[2][1] == (us_plan.image_pip_bootstrap_command(),) + assert chain[3][1] == ( + f"uv=={us_plan.IMAGE_UV_VERSION}", + f"huggingface_hub=={module.HF_HUB_VERSION}", + f"certifi>={us_plan.RUNNER_CERTIFI_MINIMUM_VERSION}", + ) + # The rebuild's own Python comes from the upgraded uv. + assert chain[4][1] == (f"uv python install {module.PYTHON_VERSION}",) + # No step before the environment passes options such as another index or + # a trusted host. + assert [step_options for _, _, step_options in chain[1:5]] == [{}, {}, {}, {}] diff --git a/tools/modal_rebuild_uk_chronicle_artifact.py b/tools/modal_rebuild_uk_chronicle_artifact.py index d653068cd5..71f7e3f55e 100644 --- a/tools/modal_rebuild_uk_chronicle_artifact.py +++ b/tools/modal_rebuild_uk_chronicle_artifact.py @@ -14,6 +14,13 @@ files against the pin. Results go to the ``microcosm-uk-chronicle`` Modal volume under ``//``, with a receipt beside them. +As in the US Modal image, before it downloads anything else the image stops +trusting emSign ECC Root CA - G3, the eMudhra root that certifi and Debian +distrust over eMudhra's disclosed intermediate keys (an upgraded CA store, a +hash-pinned pip, uv 0.13.4 and a certifi floor). So neither the packages an +upload runs with ``HF_TOKEN`` nor the upload's own connection to the Hub +depends on that root. + Check the pin and print the plan (plain Python; contacts no Modal service and sends nothing):: @@ -44,6 +51,7 @@ import json import os import resource +import shlex import shutil import subprocess import sys @@ -59,8 +67,26 @@ HF_REPO = "policyengine/microcosm-uk-build-inputs" CHRONICLE_URL = "https://github.com/PolicyEngine/chronicle.git" PYTHON_VERSION = "3.14" -UV_VERSION = "0.11.7" +# emSign ECC Root CA - G3, the eMudhra root distrusted over eMudhra's +# disclosed intermediate keys, is dropped as in the US Modal image +# (tools/modal_us_stage_plan.py, whose values the tests compare with these). +# uv verifies HTTPS against a CA list compiled into it, and 0.13.4 is the +# first release whose list drops it. +UV_VERSION = "0.13.4" +# Modal's cached debian_slim base carries pip 25.3, whose bundled certifi +# still contains emSign ECC Root CA - G3, so the image replaces it with this +# pip before downloading anything else. The wheel's sha256, as PyPI publishes +# it, not the connection, makes that one download authentic. +PIP_VERSION = "26.2.2" +PIP_WHEEL_SHA256 = "fd7f4dfa8190e014013b9f08f3e973f4e4c26ffd179a204895533a7254813a70" +PIP_BOOTSTRAP_REQUIREMENTS = "/tmp/pip-bootstrap.txt" HF_HUB_VERSION = "1.18.0" +# The upload's Hub client checks the Hub's certificate against certifi's CA +# list on the image's own Python. certifi 2026.10.19 and later leave out +# emSign ECC Root CA - G3 (2026.10.18 left out every emSign root, and +# 2026.10.19 restored the three certifi judged unaffected). Stated, since +# Modal reuses a cached layer while its definition is unchanged. +CERTIFI_MINIMUM_VERSION = "2026.10.19" FEED_PIN = Path(__file__).resolve().parents[1] / ( "packages/microcosm-build/src/microcosm/build/uk/chronicle_feed.json" ) @@ -91,10 +117,39 @@ def _secrets() -> list[modal.Secret]: return [modal.Secret.from_dict({"HF_TOKEN": token})] if token else [] +def pip_bootstrap_command() -> str: + """The shell step that replaces the base's pip before any package download. + + ``--require-hashes`` refuses any file whose sha256 is not + ``PIP_WHEEL_SHA256``, whatever index or certificate answers the request. + """ + + requirement = f"pip=={PIP_VERSION} --hash=sha256:{PIP_WHEEL_SHA256}" + path = shlex.quote(PIP_BOOTSTRAP_REQUIREMENTS) + return ( + f"printf '%s\\n' {shlex.quote(requirement)} > {path}" + f" && python -m pip install --require-hashes --no-deps -r {path}" + f" && rm {path}" + ) + + image = ( modal.Image.debian_slim(python_version=PYTHON_VERSION) - .apt_install("git", "curl", "time") - .pip_install(f"uv=={UV_VERSION}", f"huggingface_hub=={HF_HUB_VERSION}") + # Naming ca-certificates upgrades the base's CA store, which git's clone + # of Chronicle and the standard library's ssl module verify against, to + # Debian's current release, whose security fix distrusts emSign ECC Root + # CA - G3. + .apt_install("git", "curl", "time", "ca-certificates") + # Before the base's pip downloads anything else. + .run_commands(pip_bootstrap_command()) + # The upload runs huggingface_hub on the image's own Python. + .pip_install( + f"uv=={UV_VERSION}", + f"huggingface_hub=={HF_HUB_VERSION}", + f"certifi>={CERTIFI_MINIMUM_VERSION}", + ) + # The upgraded uv checks the Python it downloads against the sha256 it + # carries for that release. .run_commands(f"uv python install {PYTHON_VERSION}") .env( {