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.
Python agentaianthropicclaude-codecompressioncontext-engineeringcontext-windowcursorfastapilangchainllmmcpopenaiprompt-engineeringproxypythonragtoken-optimizationtokenstypescript
fix(binaries): fail closed on HEADROOM_BINARIES_ALLOW_UNVERIFIED=0/false (#3218)
Rajat Nagdada6d9736cd82
diff --git a/headroom/binaries.py b/headroom/binaries.pyindex 552b1638e..4b7021d9b 100644--- a/headroom/binaries.py+++ b/headroom/binaries.py@@ -19,8 +19,10 @@ Env vars:HEADROOM_BINARIES_CACHE override cache dirHEADROOM_BINARIES_OFFLINE if set, never reach the networkHEADROOM_BINARIES_ALLOW_UNVERIFIED- Escape hatch: if set, skip SHA-256 verification entirely and accept- whatever the mirror served. This disables Headroom's only defence+ Escape hatch: only 1, true, yes, or on (case-insensitive, with surrounding+ whitespace ignored) skip SHA-256 verification and accept whatever the+ mirror served. Unset, false, and unrecognized values retain verification.+ This disables Headroom's only defenceagainst a tampered or substituted binary, so it is announced onstderr every time it takes effect. Intended for developers testing alocally built asset or an off-registry version; never set it in CI or@@ -58,6 +60,19 @@ from headroom.offline import guard_egresslogger = logging.getLogger(__name__)+# Affirmative values for the sha256-verification bypass. This is a security+# control (supply-chain integrity), so it must fail CLOSED: only an explicit+# affirmative disables verification. A bare presence check treated+# `HEADROOM_BINARIES_ALLOW_UNVERIFIED=0` / `=false` — the natural way to say+# "keep verifying" — as truthy and silently skipped verification.+_TRUTHY = frozenset({"1", "true", "yes", "on"})+++def _allow_unverified_binaries() -> bool:+ """Whether sha256 verification of fetched binaries is bypassed."""+ return os.environ.get("HEADROOM_BINARIES_ALLOW_UNVERIFIED", "").strip().lower() in _TRUTHY++__all__ = ["BinaryError","BinaryFetchError",@@ -385,7 +400,7 @@ def _allow_unverified(name: str) -> bool:when ``lastResort`` will not fire, and when the log may be going somewherean operator is not watching, such as a file."""- if not os.environ.get("HEADROOM_BINARIES_ALLOW_UNVERIFIED"):+ if not _allow_unverified_binaries():return Falsemessage = (f"headroom: WARNING: accepting {name} WITHOUT sha256 verification "@@ -399,6 +414,7 @@ def _allow_unverified(name: str) -> bool:def _verify_sha256(path: Path, expected: str | None) -> None:+if _allow_unverified(path.name):returnif not expected:@@ -439,6 +455,7 @@ def verify_download_bytes(data: bytes, *, url: str, name: str) -> None:Sha256Mismatch if the bytes do not match it. SettingHEADROOM_BINARIES_ALLOW_UNVERIFIED=1 skips both checks."""+if _allow_unverified(name):returnexpected = sha256_for_url(url)diff --git a/tests/test_binaries.py b/tests/test_binaries.pyindex 05cb01b1e..ee301cbc5 100644--- a/tests/test_binaries.py+++ b/tests/test_binaries.py@@ -507,6 +507,50 @@ def test_status_reports_every_registered_tool(monkeypatch):assert r["state"] in ("on-path", "cached", "missing", "unsupported-platform")+# -------- HEADROOM_BINARIES_ALLOW_UNVERIFIED (security: fail closed) ------- #+++def _write(tmp_path, data: bytes):+ p = tmp_path / "asset.bin"+ p.write_bytes(data)+ return p++[email protected]("value", ["0", "false", "no", "off", "", " "])+def test_allow_unverified_non_affirmative_still_verifies(monkeypatch, tmp_path, value):+ # A security bypass must fail CLOSED: only an explicit affirmative disables+ # verification. Setting the var to "0"/"false" (the natural way to say "keep+ # verifying") previously passed a bare presence check and silently skipped+ # sha256 verification of the downloaded binary.+ monkeypatch.setenv("HEADROOM_BINARIES_ALLOW_UNVERIFIED", value)+ path = _write(tmp_path, b"payload")+ with pytest.raises(binaries.Sha256Mismatch):+ binaries._verify_sha256(path, "deadbeef" * 8) # wrong pin+++def test_allow_unverified_unset_still_verifies(monkeypatch, tmp_path):+ monkeypatch.delenv("HEADROOM_BINARIES_ALLOW_UNVERIFIED", raising=False)+ path = _write(tmp_path, b"payload")+ with pytest.raises(binaries.Sha256Mismatch):+ binaries._verify_sha256(path, "deadbeef" * 8)++[email protected]("value", ["1", "true", "TRUE", "yes", "on"])+def test_allow_unverified_affirmative_skips_verification(monkeypatch, tmp_path, value):+ monkeypatch.setenv("HEADROOM_BINARIES_ALLOW_UNVERIFIED", value)+ path = _write(tmp_path, b"payload")+ # Wrong pin, but an explicit affirmative bypass returns without raising.+ binaries._verify_sha256(path, "deadbeef" * 8)+++def test_verify_download_bytes_zero_still_verifies(monkeypatch):+ # Same fail-closed contract on the in-memory installer path.+ monkeypatch.setenv("HEADROOM_BINARIES_ALLOW_UNVERIFIED", "0")+ monkeypatch.setattr(binaries, "sha256_for_url", lambda _url: "deadbeef" * 8)+ with pytest.raises(binaries.Sha256Mismatch):+ binaries.verify_download_bytes(b"payload", url="https://x/y", name="y")++# -------- Registry key hygiene ------------------------------------------- #