E2E-encrypt the published alarm record
Some checks failed
dependency freshness / dependencies on newest stable (push) Has been cancelled
md-naming / markdown naming convention (push) Has been cancelled
pre-commit / pre-commit (${{ matrix.group }}) (mypy, mypy, ) (push) Has been cancelled
pre-commit / pre-commit (${{ matrix.group }}) (pylint, pylint, ) (push) Has been cancelled
pre-commit / pre-commit (${{ matrix.group }}) (rest, , mypy,pylint,free-days-interop) (push) Has been cancelled
Tests / test (push) Has been cancelled
Tests / free-days-interop (push) Has been cancelled
release-apk / release (APK) (push) Has been cancelled
repo-contract / repo contract (push) Has been cancelled

The phone pushes alarm.json sealed under wake-alarm-sync/devices-e2e
(Argon2id key from the shared crdt-sync passphrase, keystore + lock
icon in Settings). The PC reads it through open_encrypted_client on
both Firebase and the GitHub mirror (now via GitHubSyncClient); no or
wrong passphrase reads as nothing published, never plaintext. The old
plaintext prefix is copied once per process, never deleted.

crdt-sync 0.11.0, crdt_sync_dart 0.14.1, crdt_sync_flutter 0.8.1,
sync_settings_ui 0.8.2, freedays 0.4.2, sickdays 0.5.3. install.sh
now fails if crdt_sync/pynacl/argon2 do not import.

Todo: fda71ed1

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Krzysztof Rudnicki 2026-11-12 10:21:50 +01:00
parent 9dbc4547ee
commit 9250db4cc1
19 changed files with 725 additions and 276 deletions

View File

@ -107,9 +107,9 @@ repos:
# the manifest becomes an unresolvable hook environment rather
# than merely an old one.
- gatelock @ git+https://github.com/kuhyx/utils@gatelock-v0.12.0#subdirectory=gatelock
- crdt-sync @ git+https://github.com/kuhyx/utils@crdt-sync-v0.9.2#subdirectory=crdt-sync
- freedays @ git+https://github.com/kuhyx/utils@freedays-v0.4.0#subdirectory=freedays
- sickdays @ git+https://github.com/kuhyx/utils@sickdays-v0.5.0#subdirectory=sickdays
- crdt-sync @ git+https://github.com/kuhyx/utils@crdt-sync-v0.11.0#subdirectory=crdt-sync
- freedays @ git+https://github.com/kuhyx/utils@freedays-v0.4.2#subdirectory=freedays
- sickdays @ git+https://github.com/kuhyx/utils@sickdays-v0.5.3#subdirectory=sickdays
# Test suites intentionally use patterns (protected-access, magic-value
# comparisons) that don't belong in the source-code 10/10 gate.
exclude: ^(\.venv/)|(^|/)(tests/|conftest\.py)

View File

@ -100,9 +100,16 @@ systemd-sleep hook: a suspended or just-booted machine still fires.
`_sync.py` is the only reader.
- Source of truth: a `crdt_sync` log written by `phone_app/` under
`wake-alarm-sync/devices/<device-id>/alarm.json`, at
`wake-alarm-sync/devices-e2e/<device-id>/alarm.json`, at
`.alarm.fields.alarm_hour[0]` / `.alarm_minute[0]`. Same logical path on both
backends, so only the transport differs, never the parse.
- **End-to-end encrypted** (since 2026-11-12): fields are sealed with a key
derived from the shared passphrase (`~/.config/crdt-sync/password` on the
PC, the keystore + lock icon in Settings on the phone). Both readers go
through `open_encrypted_client`, which copies the old plaintext
`wake-alarm-sync/devices` over once per process and never deletes it. No
or wrong passphrase reads as "nothing published" (cache, then 08:00) —
never plaintext. diet_guard reads the same prefix for weigh-ins.
- **The Firebase reader discovers device directories; it is not pinned to
`devices/phone/`.** The phone's device id is migrating from the fixed
constant `phone` to a per-install uuid, and a pinned reader would silently
@ -128,9 +135,9 @@ systemd-sleep hook: a suspended or just-booted machine still fires.
**404, not 403**.
- `crdt_sync` is imported at module scope but **guarded by
`try/except ImportError`**: the service runs `/usr/bin/python`, not a venv, so
a dependency that failed to install must degrade the read to the GitHub path
rather than crash the per-minute trigger. The GitHub path stays stdlib-only
for that reason. Verify with `/usr/bin/python3 -c "import crdt_sync"`, not the
a dependency that failed to install must degrade the read to the cache
rather than crash the per-minute trigger. Both backends need it now (the
GitHub mirror is read through `GitHubSyncClient`, encrypted too). Verify with `/usr/bin/python3 -c "import crdt_sync"`, not the
dev venv.
- Resolution chain in `get_alarm()`: fresh cache (< 5 min) → live fetch → stale
cache → 08:00. The cache bounds remote reads to ~288/day

View File

@ -132,6 +132,10 @@ echo "[1/4] Installing wake_alarm for /usr/bin/python..."
echo " Installed. Verifying import..."
/usr/bin/python3 -c "import wake_alarm; import gatelock"
echo " wake_alarm and gatelock import under /usr/bin/python."
# The alarm is end-to-end encrypted: without crdt_sync (pynacl, argon2-cffi)
# the trigger can only fall back to the cache and 08:00. Fail the install.
/usr/bin/python3 -c "import crdt_sync, nacl, argon2"
echo " crdt_sync (E2E) imports under /usr/bin/python."
echo "[2/4] Installing systemd user units..."
mkdir -p "$SYSTEMD_USER_DIR"

View File

