Register the _hmac_key fixture; guard the redirect so it fails locally
Some checks are pending
file-length / file length <= 250 lines (push) Waiting to run
pre-commit / pre-commit (push) Waiting to run
Tests / test (3.10) (push) Waiting to run
Tests / test (3.11) (push) Waiting to run
Tests / test (3.12) (push) Waiting to run
release-apk / release (APK) (push) Waiting to run

CI's Tests job went red on six signing assertions that pass on this machine.
Root cause predates this sweep: commit 1e201acbdc 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) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RA9ynjqXPnexRQnR5oAuCC
This commit is contained in:
Krzysztof Rudnicki 2026-08-15 21:19:35 +02:00
parent 499882b1de
commit 5125a98980
3 changed files with 31 additions and 4 deletions

View File

@ -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

View File

@ -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",
]

View File

@ -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"
)