From f7e24bbb1ab0979a98544d15a641d3252d827b57 Mon Sep 17 00:00:00 2001 From: Krzysztof kuhy Rudnicki Date: Thu, 12 Nov 2026 09:56:07 +0100 Subject: [PATCH] Desktop adopts the E2E passphrase once The wrapper serves ~/.config/crdt-sync/password once on /sync-e2e-passphrase; the web app stores it in its keystore, so desktop todo never asks. Android still unlocks via the lock tile. Todo: fda71ed1 Co-Authored-By: Claude Opus 5.5 --- lib/desktop/wrapper_server.dart | 11 +++ lib/desktop/wrapper_sync_routes.dart | 19 ++++++ lib/sync/e2e_passphrase_source.dart | 38 +++++++++++ lib/sync/run_sync.dart | 7 +- pubspec.yaml | 2 +- test/wrapper_server_e2e_passphrase_test.dart | 72 ++++++++++++++++++++ 6 files changed, 144 insertions(+), 5 deletions(-) create mode 100644 lib/sync/e2e_passphrase_source.dart create mode 100644 test/wrapper_server_e2e_passphrase_test.dart diff --git a/lib/desktop/wrapper_server.dart b/lib/desktop/wrapper_server.dart index 6e64c4d..0fd5b3c 100644 --- a/lib/desktop/wrapper_server.dart +++ b/lib/desktop/wrapper_server.dart @@ -7,6 +7,11 @@ import 'package:path/path.dart' as p; import 'package:todo/desktop/static_content_type.dart'; import 'package:todo/desktop/wrapper_image_routes.dart'; +import 'package:todo/sync/e2e_passphrase_source.dart' + show kSyncE2ePassphrasePath; + +export 'package:todo/sync/e2e_passphrase_source.dart' + show kSyncE2ePassphrasePath; export 'wrapper_paths.dart'; @@ -86,6 +91,9 @@ class WrapperServer { /// wrappers also serve, which stays opt-in. bool _todoCredentialsServed = false; + /// Whether [kSyncE2ePassphrasePath] has served the passphrase yet. + bool _e2ePassphraseServed = false; + /// `todo`'s own seeded Firebase session, written by /// `crdt-sync/tool/seed_session.py --app todo` in the same /// `{id_token, refresh_token, expires_at}` shape every other app's @@ -184,6 +192,9 @@ class WrapperServer { if (path == kSyncCredentialsPath) { return await _syncCredentials(request); } + if (path == kSyncE2ePassphrasePath) { + return await _syncE2ePassphrase(request); + } if (await images.handle(request)) return; return await _static(request, path); } diff --git a/lib/desktop/wrapper_sync_routes.dart b/lib/desktop/wrapper_sync_routes.dart index d55bb0c..c69de85 100644 --- a/lib/desktop/wrapper_sync_routes.dart +++ b/lib/desktop/wrapper_sync_routes.dart @@ -121,4 +121,23 @@ extension _SyncRoutes on WrapperServer { // through must remain retryable rather than burning the one serve. _todoCredentialsServed = true; } + + /// Serves the shared crdt-sync password as the E2E passphrase, once per + /// wrapper process, like [_syncCredentials]: the web app stores it in its + /// keystore on first sync and never asks for it. + Future _syncE2ePassphrase(HttpRequest request) async { + final passwordFile = File(p.join(syncConfigDir, 'password')); + if (_e2ePassphraseServed || !passwordFile.existsSync()) { + request.response.statusCode = HttpStatus.notFound; + return; + } + final text = (await passwordFile.readAsString()).trim(); + if (text.isEmpty) { + request.response.statusCode = HttpStatus.notFound; + return; + } + request.response.headers.contentType = ContentType.text; + request.response.write(text); + _e2ePassphraseServed = true; + } } diff --git a/lib/sync/e2e_passphrase_source.dart b/lib/sync/e2e_passphrase_source.dart new file mode 100644 index 0000000..396dbfc --- /dev/null +++ b/lib/sync/e2e_passphrase_source.dart @@ -0,0 +1,38 @@ +/// Where todo gets the shared crdt-sync passphrase its E2E key comes from. +/// +/// The keystore first. On the desktop (a web build behind the local +/// wrapper) an empty keystore adopts the wrapper's one-shot +/// `/sync-e2e-passphrase` route, which serves `~/.config/crdt-sync/password`, +/// so the desktop app never asks. Android has no wrapper (`Uri.base` is +/// `file:///`) and is unlocked through the settings lock tile instead. +library; + +import 'package:crdt_sync_flutter/crdt_sync_flutter.dart' + show kE2ePassphraseKey, kSecureStorage, saveE2ePassphrase; +import 'package:http/http.dart' as http; + +/// Path of the wrapper's one-shot passphrase route. +const kSyncE2ePassphrasePath = '/sync-e2e-passphrase'; + +/// Returns the passphrase, adopting the wrapper's once; null when none. +Future readE2ePassphrase({Uri? base, http.Client? client}) async { + final stored = await kSecureStorage.read(key: kE2ePassphraseKey); + if (stored != null && stored.isNotEmpty) return stored; + final origin = base ?? Uri.base; + if (origin.scheme != 'http' && origin.scheme != 'https') return null; + final httpClient = client ?? http.Client(); + try { + final response = await httpClient.get( + origin.resolve(kSyncE2ePassphrasePath), + ); + final text = response.body.trim(); + if (response.statusCode != 200 || text.isEmpty) return null; + await saveE2ePassphrase(text); + return text; + } on Object { + // No wrapper, or it already served: this device is simply locked. + return null; + } finally { + if (client == null) httpClient.close(); + } +} diff --git a/lib/sync/run_sync.dart b/lib/sync/run_sync.dart index 7d5028b..8d89934 100644 --- a/lib/sync/run_sync.dart +++ b/lib/sync/run_sync.dart @@ -7,13 +7,13 @@ library; import 'package:crdt_sync/crdt_sync.dart'; -import 'package:crdt_sync_flutter/crdt_sync_flutter.dart' - show kE2ePassphraseKey, kSecureStorage, keystoreKeyCache; +import 'package:crdt_sync_flutter/crdt_sync_flutter.dart' show keystoreKeyCache; import 'package:flutter/foundation.dart' show ValueNotifier, visibleForTesting; import 'package:http/http.dart' as http; import 'package:todo/analytics/analytics_service.dart'; import 'package:todo/data/app_settings.dart'; import 'package:todo/data/note_repository.dart'; +import 'package:todo/sync/e2e_passphrase_source.dart'; import 'package:todo/sync/firebase_backend.dart'; import 'package:todo/sync/sync_service.dart'; import 'package:todo/sync/sync_settings.dart'; @@ -94,8 +94,7 @@ Future runSync( final e2e = await openEncryptedStore( store: backend, oldPrefix: service.notesDir, - readPassphrase: - readPassphrase ?? () => kSecureStorage.read(key: kE2ePassphraseKey), + readPassphrase: readPassphrase ?? readE2ePassphrase, params: e2eArgon2Params, // Argon2id once per device, not once per tick. keyCache: keystoreKeyCache(service.notesDir), diff --git a/pubspec.yaml b/pubspec.yaml index b0db8b9..70098f1 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -16,7 +16,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev # https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html # In Windows, build-name is used as the major, minor, and patch parts # of the product and file versions while build-number is used as the build suffix. -version: 1.0.85+1 +version: 1.0.86+1 environment: sdk: ^3.12.2 diff --git a/test/wrapper_server_e2e_passphrase_test.dart b/test/wrapper_server_e2e_passphrase_test.dart new file mode 100644 index 0000000..78d1daf --- /dev/null +++ b/test/wrapper_server_e2e_passphrase_test.dart @@ -0,0 +1,72 @@ +import 'dart:io'; + +import 'package:flutter_test/flutter_test.dart'; +import 'package:http/http.dart' as http; +import 'package:http/testing.dart' as http_testing; +import 'package:todo/desktop/wrapper_server.dart'; +import 'package:todo/sync/e2e_passphrase_source.dart'; + +import 'fake_secure_storage.dart'; +import 'wrapper_server_harness.dart'; + +void main() { + final h = WrapperHarness()..install(); + + group('wrapper e2e passphrase route', () { + test('serves the shared password once, then 404s', () async { + final origin = await h.enabledOrigin({'password': 'shared-pw\n'}); + final url = Uri.parse('$origin$kSyncE2ePassphrasePath'); + final first = await http.get(url); + expect(first.statusCode, HttpStatus.ok); + expect(first.body, 'shared-pw'); + expect((await http.get(url)).statusCode, HttpStatus.notFound); + }); + + test('is 404 without a password file', () async { + final origin = await h.enabledOrigin({}); + final url = Uri.parse('$origin$kSyncE2ePassphrasePath'); + expect((await http.get(url)).statusCode, HttpStatus.notFound); + }); + }); + + group('readE2ePassphrase', () { + setUp(() { + TestWidgetsFlutterBinding.ensureInitialized(); + installFakeSecureStorage(initial: {'crdt_sync_e2e_passphrase': ''}); + }); + + test('adopts the wrapper passphrase into the keystore once', () async { + var calls = 0; + final client = http_testing.MockClient((request) async { + calls++; + return http.Response('from-wrapper', 200); + }); + final base = Uri.parse('http://127.0.0.1:1/'); + expect( + await readE2ePassphrase(base: base, client: client), + 'from-wrapper', + ); + expect( + await readE2ePassphrase(base: base, client: client), + 'from-wrapper', + ); + expect(calls, 1); + }); + + test('no wrapper (file:// on Android) is just no passphrase', () async { + expect(await readE2ePassphrase(base: Uri.parse('file:///')), isNull); + }); + + test('a failing wrapper is no passphrase, not a crash', () async { + final client = http_testing.MockClient( + (_) async => http.Response('', 404), + ); + final base = Uri.parse('http://127.0.0.1:1/'); + expect(await readE2ePassphrase(base: base, client: client), isNull); + final down = http_testing.MockClient( + (_) async => throw const SocketException('x'), + ); + expect(await readE2ePassphrase(base: base, client: down), isNull); + }); + }); +}