mirror of
https://github.com/kuhyx/diet-guard.git
synced 2026-11-19 23:14:25 +01:00
pre-commit --all-files failed the file-length gate on AGENTS.md (262), test_sync_e2e.py (309) and test_sync_refresh.py (254), which would turn CI red. AGENTS.md prose is rewrapped (same words), test_sync_e2e.py moves its last three classes to test_sync_e2e_copy.py unchanged, and two docstrings in test_sync_refresh.py are tightened. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WzxdWWfb63jncj9Lca2jeX
193 lines
6.7 KiB
Python
193 lines
6.7 KiB
Python
"""End-to-end encryption of the sync payloads (``_sync_e2e``).
|
|
|
|
Runs the real sync tick over a dict-backed remote, so the header, the sealed
|
|
blobs and the keyed revisions are the bytes a real backend would hold.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import logging
|
|
from typing import TYPE_CHECKING
|
|
from unittest.mock import patch
|
|
|
|
from crdt_sync import (
|
|
E2eCipher,
|
|
E2eLayout,
|
|
seal_log_text,
|
|
)
|
|
import pytest
|
|
|
|
from diet_guard import _sync, _sync_client, _sync_e2e
|
|
from diet_guard._device import device_id
|
|
from diet_guard._state import load_log, log_meal
|
|
from diet_guard._state_sync import read_raw_log, write_raw_log
|
|
from diet_guard.sync_merge import daylog_to_log
|
|
from diet_guard.tests._sync_fakes import DictStore, _nutrition, _write_token
|
|
|
|
if TYPE_CHECKING:
|
|
from pathlib import Path
|
|
|
|
_PRIVATE_MEAL = "secret oatmeal"
|
|
_OLD = "diet-guard-sync/devices"
|
|
_NEW = "diet-guard-sync/devices-e2e"
|
|
|
|
|
|
def _run(store: DictStore) -> None:
|
|
_write_token()
|
|
with patch.object(_sync_client, "GitHubSyncClient", return_value=store):
|
|
_sync.run_sync()
|
|
|
|
|
|
def _peer_log(desc: str) -> str:
|
|
"""A plaintext food log a pre-E2E peer pushed, holding one meal."""
|
|
log_meal(desc, _nutrition(), slot=480)
|
|
log = daylog_to_log(read_raw_log())
|
|
write_raw_log({})
|
|
return json.dumps({rid: rec.to_dict() for rid, rec in log.items()})
|
|
|
|
|
|
def _all_meals() -> list[str]:
|
|
return [entry["desc"] for entries in load_log().values() for entry in entries]
|
|
|
|
|
|
class TestSealedPush:
|
|
def test_pushed_blobs_hold_no_plaintext(self) -> None:
|
|
log_meal(_PRIVATE_MEAL, _nutrition(), slot=480)
|
|
store = DictStore()
|
|
_run(store)
|
|
|
|
own = f"{_NEW}/{device_id()}"
|
|
log_blob = store.files[f"{own}/food_log.json"]
|
|
assert "secret" not in log_blob
|
|
assert '"fields"' not in log_blob
|
|
assert '"desc"' not in log_blob
|
|
# Record ids stay clear by design (kuhy's call: ids, tombstones and
|
|
# clocks are metadata). The food banks key records by food name, so
|
|
# only their *fields* are asserted sealed here.
|
|
for path, text in store.files.items():
|
|
assert "kcal" not in text, path
|
|
assert "protein" not in text, path
|
|
|
|
def test_nothing_is_written_to_the_old_plaintext_prefix(self) -> None:
|
|
log_meal(_PRIVATE_MEAL, _nutrition(), slot=480)
|
|
store = DictStore()
|
|
_run(store)
|
|
|
|
assert store.puts
|
|
for path in store.puts:
|
|
assert not path.startswith(f"{_OLD}/"), path
|
|
assert not path.startswith("diet-guard-sync/revs/"), path
|
|
assert f"diet-guard-sync/revs-e2e/{device_id()}" in store.files
|
|
assert "diet-guard-sync/e2e" in store.files
|
|
|
|
def test_another_device_with_the_passphrase_reads_it_back(self) -> None:
|
|
log_meal(_PRIVATE_MEAL, _nutrition(), slot=480)
|
|
store = DictStore()
|
|
_run(store)
|
|
|
|
reader = _sync_e2e.open_e2e(store)
|
|
text = reader.get_file_text(f"{_NEW}/{device_id()}/food_log.json")
|
|
assert text is not None
|
|
assert _PRIVATE_MEAL in text
|
|
|
|
|
|
class TestMigration:
|
|
def test_old_plaintext_peer_log_is_copied_and_merged(self) -> None:
|
|
peer = _peer_log("peer porridge")
|
|
store = DictStore({f"{_OLD}/phone/food_log.json": peer})
|
|
_run(store)
|
|
|
|
assert "peer porridge" in _all_meals()
|
|
# Copied under E2E, and the old node left exactly as it was.
|
|
assert "porridge" not in store.files[f"{_NEW}/phone/food_log.json"]
|
|
assert store.files[f"{_OLD}/phone/food_log.json"] == peer
|
|
|
|
def test_old_plaintext_export_keys_move_under_e2e(self) -> None:
|
|
key_b64 = "c2VjcmV0LWV4cG9ydC1rZXktYnl0ZXMtMzItbG9uZyE="
|
|
keyring = {
|
|
"kid1": {
|
|
"id": "kid1",
|
|
"fields": {"k": [key_b64, "2026-11-01T00:00:00.000Z-0000-phone"]},
|
|
"deleted": False,
|
|
}
|
|
}
|
|
old_path = f"{_OLD}/phone/export_keys.json"
|
|
store = DictStore({old_path: json.dumps(keyring)})
|
|
_run(store)
|
|
|
|
sealed = store.files[f"{_NEW}/phone/export_keys.json"]
|
|
assert key_b64 not in sealed
|
|
assert store.files[old_path] == json.dumps(keyring)
|
|
reader = _sync_e2e.open_e2e(store)
|
|
assert key_b64 in (reader.get_file_text(f"{_NEW}/phone/export_keys.json") or "")
|
|
|
|
|
|
class TestLocked:
|
|
def test_no_passphrase_fails_closed_without_any_write(self, tmp_path: Path) -> None:
|
|
log_meal(_PRIVATE_MEAL, _nutrition(), slot=480)
|
|
store = DictStore()
|
|
missing = tmp_path / "no-such-password"
|
|
with (
|
|
patch.object(_sync_e2e, "PASSWORD_FILE", missing),
|
|
pytest.raises(_sync.SyncError, match="locked"),
|
|
):
|
|
_run(store)
|
|
assert store.puts == []
|
|
|
|
def test_wrong_passphrase_fails_closed(self, tmp_path: Path) -> None:
|
|
store = DictStore()
|
|
# Another device creates the header under the test passphrase.
|
|
with patch.object(_sync_e2e, "E2E_STATE_DIR", tmp_path / "other-device"):
|
|
_sync_e2e.open_e2e(store)
|
|
store.puts.clear()
|
|
other = tmp_path / "other-password"
|
|
other.write_text("a different passphrase")
|
|
with (
|
|
patch.object(_sync_e2e, "PASSWORD_FILE", other),
|
|
pytest.raises(_sync.SyncError, match="locked"),
|
|
):
|
|
_run(store)
|
|
assert store.puts == []
|
|
|
|
def test_locked_pull_reports_a_reason_instead_of_raising(
|
|
self, tmp_path: Path
|
|
) -> None:
|
|
_write_token()
|
|
with (
|
|
patch.object(_sync_e2e, "PASSWORD_FILE", tmp_path / "none"),
|
|
patch.object(_sync_client, "GitHubSyncClient", return_value=DictStore()),
|
|
):
|
|
reason = _sync.pull_shared_log()
|
|
assert reason is not None
|
|
assert "locked" in reason
|
|
|
|
def test_unreadable_header_is_locked(self) -> None:
|
|
store = DictStore({"diet-guard-sync/e2e": "not a header"})
|
|
with pytest.raises(_sync.SyncError, match="locked"):
|
|
_run(store)
|
|
|
|
|
|
class TestUndecryptablePeer:
|
|
def test_peer_sealed_under_another_key_is_skipped(
|
|
self, caplog: pytest.LogCaptureFixture
|
|
) -> None:
|
|
log_meal("mine", _nutrition(), slot=480)
|
|
layout = E2eLayout.of(_OLD)
|
|
path = f"{_NEW}/stranger/food_log.json"
|
|
record = {
|
|
"id": "r1",
|
|
"fields": {"desc": ["x", "2026-11-01T00:00:00.000Z-0000-s"]},
|
|
}
|
|
alien = seal_log_text(
|
|
json.dumps({"r1": record}),
|
|
E2eCipher(b"\x01" * 32),
|
|
path=path,
|
|
)
|
|
store = DictStore({path: alien})
|
|
with caplog.at_level(logging.WARNING):
|
|
_run(store)
|
|
assert layout.prefix == _NEW
|
|
assert "skipping" in caplog.text
|
|
assert _all_meals() == ["mine"]
|