mirror of
https://github.com/kuhyx/todo-app.git
synced 2026-11-19 16:54:22 +01:00
Desktop adopts the E2E passphrase once
Some checks are pending
dependency freshness / dependencies on newest stable (push) Waiting to run
file-length / file length <= 250 lines (push) Waiting to run
md-naming / markdown naming convention (push) Waiting to run
release-apk / release (APK) (push) Waiting to run
repo-contract / repo contract (push) Waiting to run
Some checks are pending
dependency freshness / dependencies on newest stable (push) Waiting to run
file-length / file length <= 250 lines (push) Waiting to run
md-naming / markdown naming convention (push) Waiting to run
release-apk / release (APK) (push) Waiting to run
repo-contract / repo contract (push) Waiting to run
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 <noreply@anthropic.com>
This commit is contained in:
parent
4423937f5c
commit
f7e24bbb1a
@ -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);
|
||||
}
|
||||
|
||||
@ -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<void> _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;
|
||||
}
|
||||
}
|
||||
|
||||
38
lib/sync/e2e_passphrase_source.dart
Normal file
38
lib/sync/e2e_passphrase_source.dart
Normal file
@ -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<String?> 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();
|
||||
}
|
||||
}
|
||||
@ -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<SyncRunResult> 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),
|
||||
|
||||
@ -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
|
||||
|
||||
72
test/wrapper_server_e2e_passphrase_test.dart
Normal file
72
test/wrapper_server_e2e_passphrase_test.dart
Normal file
@ -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);
|
||||
});
|
||||
});
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user