From 5125a98980f187bdf972461e4708b4fce265adf4 Mon Sep 17 00:00:00 2001 From: Krzysztof kuhy Rudnicki Date: Sat, 15 Aug 2026 21:19:35 +0200 Subject: [PATCH] Register the _hmac_key fixture; guard the redirect so it fails locally CI's Tests job went red on six signing assertions that pass on this machine. Root cause predates this sweep: commit 1e201acbdce moved `_hmac_key` out of conftest into `_gate_fixtures` and added it to neither conftest's import list nor its `__all__`. A fixture only registers if conftest imports it, and ruff deletes an import nothing references -- which is exactly what the `__all__` below it exists to prevent for every other autouse fixture. So the fixture never ran, and this machine's real `/etc/workout-locker/hmac.key` silently stood in for the temp key. Locally the suite passed; on a keyless runner `compute_entry_hmac` returned None and every "entry carries a signature" assertion failed. Proven causal rather than assumed: with `/etc/workout-locker` masked by a tmpfs in a user namespace, HEAD fails 6 / passes 32 -- the same six CI named -- and with the fixture registered, 38 pass. `test_hmac_key_is_redirected_off_the_machine_key` now asserts the redirect directly, so an unregistered fixture is a local failure instead of a push-time one. Verified it fails closed by removing the import again. Verified: 708 Python tests / 100% branch coverage, and the same two files pass under a masked /etc/workout-locker. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01RA9ynjqXPnexRQnR5oAuCC --- app/pubspec.yaml | 2 +- diet_guard/tests/conftest.py | 5 ++--- diet_guard/tests/test_state_redirect.py | 28 +++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/app/pubspec.yaml b/app/pubspec.yaml index 23ee3f3..4d4e12e 100644 --- a/app/pubspec.yaml +++ b/app/pubspec.yaml @@ -2,7 +2,7 @@ name: diet_guard_app description: "Companion phone app for diet_guard: log meals on the go." publish_to: 'none' -version: 1.0.27+1 +version: 1.0.28+1 environment: sdk: ^3.12.2 diff --git a/diet_guard/tests/conftest.py b/diet_guard/tests/conftest.py index ae3fd3b..1ca0dd3 100644 --- a/diet_guard/tests/conftest.py +++ b/diet_guard/tests/conftest.py @@ -37,6 +37,7 @@ from diet_guard.tests._gate_fixtures import ( _block_real_tk, _block_real_vt, _hermetic_gatelock, + _hmac_key, dual_output, fake_tk, gate, @@ -81,11 +82,9 @@ __all__ = [ "_block_real_tk", "_block_real_vt", "_hermetic_gatelock", - "dual_output", + "_hmac_key", "dual_output", "fake_tk", - "fake_tk", - "gate", "gate", ] diff --git a/diet_guard/tests/test_state_redirect.py b/diet_guard/tests/test_state_redirect.py index 615edb4..958f146 100644 --- a/diet_guard/tests/test_state_redirect.py +++ b/diet_guard/tests/test_state_redirect.py @@ -20,6 +20,9 @@ from __future__ import annotations import ast import pathlib +from gatelock import log_integrity +from gatelock.log_integrity import compute_entry_hmac + from diet_guard import _budget, _budget_history, _state from diet_guard._constants import ( BUDGET_FILE as REAL_BUDGET_FILE, @@ -33,6 +36,9 @@ from diet_guard._constants import ( _PACKAGE_DIR = pathlib.Path(__file__).resolve().parent.parent +#: The real machine-wide key the fixture must redirect away from. +_REAL_HMAC_KEY = pathlib.Path("/etc/workout-locker/hmac.key") + #: Each on-disk state path conftest redirects, mapped to the single package #: module allowed to name it. Anything else naming one means the redirect no #: longer covers every reader. @@ -101,3 +107,25 @@ def test_redirect_moves_writes_off_the_live_paths() -> None: assert _budget_history.BUDGET_HISTORY_FILE.exists(), ( "budget-history write did not land on the redirect" ) + + +def test_hmac_key_is_redirected_off_the_machine_key() -> None: + """The shared HMAC key points at a temp file, not ``/etc``. + + ``_gate_fixtures._hmac_key`` is autouse, but a fixture only registers if + ``conftest.py`` *imports* it -- and ruff deletes an import that nothing + references, which is why every such fixture is also named in conftest's + ``__all__``. When ``_hmac_key`` was left out of both, this machine's real + ``/etc/workout-locker/hmac.key`` silently stood in for it: the suite passed + here and six signing tests failed on CI, which has no such file. + + Asserting the redirect directly makes that a local failure instead of a + push-time one. + """ + assert log_integrity.DEFAULT_HMAC_KEY_FILE != _REAL_HMAC_KEY, ( + "_hmac_key is not registered -- add it to conftest's import list AND " + "its __all__, or tests will pass here and fail on a keyless runner" + ) + assert compute_entry_hmac({"probe": True}) is not None, ( + "the redirected key does not sign; signing tests will fail closed" + )