mirror of
https://github.com/kuhyx/diet-guard.git
synced 2026-11-19 11:54:31 +01:00
Some checks failed
dependency freshness / dependencies on newest stable (push) Has been cancelled
file-length / file length <= 250 lines (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) (push) Has been cancelled
Tests / test (push) Has been cancelled
release-apk / release (APK) (push) Has been cancelled
repo-contract / repo contract (push) Has been cancelled
Every sync payload (food log, banks, budget, catering credential, Body, export keyring) now lives on diet-guard-sync/devices-e2e, sealed with the Argon2id key from the shared crdt-sync passphrase; record ids, tombstones and clocks stay clear. Old plaintext nodes are copied once and never written or deleted. Locked = no pull, no push. - PC: _sync_e2e.open_e2e over crdt-sync 0.11.0, key and copy memo in ~/.local/share/diet_guard/e2e; undecryptable peers are skipped. - Phone/desktop: sync_e2e.dart, crdt_sync_flutter 0.8.1, lock icon in Settings; pullExportKeys goes through the same open. - wake-alarm weigh-ins read from wake-alarm-sync/devices-e2e (own header), on both sides. Todo: fda71ed1 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
166 lines
6.0 KiB
Dart
166 lines
6.0 KiB
Dart
// End-to-end encryption of the diet_guard sync tick (`sync_e2e.dart`).
|
|
//
|
|
// The fake GitHub stores what is PUT, so the header written on first use
|
|
// reads back and the sealed blobs are the bytes a real backend would hold.
|
|
|
|
import 'dart:convert';
|
|
import 'dart:io';
|
|
|
|
import 'package:crdt_sync/crdt_sync.dart' show E2eLockReason, RemoteStore;
|
|
import 'package:diet_guard_app/services/app_settings_service.dart';
|
|
import 'package:diet_guard_app/services/budget_history_service.dart';
|
|
import 'package:diet_guard_app/services/document_store_io.dart';
|
|
import 'package:diet_guard_app/services/export_keyring_default.dart';
|
|
import 'package:diet_guard_app/services/foodbank_service.dart';
|
|
import 'package:diet_guard_app/services/log_storage_service.dart';
|
|
import 'package:diet_guard_app/services/sync_e2e.dart';
|
|
import 'package:diet_guard_app/services/sync_merge.dart';
|
|
import 'package:diet_guard_app/services/sync_service.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
|
|
import '../fake_secure_storage.dart';
|
|
import 'sync_service_test_support.dart';
|
|
|
|
const _new = 'diet-guard-sync/devices-e2e';
|
|
const _old = 'diet-guard-sync/devices';
|
|
|
|
String _text(Map<String, dynamic> put) =>
|
|
utf8.decode(base64.decode(put['content'] as String));
|
|
|
|
void main() {
|
|
late Directory tempDir;
|
|
|
|
setUp(() async {
|
|
TestWidgetsFlutterBinding.ensureInitialized();
|
|
installFakeSecureStorage();
|
|
tempDir = await Directory.systemTemp.createTemp('diet_guard_e2e_test_');
|
|
LogStorageService.resetForTesting(store: FileDocumentStore(tempDir));
|
|
FoodBankService.resetForTesting(store: FileDocumentStore(tempDir));
|
|
await AppSettingsService.initForTesting(FileDocumentStore(tempDir));
|
|
BudgetHistoryService.resetForTesting(store: FileDocumentStore(tempDir));
|
|
});
|
|
|
|
tearDown(() async {
|
|
LogStorageService.resetForTesting();
|
|
FoodBankService.resetForTesting();
|
|
AppSettingsService.resetForTesting();
|
|
BudgetHistoryService.resetForTesting();
|
|
await tempDir.delete(recursive: true);
|
|
});
|
|
|
|
test('the pushed food log holds no plaintext field', () async {
|
|
await LogStorageService.instance.logMeal('secret oatmeal', manualNutrition);
|
|
final fake = FakeGitHub();
|
|
await runSync(fake.buildClient());
|
|
|
|
final blob = _text(fake.putsByPath['$_new/phone/food_log.json']!);
|
|
expect(blob, isNot(contains('secret')));
|
|
expect(blob, isNot(contains('"fields"')));
|
|
expect(blob, isNot(contains('kcal')));
|
|
expect(await fake.opened('$_new/phone/food_log.json'), contains('secret'));
|
|
});
|
|
|
|
test('nothing is written to the old plaintext prefix or revs', () async {
|
|
await LogStorageService.instance.logMeal('oatmeal', manualNutrition);
|
|
final fake = FakeGitHub();
|
|
await runSync(fake.buildClient());
|
|
|
|
expect(fake.putsByPath, isNotEmpty);
|
|
for (final path in fake.putsByPath.keys) {
|
|
expect(path, isNot(startsWith('$_old/')));
|
|
expect(path, isNot(startsWith('diet-guard-sync/revs/')));
|
|
}
|
|
expect(fake.putsByPath, contains('diet-guard-sync/revs-e2e/phone'));
|
|
});
|
|
|
|
test('export keys are pushed sealed, never to the old node', () async {
|
|
await defaultKeyring().ensureNewest();
|
|
final fake = FakeGitHub();
|
|
await runSync(fake.buildClient());
|
|
|
|
final sealed = fake.putsByPath['$_new/phone/export_keys.json'];
|
|
expect(sealed, isNotNull);
|
|
final opened = (await fake.opened('$_new/phone/export_keys.json'))!;
|
|
final record = (jsonDecode(opened) as Map).values.single as Map;
|
|
final keyB64 = ((record['fields'] as Map)['k'] as List).first as String;
|
|
expect(_text(sealed!), isNot(contains(keyB64)));
|
|
expect(fake.putsByPath, isNot(contains('$_old/phone/export_keys.json')));
|
|
});
|
|
|
|
test('an old plaintext peer log is copied, merged, and left alone', () async {
|
|
await LogStorageService.instance.logMeal('peer porridge', manualNutrition);
|
|
final local = await LogStorageService.instance.readLog();
|
|
final peerLog = encodeLogForPush(dayLogToLog(local));
|
|
await LogStorageService.instance.writeLog(const {});
|
|
|
|
final fake = FakeGitHub(
|
|
deviceDirs: const ['pc'],
|
|
files: {'$_old/pc/food_log.json': peerLog},
|
|
);
|
|
final merged = await runSync(fake.buildClient());
|
|
|
|
final descs = merged.values.expand((e) => e).map((e) => e.desc);
|
|
expect(descs, contains('peer porridge'));
|
|
expect(
|
|
_text(fake.putsByPath['$_new/pc/food_log.json']!),
|
|
isNot(contains('porridge')),
|
|
);
|
|
expect(fake.files['$_old/pc/food_log.json'], peerLog);
|
|
});
|
|
|
|
test('no passphrase: locked, nothing read or written', () async {
|
|
installFakeSecureStorage(initial: const {'crdt_sync_e2e_passphrase': ''});
|
|
final fake = FakeGitHub();
|
|
await expectLater(
|
|
runSync(fake.buildClient()),
|
|
throwsA(isA<SyncLockedException>()),
|
|
);
|
|
expect(fake.puts, isEmpty);
|
|
expect(syncLocked.value, isTrue);
|
|
expect(
|
|
const SyncLockedException(E2eLockReason.wrongKey).toString(),
|
|
contains('wrongKey'),
|
|
);
|
|
});
|
|
|
|
test('a wake-alarm transport failure skips only the weigh-ins', () async {
|
|
await LogStorageService.instance.logMeal('oatmeal', manualNutrition);
|
|
final fake = FakeGitHub();
|
|
final store = _FailingPath(fake.buildClient(), 'wake-alarm-sync/e2e');
|
|
|
|
await runSync(store);
|
|
|
|
expect(fake.putsByPath, contains('$_new/phone/food_log.json'));
|
|
expect(await openWakeAlarmStore(store), isNull);
|
|
});
|
|
}
|
|
|
|
/// Delegates to [inner] but fails every read of [path].
|
|
class _FailingPath implements RemoteStore {
|
|
_FailingPath(this.inner, this.path);
|
|
|
|
final RemoteStore inner;
|
|
final String path;
|
|
|
|
@override
|
|
Future<String?> getFileText(String p) =>
|
|
p == path ? Future.error(Exception('down')) : inner.getFileText(p);
|
|
|
|
@override
|
|
Future<List<String>> listDirectory(String p) => inner.listDirectory(p);
|
|
|
|
@override
|
|
Future<void> putFileText(String p, String text, {required String message}) =>
|
|
inner.putFileText(p, text, message: message);
|
|
|
|
@override
|
|
Future<void> deleteFile(String p, {String message = ''}) =>
|
|
inner.deleteFile(p, message: message);
|
|
|
|
@override
|
|
Future<bool> canAccessRemote() => inner.canAccessRemote();
|
|
|
|
@override
|
|
void close() => inner.close();
|
|
}
|