Skip to content

Compress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.

fix(binaries): fail closed when a download carries no sha256 pin (#3724)

Tejas Chopra26a2c4939c40
diff --git a/headroom/binaries.py b/headroom/binaries.py
index 200257871..4916e8f22 100644
--- a/headroom/binaries.py
+++ b/headroom/binaries.py
@@ -9,10 +9,22 @@ Supported platforms: linux (glibc + musl) x86_64/aarch64, macOS x86_64/arm64,
Windows x86_64. Unsupported platforms raise PlatformNotSupported; callers in
the compression pipeline should fall back to their non-accelerated path.
+Every asset this package ships is pinned by SHA-256 in `tools.json`, and the
+tools-hash-refresh CI gate fails the build if a pin is missing or stale. So
+verification fails closed: a download whose URL carries no pin is refused
+rather than trusted on transport (HTTPS) alone.
+
Env vars:
HEADROOM_BINARIES_MIRROR base URL that replaces https://github.com
HEADROOM_BINARIES_CACHE override cache dir
HEADROOM_BINARIES_OFFLINE if set, never reach the network
+ HEADROOM_BINARIES_ALLOW_UNVERIFIED
+ Escape hatch: if set, skip SHA-256 verification entirely and accept
+ whatever the mirror served. This disables Headroom's only defence
+ against a tampered or substituted binary, so it is announced on
+ stderr every time it takes effect. Intended for developers testing a
+ locally built asset or an off-registry version; never set it in CI or
+ in production. Pin the asset in `tools.json` instead.
"""
from __future__ import annotations
@@ -47,6 +59,7 @@ __all__ = [
"BinaryFetchError",
"PlatformNotSupported",
"Sha256Mismatch",
+ "UnpinnedDownload",
"OfflineError",
"PlatformKey",
"detect_platform",
@@ -77,6 +90,18 @@ class Sha256Mismatch(BinaryError):
"""Raised when a downloaded asset's SHA256 does not match the pin."""
+class UnpinnedDownload(BinaryError):
+ """Raised when a downloaded asset has no SHA256 pin to verify it against.
+
+ Every shipped asset is pinned (tools-hash-refresh CI gate), so a missing
+ pin means an off-registry fetch — a version override, a hand-edited
+ registry, or a URL an installer built that the registry does not describe.
+ Accepting it would silently downgrade integrity to transport trust, so we
+ refuse. Pin the asset in `tools.json`, or set
+ HEADROOM_BINARIES_ALLOW_UNVERIFIED=1 to accept the risk explicitly.
+ """
+
+
class OfflineError(BinaryError):
"""Raised when a network fetch is required but HEADROOM_BINARIES_OFFLINE is set."""
@@ -310,19 +335,47 @@ def _sha256_file(path: Path) -> str:
return h.hexdigest()
+def _allow_unverified(name: str) -> bool:
+ """True if the escape hatch is set — announced loudly each time it fires.
+
+ Skipping verification turns a supply-chain guarantee into transport trust,
+ so it goes to stderr as well as the logger: the logger is frequently
+ unconfigured at proxy startup, and a silent downgrade is the failure mode
+ this whole module exists to avoid.
+
+ Exactly once, though. With no handlers configured, logging's ``lastResort``
+ already writes WARNING to stderr, so printing unconditionally produced the
+ same sentence twice. Print only when a handler exists -- that is precisely
+ when ``lastResort`` will not fire, and when the log may be going somewhere
+ an operator is not watching, such as a file.
+ """
+ if not os.environ.get("HEADROOM_BINARIES_ALLOW_UNVERIFIED"):
+ return False
+ message = (
+ f"headroom: WARNING: accepting {name} WITHOUT sha256 verification "
+ "(HEADROOM_BINARIES_ALLOW_UNVERIFIED is set). A tampered or "
+ "substituted binary will not be detected."
+ )
+ logger.warning("%s", message)
+ if logger.hasHandlers():
+ print(message, file=sys.stderr)
+ return True
+
+
def _verify_sha256(path: Path, expected: str | None) -> None:
- if os.environ.get("HEADROOM_BINARIES_ALLOW_UNVERIFIED"):
- logger.warning(
- "skipping sha256 verification for %s (HEADROOM_BINARIES_ALLOW_UNVERIFIED=1)",
- path.name,
- )
+ if _allow_unverified(path.name):
return
if not expected:
- # No pin in the registry (e.g. an off-registry version override). All
- # shipped assets ARE pinned — enforced by the tools-hash-refresh CI gate —
- # so a missing pin means an off-registry fetch; fall back to HTTPS trust.
- logger.info("binary %s downloaded without sha256 pin (HTTPS trust only)", path.name)
- return
+ # No pin in the registry. All shipped assets ARE pinned — enforced by
+ # the tools-hash-refresh CI gate — so a missing pin means an
+ # off-registry fetch. Fail closed: HTTPS proves only who served the
+ # bytes, not that they are the bytes we pinned.
+ path.unlink(missing_ok=True)
+ raise UnpinnedDownload(
+ f"refusing unpinned download {path.name}: no sha256 in the registry. "
+ "Pin it in headroom/tools.json (scripts/refresh_tool_hashes.py), or set "
+ "HEADROOM_BINARIES_ALLOW_UNVERIFIED=1 to accept it unverified."
+ )
got = _sha256_file(path)
if got.lower() != expected.lower():
path.unlink(missing_ok=True)
@@ -342,22 +395,26 @@ def sha256_for_url(url: str) -> str | None:
def verify_download_bytes(data: bytes, *, url: str, name: str) -> None:
"""Fail-closed integrity check for an in-memory downloaded archive.
- Used by installers (rtk, lean-ctx, codebase-memory-mcp) that download and
- extract on their own instead of going through the fetch path above. Verifies
- the bytes against the tools.json pin for ``url`` and refuses an unpinned URL
- unless HEADROOM_BINARIES_ALLOW_UNVERIFIED=1.
+ Used by installers (codebase-memory-mcp) that download and extract on their
+ own instead of going through the fetch path above. Verifies the bytes
+ against the tools.json pin for ``url``.
+
+ Raises UnpinnedDownload if ``url`` has no pin in the registry and
+ Sha256Mismatch if the bytes do not match it. Setting
+ HEADROOM_BINARIES_ALLOW_UNVERIFIED=1 skips both checks.
"""
- if os.environ.get("HEADROOM_BINARIES_ALLOW_UNVERIFIED"):
- logger.warning(
- "skipping sha256 verification for %s (HEADROOM_BINARIES_ALLOW_UNVERIFIED=1)", name
- )
+ if _allow_unverified(name):
return
expected = sha256_for_url(url)
if not expected:
- # Off-registry URL (e.g. a version override); shipped assets are all
- # pinned via the CI gate, so fall back to HTTPS trust here.
- logger.info("%s downloaded without sha256 pin (HTTPS trust only)", name)
- return
+ # Off-registry URL — a version override, or an installer building a
+ # filename the registry does not carry. Refuse rather than silently
+ # downgrading to transport trust.
+ raise UnpinnedDownload(
+ f"refusing unpinned download {name}: {url} has no sha256 in the registry. "
+ "Pin it in headroom/tools.json (scripts/refresh_tool_hashes.py), or set "
+ "HEADROOM_BINARIES_ALLOW_UNVERIFIED=1 to accept it unverified."
+ )
got = hashlib.sha256(data).hexdigest()
if got.lower() != expected.lower():
raise Sha256Mismatch(f"sha256 mismatch for {name}: expected {expected}, got {got}")
@@ -490,7 +547,8 @@ def resolve(tool: str) -> Path:
Raises PlatformNotSupported if the tool is unavailable on this platform,
OfflineError if a fetch is required but HEADROOM_BINARIES_OFFLINE is set,
- Sha256Mismatch if verification fails, BinaryFetchError on other IO errors.
+ Sha256Mismatch if verification fails, UnpinnedDownload if the asset carries
+ no registry pin, BinaryFetchError on other IO errors.
"""
on_path = _path_lookup(tool)
if on_path:
@@ -562,6 +620,9 @@ def ensure_tools(quiet: bool = False) -> dict[str, Path | None]:
OfflineError,
BinaryFetchError,
Sha256Mismatch,
+ # An unpinned asset is refused, not installed. Startup still
+ # degrades to "tool missing" rather than failing outright.
+ UnpinnedDownload,
# Catch readonly / sandboxed filesystems (e.g. containerized
# home dirs) so proxy startup never fails because the cache dir
# can't be created. The interceptor fall back to no-op.
diff --git a/headroom/cli/tools.py b/headroom/cli/tools.py
index 1cc0b8673..9f7e43822 100644
--- a/headroom/cli/tools.py
+++ b/headroom/cli/tools.py
@@ -52,7 +52,12 @@ def _exec_tool(tool: str, argv: Sequence[str]) -> None:
err=True,
)
sys.exit(2)
- except (binaries.Sha256Mismatch, binaries.BinaryFetchError) as e:
+ # BinaryError, not a list of subclasses: every failure here is a refusal to
+ # run an unverified tool, and enumerating them meant UnpinnedDownload -- added
+ # later -- escaped as a traceback instead of the `error: ...` + exit 2 every
+ # sibling produces. PlatformNotSupported and OfflineError are caught above for
+ # their specific hints; this is the catch-all for the rest.
+ except binaries.BinaryError as e:
click.secho(f"error: {e}", fg="red", err=True)
sys.exit(2)
@@ -232,7 +237,10 @@ def tools_install_cmd(tools: tuple[str, ...], force: bool) -> None:
except binaries.PlatformNotSupported as e:
click.secho(f"{name}: {e}", fg="red")
exit_code = 1
- except (binaries.BinaryFetchError, binaries.Sha256Mismatch, binaries.OfflineError) as e:
+ # Base class for the same reason as _exec_tool: an escaping subclass here
+ # also aborted the loop over the remaining tools and skipped the final
+ # sys.exit(exit_code), so one unpinned asset silently stopped the install.
+ except binaries.BinaryError as e:
click.secho(f"{name}: {e}", fg="red")
exit_code = 1
sys.exit(exit_code)
diff --git a/headroom/graph/installer.py b/headroom/graph/installer.py
index ef0b7b21a..07e2f220a 100644
--- a/headroom/graph/installer.py
+++ b/headroom/graph/installer.py
@@ -126,8 +126,18 @@ def ensure_cbm() -> Path | None:
if existing:
return existing
+ # Local import to match this module's lazy-import style (see download_cbm).
+ from headroom.binaries import UnpinnedDownload
+
try:
return download_cbm()
except RuntimeError as e:
logger.warning("Failed to install codebase-memory-mcp: %s", e)
return None
+ except UnpinnedDownload as e:
+ # Documented contract is "path, or None if the download failed", and a
+ # refusal is a failure to install -- the feature is simply unavailable.
+ # Deliberately NOT widened to BinaryError: Sha256Mismatch is a tamper
+ # signal and must keep propagating rather than becoming a quiet None.
+ logger.warning("Refusing to install codebase-memory-mcp: %s", e)
+ return None
diff --git a/tests/conftest.py b/tests/conftest.py
index be44bd330..e9db73f7e 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -164,10 +164,17 @@ def _null_binary_pins():
Installer tests fetch small mock archives, whose digests can't match the
real published pins. Nulling the pins lets those download/extract mechanics
- tests run (verification then falls back to HTTPS trust); the tests that
- specifically exercise verification set their own pin explicitly. Production
- keeps the real pins (this fixture is test-only) and the tools-hash-refresh
- CI gate guarantees they stay correct.
+ tests run; the tests that specifically exercise verification set their own
+ pin explicitly. Production keeps the real pins (this fixture is test-only)
+ and the tools-hash-refresh CI gate guarantees they stay correct.
+
+ Nulling a pin used to mean "fall back to HTTPS trust". It now means
+ "refuse", so the escape hatch has to be set alongside it or every test that
+ reaches a real download fails closed -- which is what happened to
+ test_bundled_tools_savings.py on a cold CI cache, while passing locally
+ against an already-populated one. Setting both together keeps this fixture
+ saying one thing: "verification is not what these tests are about."
+ Verification tests delenv it in their own fixture.
"""
try:
from headroom import binaries
@@ -185,9 +192,17 @@ def _null_binary_pins():
]
for asset, _original in saved:
asset["sha256"] = None
- yield
- for asset, original in saved:
- asset["sha256"] = original
+ previous = os.environ.get("HEADROOM_BINARIES_ALLOW_UNVERIFIED")
+ os.environ["HEADROOM_BINARIES_ALLOW_UNVERIFIED"] = "1"
+ try:
+ yield
+ finally:
+ if previous is None:
+ os.environ.pop("HEADROOM_BINARIES_ALLOW_UNVERIFIED", None)
+ else:
+ os.environ["HEADROOM_BINARIES_ALLOW_UNVERIFIED"] = previous
+ for asset, original in saved:
+ asset["sha256"] = original
@pytest.fixture(autouse=True)
diff --git a/tests/test_binaries.py b/tests/test_binaries.py
index 4d32a05fb..edb76197c 100644
--- a/tests/test_binaries.py
+++ b/tests/test_binaries.py
@@ -32,6 +32,10 @@ def _clear_caches(monkeypatch, tmp_path):
monkeypatch.setenv("HEADROOM_BINARIES_CACHE", str(tmp_path / "cache"))
monkeypatch.delenv("HEADROOM_BINARIES_MIRROR", raising=False)
monkeypatch.delenv("HEADROOM_BINARIES_OFFLINE", raising=False)
+ # Verification fails closed on a nulled (unpinned) asset, so mechanics
+ # tests opt into the escape hatch via the `allow_unverified` fixture; a
+ # developer's exported value must not leak in and mask the guard.
+ monkeypatch.delenv("HEADROOM_BINARIES_ALLOW_UNVERIFIED", raising=False)
yield
binaries.detect_platform.cache_clear()
binaries._registry.cache_clear()
@@ -81,6 +85,17 @@ class _FakeResponse:
return False
+def allow_unverified(monkeypatch):
+ """Opt out of SHA-256 verification for tests about fetch/extract mechanics.
+
+ The autouse fixture nulls the shipped pins so these tests can serve small
+ mock archives; without the escape hatch every such fetch is refused as
+ unpinned, which is the point of `test_unpinned_*` below.
+ """
+ monkeypatch.setenv("HEADROOM_BINARIES_ALLOW_UNVERIFIED", "1")
+
+
@pytest.fixture
def fake_urlopen(monkeypatch):
"""Install a fake urllib.request.urlopen that serves registered URLs."""
@@ -203,7 +218,9 @@ def test_mirror_substitution(monkeypatch):
assert binaries._mirror_url("https://example.com/x") == "https://example.com/x"
-def test_mirror_url_with_query_params_strips_them_from_download_filename(monkeypatch, fake_urlopen):
+def test_mirror_url_with_query_params_strips_them_from_download_filename(
+ monkeypatch, fake_urlopen, allow_unverified
+):
"""Mirror URLs with `?token=...` must not leak into the download filename.
Regression test for the case where `Path(url).name` gave
@@ -231,7 +248,7 @@ def test_mirror_url_with_query_params_strips_them_from_download_filename(monkeyp
asset["url"] = original_url
-def test_fetch_extract_and_cache_tar_gz(monkeypatch, fake_urlopen, tmp_path):
+def test_fetch_extract_and_cache_tar_gz(monkeypatch, fake_urlopen, tmp_path, allow_unverified):
_set_platform(monkeypatch, sys_plat="darwin", machine="arm64")
monkeypatch.setattr(binaries.shutil, "which", lambda _name: None)
@@ -249,7 +266,7 @@ def test_fetch_extract_and_cache_tar_gz(monkeypatch, fake_urlopen, tmp_path):
assert path2 == path
-def test_fetch_extract_zip(monkeypatch, fake_urlopen):
+def test_fetch_extract_zip(monkeypatch, fake_urlopen, allow_unverified):
_set_platform(monkeypatch, sys_plat="win32", machine="AMD64")
monkeypatch.setattr(binaries.shutil, "which", lambda _name: None)
payload = b"MZfake"
@@ -321,6 +338,97 @@ def test_sha256_match_passes(monkeypatch, fake_urlopen):
asset["sha256"] = None
+# -------- Unpinned downloads fail closed (A-7) --------------------------- #
+
+
+def test_unpinned_asset_is_refused_and_partial_deleted(monkeypatch, fake_urlopen):
+ """An asset with no registry pin must raise, not fall back to HTTPS trust.
+
+ The autouse fixture has already nulled every shipped pin, which is exactly
+ the shape of an off-registry version override.
+ """
+ _set_platform(monkeypatch, sys_plat="darwin", machine="arm64")
+ monkeypatch.setattr(binaries.shutil, "which", lambda _name: None)
+ asset = binaries._registry()["tools"]["difft"]["assets"]["darwin-aarch64"]
+ assert asset["sha256"] is None
+ fake_urlopen[asset["url"]] = _make_tar_gz({"difft": b"untrusted"})
+
+ # Caught as BinaryError so the assertion is about behaviour, not the name
+ # of the subclass; the type is pinned on the next line.
+ with pytest.raises(binaries.BinaryError) as exc:
+ binaries.resolve("difft")
+ assert type(exc.value) is binaries.UnpinnedDownload
+ assert "HEADROOM_BINARIES_ALLOW_UNVERIFIED" in str(exc.value)
+ # Nothing unverified is left in the cache for a later call to pick up.
+ # Version read from the registry, not hardcoded: a hardcoded one silently
+ # starts asserting about a path that never existed after a version bump.
+ version = binaries._registry()["tools"]["difft"]["version"]
+ assert not binaries._cached_path("difft", version, binaries.detect_platform()).exists()
+
+
+def test_unpinned_asset_allowed_by_escape_hatch_warns_on_stderr(
+ monkeypatch, fake_urlopen, capsys, allow_unverified
+):
+ """The escape hatch still installs, but says so where an operator sees it."""
+ _set_platform(monkeypatch, sys_plat="darwin", machine="arm64")
+ monkeypatch.setattr(binaries.shutil, "which", lambda _name: None)
+ asset = binaries._registry()["tools"]["difft"]["assets"]["darwin-aarch64"]
+ fake_urlopen[asset["url"]] = _make_tar_gz({"difft": b"untrusted"})
+
+ path = binaries.resolve("difft")
+ assert path.read_bytes() == b"untrusted"
+ err = capsys.readouterr().err
+ assert "WITHOUT sha256 verification" in err
+ assert "HEADROOM_BINARIES_ALLOW_UNVERIFIED" in err
+
+
+def test_verify_download_bytes_refuses_unpinned_url():
+ with pytest.raises(binaries.BinaryError) as exc:
+ binaries.verify_download_bytes(
+ b"payload", url="https://example.test/off-registry.tar.gz", name="some-tool"
+ )
+ assert type(exc.value) is binaries.UnpinnedDownload
+ assert "off-registry.tar.gz" in str(exc.value)
+
+
+def test_verify_download_bytes_escape_hatch_warns_on_stderr(capsys, allow_unverified):
+ binaries.verify_download_bytes(
+ b"payload", url="https://example.test/off-registry.tar.gz", name="some-tool"
+ )
+ assert "WITHOUT sha256 verification" in capsys.readouterr().err
+
+
+def test_verify_download_bytes_checks_a_pinned_url():
+ payload = b"payload"
+ asset = binaries._registry()["tools"]["difft"]["assets"]["darwin-aarch64"]
+ asset["sha256"] = hashlib.sha256(payload).hexdigest()
+ try:
+ binaries.verify_download_bytes(payload, url=asset["url"], name="difft")
+ with pytest.raises(binaries.Sha256Mismatch):
+ binaries.verify_download_bytes(b"tampered", url=asset["url"], name="difft")
+ finally:
+ asset["sha256"] = None
+
+
+def test_every_shipped_asset_is_pinned():
+ """The fail-closed guard is only safe because the registry pins everything.
+
+ Enforced against the published assets by the tools-hash-refresh workflow;
+ checked here so a new unpinned entry fails fast in unit tests too. Reads
+ the registry from disk because the autouse fixture nulls the live pins.
+ """
+ import json
+
+ registry = json.loads(binaries._REGISTRY_PATH.read_text(encoding="utf-8"))
+ unpinned = [
+ f"{tool}/{key}"
+ for tool, entry in registry.get("tools", {}).items()
+ for key, asset in entry.get("assets", {}).items()
+ if not asset.get("sha256")
+ ]
+ assert unpinned == []
+
+
# -------- status() ------------------------------------------------------- #
@@ -349,7 +457,7 @@ def test_ensure_tools_survives_readonly_cache_dir(monkeypatch, tmp_path):
readonly_parent.chmod(0o700) # so pytest tmp cleanup succeeds
-def test_ensure_tools_partial_failure_proxy_still_starts(monkeypatch):
+def test_ensure_tools_partial_failure_proxy_still_starts(monkeypatch, allow_unverified):
"""If one tool fails to fetch, others still install and ensure_tools returns."""
_set_platform(monkeypatch, sys_plat="darwin", machine="arm64")
monkeypatch.setattr(binaries.shutil, "which", lambda _name: None)
diff --git a/tests/test_binaries_refusal_reaches_callers.py b/tests/test_binaries_refusal_reaches_callers.py
new file mode 100644
index 000000000..031769f60
--- /dev/null
+++ b/tests/test_binaries_refusal_reaches_callers.py
@@ -0,0 +1,113 @@
+"""A refusal is only useful if the caller turns it into a message.
+
+`UnpinnedDownload` was added as a new `BinaryError` subclass, but three callers
+caught a hand-written tuple of subclasses rather than the base, so the new one
+escaped as a traceback. These pin the contract at the boundaries a person or a
+calling feature actually sees.
+"""
+
+from __future__ import annotations
+
+import logging
+
+import pytest
+from click.testing import CliRunner
+
+from headroom import binaries
+
+
+def _refuse(monkeypatch: pytest.MonkeyPatch):
+ """Make `resolve` refuse the way an off-registry asset does."""
+
+ def _raise(tool: str):
+ raise binaries.UnpinnedDownload(
+ f"refusing unpinned download {tool}: no sha256 in the registry. "
+ "Pin it, or set HEADROOM_BINARIES_ALLOW_UNVERIFIED=1."
+ )
+
+ monkeypatch.setattr(binaries, "resolve", _raise)
+ monkeypatch.delenv("HEADROOM_BINARIES_ALLOW_UNVERIFIED", raising=False)
+
+
+def test_tools_run_reports_a_refusal_instead_of_a_traceback(_refuse) -> None:
+ """`headroom tools <name>` must exit 2 with `error: ...`, like every sibling."""
+ from headroom.cli.tools import diff_cmd
+
+ result = CliRunner().invoke(diff_cmd, ["--version"])
+
+ assert result.exit_code == 2, result.output
+ assert not isinstance(result.exception, binaries.UnpinnedDownload), (
+ "the refusal escaped the CLI as a traceback"
+ )
+ assert "refusing unpinned download" in result.output
+
+
+def test_tools_install_keeps_going_and_still_sets_an_exit_code(_refuse) -> None:
+ """A refusal on one tool must not abort the loop over the others.
+
+ The escape skipped the final `sys.exit(exit_code)` entirely, so the command
+ reported success-by-omission for every tool after the first refusal.
+ """
+ from headroom.cli.tools import tools_group
+
+ result = CliRunner().invoke(tools_group, ["install"])
+
+ assert not isinstance(result.exception, binaries.UnpinnedDownload), (
+ "the refusal aborted `tools install`"
+ )
+ assert result.exit_code == 1, result.output
+
+
+def test_ensure_cbm_honours_its_documented_none_contract(monkeypatch) -> None:
+ """`ensure_cbm` promises "path, or None if the download failed"."""
+ from headroom.graph import installer
+
+ monkeypatch.setattr(installer, "get_cbm_path", lambda: None)
+
+ def _raise():
+ raise binaries.UnpinnedDownload("refusing unpinned download codebase-memory-mcp")
+
+ monkeypatch.setattr(installer, "download_cbm", _raise)
+
+ assert installer.ensure_cbm() is None
+
+
+def test_a_tamper_signal_still_propagates_from_ensure_cbm(monkeypatch) -> None:
+ """The None contract must not swallow a sha256 mismatch.
+
+ An unpinned asset is a misconfiguration; a mismatch is evidence of tampering
+ and has to stay loud rather than becoming a quiet "feature unavailable".
+ """
+ from headroom.graph import installer
+
+ monkeypatch.setattr(installer, "get_cbm_path", lambda: None)
+
+ def _raise():
+ raise binaries.Sha256Mismatch("sha256 mismatch for codebase-memory-mcp")
+
+ monkeypatch.setattr(installer, "download_cbm", _raise)
+
+ with pytest.raises(binaries.Sha256Mismatch):
+ installer.ensure_cbm()
+
+
+def test_the_escape_hatch_warning_is_not_printed_twice(monkeypatch, capsys) -> None:
+ """With no handlers configured, logging's lastResort already writes stderr.
+
+ Printing unconditionally as well produced the same sentence twice.
+ """
+ monkeypatch.setenv("HEADROOM_BINARIES_ALLOW_UNVERIFIED", "1")
+
+ logger = logging.getLogger("headroom.binaries")
+ saved, logger.handlers = logger.handlers, []
+ saved_propagate, logger.propagate = logger.propagate, False
+ try:
+ assert binaries._allow_unverified("difft") is True
+ finally:
+ logger.handlers, logger.propagate = saved, saved_propagate
+
+ stderr = capsys.readouterr().err
+ assert stderr.count("WITHOUT sha256 verification") <= 1, (
+ f"the warning was emitted more than once:\n{stderr}"
+ )
diff --git a/tests/test_graph.py b/tests/test_graph.py
index 4eca7c4f5..447b2d431 100644
--- a/tests/test_graph.py
+++ b/tests/test_graph.py
@@ -76,6 +76,9 @@ def test_get_cbm_path_prefers_path_then_install_dir(monkeypatch, tmp_path: Path)
def test_download_cbm_success_and_verification_paths(monkeypatch, tmp_path: Path) -> None:
+ # v1.2.3 is not in the registry and the mock archive would not match a pin
+ # anyway; this test is about the install mechanics, not verification.
+ monkeypatch.setenv("HEADROOM_BINARIES_ALLOW_UNVERIFIED", "1")
monkeypatch.setattr(installer, "CBM_BIN_DIR", tmp_path)
monkeypatch.setattr(installer, "_detect_platform", lambda: "linux-amd64")
monkeypatch.setattr(
@@ -109,6 +112,9 @@ def test_download_cbm_success_and_verification_paths(monkeypatch, tmp_path: Path
def test_download_cbm_invalid_url_download_failure_and_extract_errors(
monkeypatch, tmp_path: Path
) -> None:
+ # These failure modes are reached with an overridden (off-registry) release
+ # host, so verification would refuse them before the code under test runs.
+ monkeypatch.setenv("HEADROOM_BINARIES_ALLOW_UNVERIFIED", "1")
monkeypatch.setattr(installer, "CBM_BIN_DIR", tmp_path)
monkeypatch.setattr(installer, "_detect_platform", lambda: "linux-amd64")
@@ -138,6 +144,27 @@ def test_download_cbm_invalid_url_download_failure_and_extract_errors(
installer.download_cbm()
+def test_download_cbm_refuses_an_off_registry_version(monkeypatch, tmp_path: Path) -> None:
+ """A version override points at a URL with no pin — refuse it (A-7).
+
+ The default CBM_VERSION is pinned for every platform in tools.json, so only
+ an override reaches this path.
+ """
+ from headroom import binaries
+
+ monkeypatch.delenv("HEADROOM_BINARIES_ALLOW_UNVERIFIED", raising=False)
+ monkeypatch.setattr(installer, "CBM_BIN_DIR", tmp_path)
+ monkeypatch.setattr(installer, "_detect_platform", lambda: "linux-amd64")
+ monkeypatch.setattr(
+ installer, "urlopen", lambda url, timeout=60: FakeResponse(_build_archive())
+ )
+
+ with pytest.raises(binaries.BinaryError) as exc:
+ installer.download_cbm(version="v9.9.9")
+ assert type(exc.value) is binaries.UnpinnedDownload
+ assert not (tmp_path / installer.CBM_BIN_NAME).exists()
+
+
def test_ensure_cbm_uses_existing_or_returns_none_on_failure(monkeypatch, tmp_path: Path) -> None:
existing = tmp_path / installer.CBM_BIN_NAME
monkeypatch.setattr(installer, "get_cbm_path", lambda: existing)