@ -1,5 +1,7 @@
import 'dart:developer' as developer;
import 'package:crdt_sync_flutter/crdt_sync_flutter.dart'
show E2ePassphraseButton;
import 'package:material_ui/material_ui.dart';
import 'package:sync_settings_ui/sync_settings_ui.dart';
@ -7,6 +9,7 @@ import 'package:wake_alarm_sync/screens/github_mirror_screen.dart';
import 'package:wake_alarm_sync/screens/home_location_screen.dart';
import 'package:wake_alarm_sync/screens/room_proof_screen.dart';
import 'package:wake_alarm_sync/screens/session_tags_screen.dart';
import 'package:wake_alarm_sync/services/alarm_sync_e2e.dart';
import 'package:wake_alarm_sync/services/alarm_sync_service.dart';
import 'package:wake_alarm_sync/services/data_transfer.dart';
import 'package:wake_alarm_sync/services/firebase_backend.dart';
@ -79,6 +82,15 @@ class SettingsScreen extends StatelessWidget {
return Scaffold(
appBar: AppBar(
actions: [
// End-to-end encryption passphrase: a closed lock while the last
// sync found no usable key, so nothing reaches the PC until set.
ValueListenableBuilder<bool>(
valueListenable: alarmSyncLocked,
builder: (_, locked, _) => E2ePassphraseButton(
key: const Key('settings-e2e'),
locked: locked,
),
),
IconButton(
key: const Key('settings-export'),
icon: const Icon(Icons.upload_file, semanticLabel: 'Export data'),

View File

@ -0,0 +1,59 @@
/// End-to-end encryption of the alarm record this app publishes.
///
/// The record (alarm time, tags, home location, morning sessions, weigh-ins,
/// enforcement fields) is sealed with a key derived from the shared
/// crdt-sync passphrase in the OS keystore, under
/// `wake-alarm-sync/devices-e2e`; Firebase and the GitHub mirror only see
/// the record id and clocks. The PC (`wake_alarm/_sync_transport.py`) and
/// diet_guard (its weigh-in ingest) read the same layout.
///
/// Fail closed: without a usable key nothing is pulled or pushed. The old
/// plaintext prefix is copied over on open and never written or deleted.
library;
import 'package:crdt_sync/crdt_sync.dart';
import 'package:crdt_sync_flutter/crdt_sync_flutter.dart'
show openEncryptedStoreFromKeystore;
import 'package:flutter/foundation.dart';
/// The plaintext prefix every pre-E2E build wrote; only the copy reads it.
const legacyAlarmPrefix = 'wake-alarm-sync/devices';
/// Where the alarm record now lives, and where its revisions go.
final E2eLayout alarmLayout = E2eLayout.of(legacyAlarmPrefix);
/// Argon2id cost for a header this device creates; only tests lower it.
@visibleForTesting
Argon2Params e2eArgon2Params = Argon2Params.recommended;
/// Whether the last open found no usable key, for the lock button.
final ValueNotifier<bool> alarmSyncLocked = ValueNotifier<bool>(false);
/// Thrown instead of syncing when end-to-end encryption has no usable key.
class SyncLockedException implements Exception {
/// Creates the exception for [reason].
const SyncLockedException(this.reason);
/// Why the store is locked.
final E2eLockReason reason;
@override
String toString() => 'sync locked (${reason.name})';
}
/// The encrypted view of [backend], the outermost store (above the mirror).
///
/// Throws [SyncLockedException] when locked, so no caller can fall through
/// to a plaintext push.
Future<RemoteStore> openAlarmStore(RemoteStore backend) async {
final state = await openEncryptedStoreFromKeystore(
store: backend,
oldPrefix: legacyAlarmPrefix,
params: e2eArgon2Params,
);
alarmSyncLocked.value = state is! E2eReady;
if (state is E2eReady) return state.store;
throw SyncLockedException(
state is E2eLocked ? state.reason : E2eLockReason.badHeader,
);
}

View File

@ -7,6 +7,7 @@ import 'package:path/path.dart' as p;
import 'package:path_provider/path_provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:uuid/uuid.dart';
import 'package:wake_alarm_sync/services/alarm_sync_e2e.dart';
import 'package:wake_alarm_sync/services/firebase_backend.dart';
/// The alarm time this device holds, as read from or written to the store.
@ -17,7 +18,8 @@ typedef Alarm = ({int hour, int minute});
///
/// The phone is the **sole writer**: it stores the chosen alarm locally in a
/// [LogStore] (one HLC-stamped [Record], id [_recordId]) and pushes that log
/// to `wake-alarm-sync/devices/<deviceId>/alarm.json` — Firebase first,
/// to `wake-alarm-sync/devices-e2e/<deviceId>/alarm.json`, end-to-end
/// encrypted (`alarm_sync_e2e.dart`) — Firebase first,
/// mirrored to the private `kuhyx/syncs` repo during the cutover (see
/// [syncBackend]). The path is deliberately identical on both backends, so
/// only the transport differs.
@ -162,11 +164,17 @@ class AlarmSyncService {
/// The pull is what brings the tags and home location back to a fresh
/// install. The alarm time itself is unaffected: [setLocal] runs first on
/// every publish, so the local time is always the newest and wins.
///
/// Everything goes through the end-to-end encrypted view of [client]
/// (`alarm_sync_e2e.dart`); a locked device throws [SyncLockedException]
/// before reading or writing anything.
Future<void> pushWith(RemoteStore client) async {
final sealed = await openAlarmStore(client);
final merged = await syncLog(
client: client,
client: sealed,
deviceId: deviceId,
pathPrefix: 'wake-alarm-sync/devices',
pathPrefix: alarmLayout.prefix,
revsPath: alarmLayout.revsPath,
filename: 'alarm.json',
localLog: _store.snapshot(),
encode: logToJson,

View File

@ -101,11 +101,20 @@ packages:
dependency: "direct main"
description:
path: crdt_sync_dart
ref: "crdt_sync_dart-v0.13.1"
resolved-ref: eebeb0b51053f160f163140141aba10a447ba1e8
ref: "crdt_sync_dart-v0.14.1"
resolved-ref: edd0f7a7882cb1d3904932ba94faa282d08165df
url: "https://github.com/kuhyx/utils"
source: git
version: "0.13.1"
version: "0.14.1"
crdt_sync_flutter:
dependency: "direct main"
description:
path: crdt_sync_flutter
ref: "crdt_sync_flutter-v0.8.1"
resolved-ref: "3225af8365481677d61a8cabec0c0ad4d0407567"
url: "https://github.com/kuhyx/utils"
source: git
version: "0.8.1"
cross_file:
dependency: transitive
description:
@ -778,11 +787,11 @@ packages:
dependency: "direct main"
description:
path: sync_settings_ui
ref: "sync_settings_ui-v0.8.0"
resolved-ref: "0ef5461805e8f574de5174c940775e5245d675b3"
ref: "sync_settings_ui-v0.8.2"
resolved-ref: "3225af8365481677d61a8cabec0c0ad4d0407567"
url: "https://github.com/kuhyx/utils"
source: git
version: "0.8.0"
version: "0.8.2"
term_glyph:
dependency: transitive
description:

View File

@ -12,8 +12,14 @@ dependencies:
crdt_sync:
git:
url: https://github.com/kuhyx/utils
ref: crdt_sync_dart-v0.13.1
ref: crdt_sync_dart-v0.14.1
path: crdt_sync_dart
# Keystore-backed end-to-end encryption open + the passphrase lock button.
crdt_sync_flutter:
git:
url: https://github.com/kuhyx/utils
ref: crdt_sync_flutter-v0.8.1
path: crdt_sync_flutter
cupertino_icons: 2.0.0
# Shared JSON export/import plumbing (Save-as, picker, envelope).
@ -50,7 +56,7 @@ dependencies:
sync_settings_ui:
git:
url: https://github.com/kuhyx/utils
ref: sync_settings_ui-v0.8.0
ref: sync_settings_ui-v0.8.2
path: sync_settings_ui
url_launcher: 6.3.3
uuid: 4.6.0

View File

@ -0,0 +1,92 @@
/// Shared end-to-end encryption plumbing for the sync tests.
library;
import 'dart:convert';
import 'package:crdt_sync/crdt_sync.dart';
import 'package:http/http.dart' as http;
import 'package:http/testing.dart' as http_testing;
import 'package:wake_alarm_sync/services/alarm_sync_e2e.dart';
import 'fake_secure_storage.dart';
/// The passphrase every test device shares.
const testPassphrase = 'test-passphrase';
/// A keystore holding [testPassphrase], cheap Argon2id, a fresh copy memo.
void installE2eTestStorage({Map<String, String>? initial}) {
e2eArgon2Params = const Argon2Params(
memoryKiB: 8,
iterations: 1,
parallelism: 1,
);
resetE2eCopyMemo();
installFakeSecureStorage(
initial: {'crdt_sync_e2e_passphrase': testPassphrase, ...?initial},
);
}
/// A GitHub Contents API stand-in that stores what is PUT and lists
/// directories from it, so the encryption header reads back.
class MemGitHub {
/// Starts holding [files] (repo path -> text).
MemGitHub([Map<String, String>? files]) : files = {...?files};
/// Repo path -> text.
final Map<String, String> files;
/// Every PUT path, in order.
final List<String> putPaths = [];
/// The mock HTTP client.
late final http.Client httpClient = http_testing.MockClient(_handle);
/// A [GitHubClient] over [httpClient].
GitHubClient client() =>
GitHubClient(owner: 'o', repo: 'r', token: 't', httpClient: httpClient);
/// The decrypted text at [path], read like another device would.
Future<String?> opened(String path) async {
final state = await openEncryptedStore(
store: client(),
oldPrefix: legacyAlarmPrefix,
readPassphrase: () async => testPassphrase,
);
return await (state as E2eReady).store.getFileText(path);
}
Future<http.Response> _handle(http.Request request) async {
final path = request.url.path;
if (!path.contains('/contents/')) return http.Response('{}', 200);
final key = path.split('/contents/').last;
if (request.method == 'PUT') {
final body = jsonDecode(request.body) as Map<String, dynamic>;
files[key] = utf8.decode(base64.decode(body['content'] as String));
putPaths.add(key);
return http.Response('{}', 200);
}
final text = files[key];
if (text != null) {
return http.Response(
jsonEncode({'content': base64.encode(utf8.encode(text)), 'sha': 's'}),
200,
);
}
final names = {
for (final k in files.keys)
if (k.startsWith('$key/')) k.substring(key.length + 1).split('/')[0],
};
if (names.isEmpty) return http.Response('{}', 404);
return http.Response(
jsonEncode([
for (final n in names)
{
'type': files.containsKey('$key/$n') ? 'file' : 'dir',
'name': n,
'path': '$key/$n',
},
]),
200,
);
}
}

View File

@ -2,13 +2,14 @@ import 'dart:convert';
import 'package:crdt_sync/crdt_sync.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:http/http.dart' as http;
import 'package:http/testing.dart' as http_testing;
import 'package:wake_alarm_sync/services/alarm_sync_service.dart';
import '../e2e_support.dart';
const _deviceId = 'this-install';
const _peerId = 'previous-install';
const _prefix = 'wake-alarm-sync/devices';
const _old = 'wake-alarm-sync/devices';
const _prefix = 'wake-alarm-sync/devices-e2e';
class _MemPersistence implements LogPersistence {
String? text;
@ -20,45 +21,6 @@ class _MemPersistence implements LogPersistence {
Future<void> write(String value) async => text = value;
}
http.Response _json(Object body, [int status = 200]) =>
http.Response(jsonEncode(body), status);
/// Mock GitHub Contents API with one peer directory holding [peerLog].
///
/// The directory GET answers the listing `syncLog` walks, the peer's file GET
/// answers the pull, and every PUT is recorded so the merged push can be
/// inspected.
({http.Client client, List<Map<String, dynamic>> pushes}) _mockRemote({
Map<String, dynamic>? peerLog,
}) {
final pushes = <Map<String, dynamic>>[];
final client = http_testing.MockClient((request) async {
final path = request.url.path;
if (!path.contains('/contents/')) return _json({});
final key = path.split('/contents/').last;
if (request.method == 'PUT') {
final body = jsonDecode(request.body) as Map<String, dynamic>;
final text = utf8.decode(base64.decode(body['content'] as String));
pushes.add(jsonDecode(text) as Map<String, dynamic>);
return _json({});
}
if (key == _prefix) {
return _json([
if (peerLog != null) {'name': _peerId, 'type': 'dir'},
{'name': _deviceId, 'type': 'dir'},
]);
}
if (key == '$_prefix/$_peerId/alarm.json' && peerLog != null) {
return _json({
'content': base64.encode(utf8.encode(jsonEncode(peerLog))),
'encoding': 'base64',
});
}
return _json({}, 404);
});
return (client: client, pushes: pushes);
}
Map<String, dynamic> _peerRecord(Map<String, Object?> fields, String hlc) => {
'alarm': {
'id': 'alarm',
@ -74,6 +36,8 @@ void main() {
late AlarmSyncService service;
setUp(() async {
TestWidgetsFlutterBinding.ensureInitialized();
installE2eTestStorage();
service = await AlarmSyncService.open(
persistence: _MemPersistence(),
deviceId: _deviceId,
@ -101,20 +65,18 @@ void main() {
test('pushWith pulls a previous install and takes its newer field', () async {
await service.setLocal(8, 0);
final (:client, :pushes) = _mockRemote(
peerLog: _peerRecord({
'alarm_hour': 7,
'alarm_minute': 0,
'nfc_tag_ids': ['x', 'y'],
}, '2026-09-11T10:26:18.254Z-0000-$_peerId'),
);
final github = GitHubClient(
owner: 'o',
repo: 'r',
token: 't',
httpClient: client,
);
// The previous install never updated: its record sits in plaintext on
// the old prefix and reaches this one through the one-time E2E copy.
final remote = MemGitHub({
'$_old/$_peerId/alarm.json': jsonEncode(
_peerRecord({
'alarm_hour': 7,
'alarm_minute': 0,
'nfc_tag_ids': ['x', 'y'],
}, '2026-09-11T10:26:18.254Z-0000-$_peerId'),
),
});
final github = remote.client();
await service.pushWith(github);
// The peer's tags are merged in; the peer's older alarm loses to the
@ -122,25 +84,23 @@ void main() {
expect(service.field('nfc_tag_ids')?.$1, ['x', 'y']);
expect(service.currentAlarm, (hour: 8, minute: 0));
// And the merged result is what was pushed, under this install's path.
expect(pushes, hasLength(1));
final fields = (pushes.single['alarm'] as Map)['fields'] as Map;
const path = '$_prefix/$_deviceId/alarm.json';
expect(remote.files[path], isNot(contains('nfc_tag_ids')));
final pushed = jsonDecode((await remote.opened(path))!) as Map;
final fields = (pushed['alarm'] as Map)['fields'] as Map;
expect((fields['nfc_tag_ids'] as List).first, ['x', 'y']);
expect((fields['alarm_hour'] as List).first, 8);
// The old plaintext node is copied, never rewritten.
expect(remote.putPaths.where((p) => p.startsWith('$_old/')), isEmpty);
});
test('pushWith with no peers pushes the local log unchanged', () async {
await service.setLocal(8, 0);
final (:client, :pushes) = _mockRemote();
final github = GitHubClient(
owner: 'o',
repo: 'r',
token: 't',
httpClient: client,
);
final remote = MemGitHub();
await service.pushWith(github);
await service.pushWith(remote.client());
expect(pushes, hasLength(1));
expect(remote.putPaths, contains('$_prefix/$_deviceId/alarm.json'));
expect(service.field('nfc_tag_ids'), isNull);
});
}

View File

@ -6,13 +6,13 @@ import 'package:http/http.dart' as http;
import 'package:http/testing.dart' as http_testing;
import 'package:shared_preferences/shared_preferences.dart';
import 'package:uuid/uuid.dart';
import 'package:wake_alarm_sync/services/alarm_sync_e2e.dart';
import 'package:wake_alarm_sync/services/alarm_sync_service.dart';
const _deviceId = 'test-device-uuid';
const _alarmPath = 'wake-alarm-sync/devices/$_deviceId/alarm.json';
import '../e2e_support.dart';
http.Response _response(int statusCode, [Object? jsonBody]) =>
http.Response(jsonEncode(jsonBody ?? {}), statusCode);
const _deviceId = 'test-device-uuid';
const _alarmPath = 'wake-alarm-sync/devices-e2e/$_deviceId/alarm.json';
/// In-memory persistence so the store never touches the real documents dir.
class _MemPersistence implements LogPersistence {
@ -25,39 +25,12 @@ class _MemPersistence implements LogPersistence {
Future<void> write(String value) async => text = value;
}
class _PutCall {
_PutCall(this.path, this.body);
final String path;
final Map<String, dynamic> body;
}
/// Mock GitHub Contents API: GET `.../contents/<key>` returns
/// [contentResponses]`[key]` (404 if absent), the bare repo-existence GET
/// always succeeds, and every PUT is recorded. Mirrors the workout_app test.
({http.Client httpClient, List<_PutCall> putCalls}) _mockGitHub({
Map<String, http.Response> contentResponses = const {},
}) {
final putCalls = <_PutCall>[];
final client = http_testing.MockClient((request) async {
final path = request.url.path;
if (!path.contains('/contents/')) return _response(200);
final key = path.split('/contents/').last;
if (request.method == 'PUT') {
putCalls.add(
_PutCall(key, jsonDecode(request.body) as Map<String, dynamic>),
);
return _response(200);
}
return contentResponses[key] ?? _response(404);
});
return (httpClient: client, putCalls: putCalls);
}
Map<String, dynamic> _decodePush(_PutCall call) =>
jsonDecode(utf8.decode(base64.decode(call.body['content'] as String)))
as Map<String, dynamic>;
void main() {
setUp(() {
TestWidgetsFlutterBinding.ensureInitialized();
installE2eTestStorage();
});
test('currentAlarm is null before anything is set', () async {
final service = await AlarmSyncService.open(
persistence: _MemPersistence(),
@ -89,13 +62,14 @@ void main() {
deviceId: _deviceId,
);
await service.setLocal(7, 15);
final (:httpClient, :putCalls) = _mockGitHub();
final remote = MemGitHub();
await service.push('tok', httpClient: httpClient);
await service.push('tok', httpClient: remote.httpClient);
expect(putCalls, hasLength(1));
expect(putCalls.single.path, _alarmPath);
final pushed = _decodePush(putCalls.single);
expect(remote.putPaths, contains(_alarmPath));
expect(remote.files[_alarmPath], isNot(contains('alarm_hour')));
final pushed =
jsonDecode((await remote.opened(_alarmPath))!) as Map<String, dynamic>;
final fields =
(pushed['alarm'] as Map<String, dynamic>)['fields']
as Map<String, dynamic>;
@ -103,6 +77,27 @@ void main() {
expect((fields['alarm_minute'] as List).first, 15);
});
test('a device with no passphrase pushes nothing', () async {
installE2eTestStorage(initial: {'crdt_sync_e2e_passphrase': ''});
final service = await AlarmSyncService.open(
persistence: _MemPersistence(),
deviceId: _deviceId,
);
await service.setLocal(7, 15);
final remote = MemGitHub();
await expectLater(
service.push('tok', httpClient: remote.httpClient),
throwsA(isA<SyncLockedException>()),
);
expect(remote.putPaths, isEmpty);
expect(alarmSyncLocked.value, isTrue);
expect(
const SyncLockedException(E2eLockReason.wrongKey).toString(),
contains('wrongKey'),
);
});
test('push is a network round-trip that swallows a server error', () async {
final service = await AlarmSyncService.open(
persistence: _MemPersistence(),
@ -161,19 +156,18 @@ void main() {
deviceId: 'uuid-abc',
);
await service.setLocal(6, 30);
final paths = <String>[];
final client = http_testing.MockClient((request) async {
paths.add(request.url.path);
return _response(200);
});
final remote = MemGitHub();
await service.push('tok', httpClient: client);
await service.push('tok', httpClient: remote.httpClient);
expect(paths.any((path) => path.contains('devices/uuid-abc/')), isTrue);
final paths = remote.putPaths;
expect(
paths.any((path) => path.contains('devices-e2e/uuid-abc/')),
isTrue,
);
expect(
paths.any(
(path) =>
path.contains('devices/${AlarmSyncService.legacyDeviceId}/'),
(path) => path.contains('/${AlarmSyncService.legacyDeviceId}/'),
),
isFalse,
);

View File

@ -12,11 +12,11 @@ dependencies = [
# Firebase transport for the alarm read. _sync.py imports it lazily and
# falls back to its stdlib GitHub path if the import fails, so a broken
# install degrades the read instead of killing the per-minute trigger.
"crdt-sync @ git+https://github.com/kuhyx/utils@crdt-sync-v0.9.2#subdirectory=crdt-sync",
"freedays @ git+https://github.com/kuhyx/utils@freedays-v0.4.0#subdirectory=freedays",
"crdt-sync @ git+https://github.com/kuhyx/utils@crdt-sync-v0.11.0#subdirectory=crdt-sync",
"freedays @ git+https://github.com/kuhyx/utils@freedays-v0.4.2#subdirectory=freedays",
# Sick days stand the PC's enforcement down (_sick.py). Imported behind
# a guard: missing means "not sick", never a crashed tick.
"sickdays @ git+https://github.com/kuhyx/utils@sickdays-v0.5.0#subdirectory=sickdays",
"sickdays @ git+https://github.com/kuhyx/utils@sickdays-v0.5.3#subdirectory=sickdays",
]
# Package discovery must be explicit: phone_app/ is a Flutter app, not a Python

View File

@ -5,8 +5,8 @@
bandit==1.9.4
codespell==2.4.3
coverage==7.17.2
crdt-sync @ git+https://github.com/kuhyx/utils@crdt-sync-v0.9.2#subdirectory=crdt-sync
freedays @ git+https://github.com/kuhyx/utils@freedays-v0.4.0#subdirectory=freedays
crdt-sync @ git+https://github.com/kuhyx/utils@crdt-sync-v0.11.0#subdirectory=crdt-sync
freedays @ git+https://github.com/kuhyx/utils@freedays-v0.4.2#subdirectory=freedays
gatelock @ git+https://github.com/kuhyx/utils@gatelock-v0.13.0#subdirectory=gatelock
mypy==2.4.1
pre-commit==4.7.0
@ -17,4 +17,4 @@ pytest-randomly==5.1.0
pytest-sugar==1.1.1
pytest-xdist==3.8.0
ruff==0.17.4
sickdays @ git+https://github.com/kuhyx/utils@sickdays-v0.5.0#subdirectory=sickdays
sickdays @ git+https://github.com/kuhyx/utils@sickdays-v0.5.3#subdirectory=sickdays

View File

@ -60,21 +60,21 @@ LOG_JSONL_FILE: Path = LOG_STATE_DIR / "events.jsonl"
# suppressed across separate per-minute trigger processes.
LOG_DEDUPE_FILE: Path = LOG_STATE_DIR / "last_event.json"
# The phone's path to its published alarm. Deliberately the *same* logical
# path on both backends — Firebase's namespace mirrors the repo layout — so
# Where the phone publishes, end-to-end encrypted under the shared crdt-sync
# passphrase: the ``-e2e`` twin of the old plaintext prefix. The same logical
# path on both backends (Firebase's namespace mirrors the repo layout), so
# only the transport changes, never the parse.
#
# This is the *legacy* path: the phone used to be the sole writer under the
# fixed device id "phone". It still is in practice, but the id is migrating
# to a per-install uuid, so the reader discovers device directories under
# SYNC_DEVICES_PREFIX and falls back to this path for a phone that has not
# yet published under its uuid. Keep reading it until every install has
# migrated; deleting it early means a stale phone's alarm silently stops
# being seen, and the trigger falls back to FALLBACK_ALARM_HOUR.
SYNC_ALARM_PATH: str = "wake-alarm-sync/devices/phone/alarm.json"
SYNC_DEVICES_PREFIX: str = "wake-alarm-sync/devices-e2e"
# The directory holding one subdirectory per publishing device.
SYNC_DEVICES_PREFIX: str = "wake-alarm-sync/devices"
# The old plaintext prefix. Never read for the alarm itself any more: only
# the one-time E2E copy reads it, picking up a phone not yet updated.
SYNC_LEGACY_PREFIX: str = "wake-alarm-sync/devices"
# The *legacy device id* path: the phone used to be the sole writer under the
# fixed id "phone"; it now publishes under a per-install uuid, so the reader
# discovers device directories and falls back to this one only when nothing
# lists. Copied over from the plaintext prefix like everything else.
SYNC_ALARM_PATH: str = f"{SYNC_DEVICES_PREFIX}/phone/alarm.json"
# The file each device publishes its alarm as, inside its own directory.
SYNC_ALARM_FILENAME: str = "alarm.json"
@ -84,8 +84,7 @@ SYNC_ALARM_FILENAME: str = "alarm.json"
SYNC_APP_NAME: str = "wake_alarm"
# The shared private repo, used only by the GitHub fallback path. Retiring the
# mirror means deleting these three and _fetch_github_alarm() together.
SYNC_API_BASE: str = "https://api.github.com"
# mirror means deleting these two and _fetch_github_alarm() together.
SYNC_REPO_OWNER: str = "kuhyx"
SYNC_REPO_NAME: str = "syncs"

View File

@ -3,8 +3,14 @@
Split out of ``_sync.py`` for the 250-line cap, along the seam item 8 needs:
this module is the **transport** (token discovery and the two backends), while
``_sync.py`` keeps **resolution** (cache, staleness, fallback) and the CLI.
Item 8 adds a dismissal-record reader, which belongs here beside the alarm
reader rather than inside the resolution chain.
Both backends hold the same end-to-end encrypted layout
(``wake-alarm-sync/devices-e2e``), sealed under the shared crdt-sync
passphrase (``~/.config/crdt-sync/password``). Every read goes through
``crdt_sync.open_encrypted_client``, which also copies the old plaintext
prefix over once per process, so a phone not yet updated is still seen. A
missing or wrong passphrase reads as "nothing published" (fail closed: the
resolution chain falls to the cache, then 08:00), never as plaintext.
``ALARM_CACHE_FILE`` deliberately does NOT live here: the suite patches
``wake_alarm._sync.ALARM_CACHE_FILE`` to keep tests off
@ -17,30 +23,42 @@ from __future__ import annotations
import json
import logging
import urllib.error
import urllib.request
from typing import TYPE_CHECKING
from wake_alarm._alarm_payload import newest_alarm, parse_log
from wake_alarm._alarm_payload import newest_alarm
from wake_alarm._constants import (
SYNC_ALARM_FILENAME,
SYNC_ALARM_PATH,
SYNC_API_BASE,
SYNC_APP_NAME,
SYNC_CONFIG_FILE,
SYNC_DEVICES_PREFIX,
SYNC_HTTP_TIMEOUT_SECONDS,
SYNC_LEGACY_PREFIX,
SYNC_REPO_NAME,
SYNC_REPO_OWNER,
SYNC_TOKEN_FALLBACK_FILES,
)
if TYPE_CHECKING:
from crdt_sync import Argon2Params, RemoteStore
_logger = logging.getLogger(__name__)
# Optional at runtime, exactly like python-kasa in _smart_plug.py: the trigger
# runs every minute under /usr/bin/python, so a dependency that failed to
# install must degrade this read to the GitHub path, not raise.
# install must degrade this read to the cache, not raise. Without crdt_sync
# (and its pynacl/argon2-cffi) nothing can be decrypted, so neither backend
# is read.
try:
from crdt_sync import ConfigError, firebase_client_for
from crdt_sync import (
PASSWORD_FILE,
ConfigError,
E2eDecryptError,
E2eLayout,
E2eLockedError,
GitHubSyncClient,
firebase_client_for,
open_encrypted_client,
)
from crdt_sync._config import CONFIG_FILE as FIREBASE_CONFIG_FILE
from crdt_sync._firebase_auth import FirebaseAuthError
from crdt_sync._remote import RemoteSyncError
@ -48,7 +66,61 @@ try:
_FIREBASE_AVAILABLE = True
except ImportError:
_FIREBASE_AVAILABLE = False
_logger.info("crdt_sync is not installed; reading the alarm from GitHub")
_logger.info("crdt_sync is not installed; the alarm cannot be fetched")
#: Argon2id cost for a header this machine creates; None is the library's
#: default. Only tests lower it.
E2E_ARGON2_PARAMS: Argon2Params | None = None
def _read_passphrase() -> str:
"""The shared crdt-sync passphrase, or "" (locked) when there is none."""
try:
return PASSWORD_FILE.read_text().strip()
except OSError:
return ""
def _read_sealed(store: RemoteStore, path: str) -> str | None:
"""One device's decrypted record; None when absent or undecryptable."""
try:
return store.get_file_text(path)
except E2eDecryptError as exc:
_logger.info("Skipping an unreadable alarm at %s: %s", path, exc)
return None
def _read_published(client: RemoteStore) -> list[tuple[str, str]]:
"""Every device's decrypted ``alarm.json`` through ``client``.
Discovers device directories rather than reading one fixed path: the
phone's device id is migrating from the constant "phone" to a per-install
uuid, and a reader pinned to the old path would stop seeing the alarm the
moment the phone migrated -- falling back to FALLBACK_ALARM_HOUR, i.e.
waking the user at the wrong time. Listing covers both ids, and
SYNC_ALARM_PATH remains as a last resort for a backend that cannot list.
Raises:
E2eLockedError: No usable key; the caller reads that as nothing.
"""
store, layout = open_encrypted_client(
client,
SYNC_LEGACY_PREFIX,
passphrase=_read_passphrase(),
params=E2E_ARGON2_PARAMS,
)
published: list[tuple[str, str]] = []
for device_id in store.list_directory(layout.prefix):
raw = _read_sealed(store, f"{layout.prefix}/{device_id}/{SYNC_ALARM_FILENAME}")
if raw is not None:
published.append((device_id, raw))
if not published:
# No directories listed (or none held a file): still try the
# legacy fixed path, so a phone that has not migrated is read.
raw = _read_sealed(store, SYNC_ALARM_PATH)
if raw is not None:
published.append(("phone", raw))
return published
def _read_token_config() -> str | None:
@ -104,25 +176,19 @@ def _read_token() -> str | None:
def fetch_firebase_logs() -> list[tuple[str, str]] | None:
"""Fetch every device's raw ``alarm.json`` from Firebase.
Discovers device directories rather than reading one fixed path: the
phone's device id is migrating from the constant "phone" to a per-install
uuid, and a reader pinned to the old path would stop seeing the alarm the
moment the phone migrated — falling back to FALLBACK_ALARM_HOUR, i.e.
waking the user at the wrong time. Listing covers both ids, and
SYNC_ALARM_PATH remains as a last resort for a backend that cannot list.
"""Fetch every device's decrypted ``alarm.json`` from Firebase.
Shared by the alarm reader and ``_weight``: the morning session's
weigh-in rides on the same record, so one fetch serves both.
Returns None — never raises — for every failure mode, including
``crdt_sync`` not being importable on this interpreter, so the caller can
fall back to GitHub and then to the cache.
``crdt_sync`` not being importable on this interpreter and a locked
(no or wrong passphrase) store, so the caller can fall back to GitHub and
then to the cache.
Returns:
``(device_id, raw_json)`` per device, or None if unconfigured or
unreadable.
``(device_id, raw_json)`` per device, or None if unconfigured,
locked or unreadable.
"""
if not _FIREBASE_AVAILABLE:
return None
@ -134,25 +200,19 @@ def fetch_firebase_logs() -> list[tuple[str, str]] | None:
return None
try:
client = firebase_client_for(SYNC_APP_NAME)
published: list[tuple[str, str]] = []
for device_id in client.list_directory(SYNC_DEVICES_PREFIX):
raw = client.get_file_text(
f"{SYNC_DEVICES_PREFIX}/{device_id}/{SYNC_ALARM_FILENAME}"
)
if raw is not None:
published.append((device_id, raw))
if not published:
# No directories listed (or none held a file): still try the
# legacy fixed path, so a phone that has not migrated is read.
raw = client.get_file_text(SYNC_ALARM_PATH)
if raw is None:
_logger.info("No alarm published to Firebase yet")
return None
published.append(("phone", raw))
except (ConfigError, FirebaseAuthError, RemoteSyncError, OSError) as exc:
published = _read_published(firebase_client_for(SYNC_APP_NAME))
except (
ConfigError,
FirebaseAuthError,
RemoteSyncError,
OSError,
E2eLockedError,
) as exc:
_logger.info("Cannot fetch the alarm from Firebase: %s", exc)
return None
if not published:
_logger.info("No alarm published to Firebase yet")
return None
return published
@ -163,17 +223,18 @@ def _fetch_firebase_alarm() -> tuple[int, int, float | None] | None:
def _fetch_github_alarm() -> tuple[int, int, float | None] | None:
"""Fetch the alarm the phone last pushed to GitHub.
"""Fetch the alarm the phone last pushed to the GitHub mirror.
Uses the raw media type so the Contents API returns the file body directly
rather than base64-wrapped JSON.
The same encrypted layout as Firebase (the phone's mirror store writes
both), read through ``crdt_sync``'s Contents API client.
Returns:
``(hour, minute, published_at)`` -- age unknown on this path -- or
None if unconfigured, unreachable, or
malformed. Never raises — a failed read must degrade to the cache,
not crash the trigger.
``(hour, minute, published_at)``, or None if unconfigured,
unreachable, locked or malformed. Never raises — a failed read must
degrade to the cache, not crash the trigger.
"""
if not _FIREBASE_AVAILABLE:
return None
token = _read_token()
if token is None:
# INFO, not WARNING: get_alarm() runs on every one-minute trigger tick,
@ -182,43 +243,26 @@ def _fetch_github_alarm() -> tuple[int, int, float | None] | None:
# actually fires on a degraded source — that is the actionable event.
_logger.info("No sync token configured at %s", SYNC_CONFIG_FILE)
return None
url = (
f"{SYNC_API_BASE}/repos/{SYNC_REPO_OWNER}/{SYNC_REPO_NAME}"
f"/contents/{SYNC_ALARM_PATH}"
client = GitHubSyncClient(
SYNC_REPO_OWNER,
SYNC_REPO_NAME,
token,
timeout_seconds=SYNC_HTTP_TIMEOUT_SECONDS,
)
request = urllib.request.Request(
url,
headers={
"Authorization": f"token {token}",
"Accept": "application/vnd.github.raw",
},
)
# The URL is built from module constants, never from remote input, but
# assert the scheme anyway: this call carries a credential, and an http://
# or file:// target would leak it. Belt and braces for a bad edit above.
if request.type != "https":
_logger.error("Refusing to send the sync token over %s", request.type)
return None
try:
with urllib.request.urlopen(
request, timeout=SYNC_HTTP_TIMEOUT_SECONDS
) as response:
raw = response.read().decode("utf-8")
except (urllib.error.URLError, OSError, UnicodeDecodeError) as exc:
# Read-only on the mirror: only the phone may create its header. A
# PC-made one (fresh salt) would leave every blob the phone sealed
# unreadable there, and could mislead a phone opening offline.
if client.get_file_text(E2eLayout.of(SYNC_LEGACY_PREFIX).e2e_path) is None:
_logger.info("No encrypted alarm on the GitHub mirror yet")
return None
published = _read_published(client)
except (RemoteSyncError, OSError, E2eLockedError) as exc:
# INFO for the same reason as the no-token path: a persistent failure
# (404 because the phone never pushed, an expired token, no network)
# would otherwise be one WARNING per cache expiry, forever.
_logger.info("Cannot fetch synced alarm: %s", exc)
return None
parsed = parse_log(raw)
if parsed is None:
return None
# The mirror is a single fixed path with no per-field HLC to read, so its
# age is unknown rather than fresh. See hlc_instant on why that is treated
# as trustworthy.
return (*parsed, None)
return newest_alarm(published) if published else None
def fetch_remote_alarm() -> tuple[int, int, float | None] | None:

View File

@ -34,6 +34,7 @@ from contextlib import ExitStack
from typing import TYPE_CHECKING
from unittest.mock import patch
from crdt_sync import Argon2Params
import freedays
import pytest
import sickdays
@ -72,6 +73,67 @@ class FakeClock:
self.elapsed += seconds
#: The passphrase every test device shares.
TEST_PHRASE = "test-passphrase"
#: Argon2id at a cost a test suite can afford many times over.
TEST_ARGON2 = Argon2Params(memory_kib=8, iterations=1, parallelism=1)
def _passphrase(tmp_path: Path) -> Path:
path = tmp_path / "crdt-sync-password"
path.write_text(TEST_PHRASE)
return path
class DictStore:
"""In-memory ``RemoteStore``: paths are keys, directories are derived."""
def __init__(self, files: dict[str, str] | None = None) -> None:
"""Start with ``files`` (path -> text)."""
self.files = dict(files or {})
self.puts: list[str] = []
def list_directory(self, path: str) -> list[str]:
"""Immediate children of ``path``."""
prefix = f"{path}/"
return sorted(
{
k[len(prefix) :].split("/", 1)[0]
for k in self.files
if k.startswith(prefix)
}
)
def get_file_text(self, path: str) -> str | None:
"""The stored text, or None."""
return self.files.get(path)
def put_file_text(self, path: str, text: str, *, message: str) -> None:
"""Store ``text`` and record the write."""
del message
self.files[path] = text
self.puts.append(path)
def get_string_map(self, path: str) -> dict[str, str]:
"""Leaf children of ``path`` with their text."""
prefix = f"{path}/"
return {
k[len(prefix) :]: v
for k, v in self.files.items()
if k.startswith(prefix) and "/" not in k[len(prefix) :]
}
def delete_file(self, path: str, *, message: str = "") -> None:
"""Forget ``path``."""
del message
self.files.pop(path, None)
def can_access_remote(self) -> bool:
"""Always reachable."""
return True
@pytest.fixture
def fake_clock() -> FakeClock:
"""A fresh :class:`FakeClock`."""
@ -118,6 +180,11 @@ def _hermetic(tmp_path: Path) -> Generator[None]:
tmp_path / "no-firebase.json",
create=True,
),
# End-to-end encryption: never the real ~/.config/crdt-sync/password,
# a cheap Argon2id cost, and no per-process copy memo across tests.
patch("wake_alarm._sync_transport.PASSWORD_FILE", _passphrase(tmp_path)),
patch("wake_alarm._sync_transport.E2E_ARGON2_PARAMS", TEST_ARGON2),
patch.dict("crdt_sync._e2e_store._copied", clear=True),
patch("wake_alarm._events.EVENT_STATE_FILE", tmp_path / "event_state.json"),
patch(
"wake_alarm._fallback_latch.FALLBACK_RING_FILE",

View File

@ -5,12 +5,13 @@ from __future__ import annotations
import json
from typing import TYPE_CHECKING
from unittest.mock import MagicMock, patch
import urllib.error
from crdt_sync import RemoteSyncError, open_encrypted_client
import pytest
from wake_alarm._constants import (
FALLBACK_ALARM_HOUR,
SYNC_LEGACY_PREFIX,
)
from wake_alarm._sync import (
Alarm,
@ -19,6 +20,7 @@ from wake_alarm._sync import (
_write_cache,
get_alarm,
)
from wake_alarm.tests.conftest import TEST_ARGON2, TEST_PHRASE, DictStore
if TYPE_CHECKING:
from pathlib import Path
@ -77,6 +79,28 @@ class TestAlarm:
assert str(Alarm(7, 5, "remote")) == "07:05 (remote)"
def _records(text: str) -> object:
"""Serve ``text`` for every record path, beside a phone-made header."""
header = {}
def _get(path: str) -> str | None:
if path.endswith("/e2e"):
if not header:
header["h"] = _phone_header()
return header["h"]
return text
return _get
def _phone_header() -> str:
store = DictStore()
open_encrypted_client(
store, SYNC_LEGACY_PREFIX, passphrase=TEST_PHRASE, params=TEST_ARGON2
)
return store.files["wake-alarm-sync/e2e"]
class TestFetchRemoteAlarm:
"""Tests for the GitHub backend behind fetch_remote_alarm."""
@ -87,63 +111,45 @@ class TestFetchRemoteAlarm:
def _with_token(self, config_file: Path) -> None:
config_file.write_text(json.dumps({"token": "ghp_x"}))
def _client(self, **methods: object) -> MagicMock:
"""A Contents API client stand-in; never the network."""
client = MagicMock()
for name, value in methods.items():
setattr(client, name, value)
return client
def test_success(self, config_file: Path) -> None:
"""A 200 with a valid body yields the pair."""
"""A published log through the (encrypting) client yields the pair."""
self._with_token(config_file)
response = MagicMock()
response.read.return_value = _log(6, 15).encode()
response.__enter__.return_value = response
with patch("urllib.request.urlopen", return_value=response):
client = self._client(
list_directory=MagicMock(return_value=["phone"]),
get_file_text=MagicMock(side_effect=_records(_log(6, 15))),
)
del client.get_string_map
with patch("wake_alarm._sync_transport.GitHubSyncClient", return_value=client):
fetched = _fetch_github_alarm()
assert fetched is not None
assert fetched[:2] == (6, 15)
def test_unparseable_body_is_not_an_alarm(self, config_file: Path) -> None:
"""A 200 whose body is not a usable log yields nothing, not a crash."""
"""A body that is not a usable log yields nothing, not a crash."""
self._with_token(config_file)
response = MagicMock()
response.read.return_value = b"{}"
response.__enter__.return_value = response
with patch("urllib.request.urlopen", return_value=response):
client = self._client(
list_directory=MagicMock(return_value=["phone"]),
get_file_text=MagicMock(side_effect=_records("{}")),
)
del client.get_string_map
with patch("wake_alarm._sync_transport.GitHubSyncClient", return_value=client):
assert _fetch_github_alarm() is None
def test_sends_raw_media_type_and_auth(self, config_file: Path) -> None:
"""The request carries the token and the raw Accept header."""
self._with_token(config_file)
response = MagicMock()
response.read.return_value = _log().encode()
response.__enter__.return_value = response
with patch("urllib.request.urlopen", return_value=response) as urlopen:
_fetch_github_alarm()
request = urlopen.call_args[0][0]
assert request.get_header("Authorization") == "token ghp_x"
assert request.get_header("Accept") == "application/vnd.github.raw"
assert "kuhyx/syncs" in request.full_url
def test_refuses_a_non_https_scheme(self, config_file: Path) -> None:
"""The token is never sent over a plaintext or file:// URL."""
self._with_token(config_file)
with (
patch("wake_alarm._sync_transport.SYNC_API_BASE", "http://api.github.com"),
patch("urllib.request.urlopen") as urlopen,
):
assert _fetch_github_alarm() is None
urlopen.assert_not_called()
@pytest.mark.parametrize(
"exc",
[
urllib.error.URLError("down"),
OSError("timeout"),
UnicodeDecodeError("utf-8", b"\xff", 0, 1, "bad"),
],
)
@pytest.mark.parametrize("exc", [RemoteSyncError("down"), OSError("timeout")])
def test_network_failures_yield_none(
self, config_file: Path, exc: Exception
) -> None:
"""Every transport failure degrades to None, never raises."""
self._with_token(config_file)
with patch("urllib.request.urlopen", side_effect=exc):
client = self._client(get_file_text=MagicMock(side_effect=exc))
with patch("wake_alarm._sync_transport.GitHubSyncClient", return_value=client):
assert _fetch_github_alarm() is None

View File

@ -10,6 +10,7 @@ import json
from typing import TYPE_CHECKING
from unittest.mock import MagicMock, patch
from crdt_sync import E2eLayout
import pytest
from wake_alarm._constants import (
@ -70,6 +71,9 @@ class TestFetchFirebaseAlarm:
firebase_client_for=MagicMock(
return_value=client if client is not None else MagicMock()
),
# Ranking and fallback are under test here, not encryption
# (test_sync_e2e.py): a pass-through open on the E2E layout.
open_encrypted_client=lambda store, old, **_: (store, E2eLayout.of(old)),
ConfigError=_FakeConfigError,
FirebaseAuthError=_FakeAuthError,
RemoteSyncError=_FakeRemoteError,

View File

@ -0,0 +1,178 @@
"""The PC reads the phone's alarm end-to-end encrypted, on both backends.
Real ``crdt_sync`` encryption over a dict-backed remote: the header, the
sealed records and the one-time copy of the old plaintext prefix are the
bytes a real backend would hold.
"""
from __future__ import annotations
import json
from typing import TYPE_CHECKING
from unittest.mock import patch
from crdt_sync import open_encrypted_client
import pytest
from wake_alarm import _sync_transport
from wake_alarm._constants import SYNC_DEVICES_PREFIX, SYNC_LEGACY_PREFIX
from wake_alarm.tests.conftest import TEST_ARGON2, TEST_PHRASE, DictStore
if TYPE_CHECKING:
from collections.abc import Iterator
from pathlib import Path
_HLC = "2026-11-12T05:00:00.000Z-0000-uuid1"
@pytest.fixture
def config_file(tmp_path: Path) -> Iterator[Path]:
"""This app's GitHub token config, redirected; no sibling fallbacks."""
path = tmp_path / "sync.json"
with (
patch.object(_sync_transport, "SYNC_CONFIG_FILE", path),
patch.object(_sync_transport, "SYNC_TOKEN_FALLBACK_FILES", ()),
):
yield path
def _log(hour: int, minute: int, hlc: str = _HLC) -> str:
return json.dumps(
{
"alarm": {
"id": "alarm",
"fields": {"alarm_hour": [hour, hlc], "alarm_minute": [minute, hlc]},
"deleted": False,
}
}
)
def _phone_publishes(store: DictStore, hour: int, minute: int) -> None:
"""What the updated phone app does: seal its log under the E2E prefix."""
sealed, layout = open_encrypted_client(
store, SYNC_LEGACY_PREFIX, passphrase=TEST_PHRASE, params=TEST_ARGON2
)
sealed.put_file_text(
f"{layout.prefix}/uuid1/alarm.json", _log(hour, minute), message="t"
)
def _firebase(store: DictStore, tmp_path: Path) -> object:
config = tmp_path / "firebase.json"
config.write_text("{}")
return patch.multiple(
"wake_alarm._sync_transport",
FIREBASE_CONFIG_FILE=config,
firebase_client_for=lambda _app: store,
)
class TestFirebase:
def test_reads_the_sealed_alarm(self, tmp_path: Path) -> None:
store = DictStore()
_phone_publishes(store, 6, 40)
blob = store.files[f"{SYNC_DEVICES_PREFIX}/uuid1/alarm.json"]
assert "alarm_hour" not in blob
with _firebase(store, tmp_path):
fetched = _sync_transport._fetch_firebase_alarm()
assert fetched is not None
assert fetched[:2] == (6, 40)
def test_a_phone_not_yet_updated_is_copied_and_read(self, tmp_path: Path) -> None:
old = f"{SYNC_LEGACY_PREFIX}/phone/alarm.json"
store = DictStore({old: _log(7, 5)})
with _firebase(store, tmp_path):
fetched = _sync_transport._fetch_firebase_alarm()
assert fetched is not None
assert fetched[:2] == (7, 5)
assert (
"alarm_hour" not in store.files[f"{SYNC_DEVICES_PREFIX}/phone/alarm.json"]
)
assert store.files[old] == _log(7, 5)
def test_no_passphrase_reads_nothing(self, tmp_path: Path) -> None:
store = DictStore()
_phone_publishes(store, 6, 40)
with (
_firebase(store, tmp_path),
patch.object(_sync_transport, "PASSWORD_FILE", tmp_path / "gone"),
):
assert _sync_transport.fetch_firebase_logs() is None
def test_wrong_passphrase_reads_nothing(self, tmp_path: Path) -> None:
store = DictStore()
_phone_publishes(store, 6, 40)
other = tmp_path / "other"
other.write_text("not the passphrase")
with (
_firebase(store, tmp_path),
patch.object(_sync_transport, "PASSWORD_FILE", other),
):
assert _sync_transport.fetch_firebase_logs() is None
def test_a_blob_under_another_key_is_skipped(self, tmp_path: Path) -> None:
store = DictStore()
_phone_publishes(store, 6, 40)
alien = DictStore()
_phone_publishes(alien, 9, 0) # its own header, so its own key
path = f"{SYNC_DEVICES_PREFIX}/uuid1/alarm.json"
store.files[f"{SYNC_DEVICES_PREFIX}/stranger/alarm.json"] = alien.files[path]
with _firebase(store, tmp_path):
published = _sync_transport.fetch_firebase_logs()
assert published is not None
assert [device for device, _ in published] == ["uuid1"]
class TestGitHubMirror:
def test_reads_the_sealed_alarm_through_the_contents_client(
self, config_file: Path
) -> None:
config_file.write_text(json.dumps({"token": "ghp_x"}))
store = DictStore()
_phone_publishes(store, 5, 55)
with patch.object(
_sync_transport, "GitHubSyncClient", return_value=store
) as client_cls:
fetched = _sync_transport._fetch_github_alarm()
assert fetched is not None
assert fetched[:2] == (5, 55)
assert client_cls.call_args.args[2] == "ghp_x"
def test_locked_reads_nothing(self, config_file: Path, tmp_path: Path) -> None:
config_file.write_text(json.dumps({"token": "ghp_x"}))
store = DictStore()
_phone_publishes(store, 5, 55)
with (
patch.object(_sync_transport, "GitHubSyncClient", return_value=store),
patch.object(_sync_transport, "PASSWORD_FILE", tmp_path / "gone"),
):
assert _sync_transport._fetch_github_alarm() is None
def test_no_phone_header_reads_nothing_and_writes_nothing(
self, config_file: Path
) -> None:
config_file.write_text(json.dumps({"token": "ghp_x"}))
store = DictStore()
with patch.object(_sync_transport, "GitHubSyncClient", return_value=store):
assert _sync_transport._fetch_github_alarm() is None
assert store.puts == []
def test_header_but_no_alarm_is_none(self, config_file: Path) -> None:
config_file.write_text(json.dumps({"token": "ghp_x"}))
store = DictStore()
open_encrypted_client(
store, SYNC_LEGACY_PREFIX, passphrase=TEST_PHRASE, params=TEST_ARGON2
)
with patch.object(_sync_transport, "GitHubSyncClient", return_value=store):
assert _sync_transport._fetch_github_alarm() is None
def test_without_crdt_sync_nothing_is_read(self) -> None:
with patch.object(_sync_transport, "_FIREBASE_AVAILABLE", new=False):
assert _sync_transport._fetch_github_alarm() is None