Skip to content

Commit dcced2c

Browse files
wesbillmanBrain
andauthored
fix(mobile): preserve saved identity lookup after enumeration fails (#8470)
An account-scoped saved identity could fail to open when secure-storage enumeration succeeded while showing choices, then failed before selection. Retain the discovered storage location and reread/validate that exact record before falling back to inventory lookup. Only public location hints are retained; signing material is read fresh. Follow-up accepted at merge of #8375: #8375 (review). Validation: existing self-hosted widget journey now injects enumeration failure after choices load and verifies the same key is admitted and persisted. Three focused files passed: 37 tests with hosted domains configured; 28 passed and 9 hosted-only skips with default configuration. Changed-file analysis passed. In an isolated clone, removing the location lookup failed that journey; control and restored runs passed. Self-review found no blocking defect. Draft acceptance gate: native/device behavior and human acceptance are not established by widget fixtures. On a disposable saved identity, enter a self-hosted community link, choose the identity, and confirm admission preserves it; cancel/retry as well. Hosted live-backend acceptance remains separate and pending backend availability. No full local suite was manually run; mandatory hooks/CI provide broader coverage. Signed-off-by: Wes <[email protected]> Co-authored-by: Brain <1a02c72794dcd0f07058a353bc3a81f4028b8c77c92c87fce6d5c8b85970a20b@feedback.communities.buzz.xyz>
1 parent 91b1cc4 commit dcced2c

3 files changed

Lines changed: 41 additions & 1 deletion

File tree

‎mobile/lib/shared/hosted/account_identity_store.dart‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ class AccountIdentityStore {
118118
'buzz_account_identity_${HostedEnvironment.origin.host.replaceAll('.', '_')}_v1';
119119
static final _queue = SerialQueue();
120120
final FlutterSecureStorage _storage;
121+
// Public lookup hints from the choices shown by this store. Always reread
122+
// and validate the record; never retain signing material across selection.
123+
final _savedLocations = <String, String>{};
121124

122125
Future<T> _serialized<T>(Future<T> Function() operation) {
123126
final result = _queue.run(() => operation());
@@ -215,10 +218,17 @@ class AccountIdentityStore {
215218
Future<List<AccountIdentitySnapshot>> identities() => _serialized(() async {
216219
final keys = await _identityKeys();
217220
final identities = <String, AccountIdentitySnapshot>{};
221+
final locations = <String, String>{};
218222
for (final key in keys) {
219223
final record = await _atKey(key)._read();
220-
if (record != null) identities[record.pubkey] = record.snapshot;
224+
if (record != null) {
225+
identities[record.pubkey] = record.snapshot;
226+
locations[record.pubkey] = key;
227+
}
221228
}
229+
_savedLocations
230+
..clear()
231+
..addAll(locations);
222232
return identities.values.toList();
223233
});
224234

@@ -236,6 +246,11 @@ class AccountIdentityStore {
236246

237247
Future<String> _savedNsec(String pubkey) => _serialized(() async {
238248
try {
249+
final location = _savedLocations[pubkey];
250+
if (location != null) {
251+
final record = await _atKey(location)._read();
252+
if (record?.pubkey == pubkey) return record!.nsec;
253+
}
239254
final keys = await _identityKeys(pubkey: pubkey);
240255
for (final key in keys) {
241256
final record = await _atKey(key)._read();

‎mobile/test/features/onboarding/hosted_journeys_test.dart‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,13 +553,17 @@ void main() {
553553
'my-community.example',
554554
);
555555
await f.tap(tester, 'Continue');
556+
// Choices loaded successfully; enumeration breaks before selection.
557+
// The account-scoped key remains directly readable throughout admission.
558+
f.identityStorage.failEnumeration = true;
556559
await f.tap(tester, 'Saved identity');
557560
final saved = (await f.storage.loadAll()).single;
558561
expect(saved.relayUrl, 'wss://my-community.example');
559562
expect(saved.nsec, f.keys.nsec);
560563
expect(await f.storage.loadActiveId(), saved.id);
561564
expect(f.probes, hasLength(1));
562565
expect(f.sent('/claim'), isEmpty);
566+
f.identityStorage.failEnumeration = false;
563567
expect(
564568
(await const FlutterSecureStorage().readAll()).keys.where(
565569
(key) => key.startsWith('buzz_pending_self_hosted_'),

‎mobile/test/helpers/hosted_journey_fixture.dart‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class HostedJourneyFixture {
4040
final secondKeys = nostr.Keys('2'.padLeft(64, '0'));
4141
AccountIdentityStore get store => AccountIdentityStore(subject: subject);
4242
final storage = CommunityStorage();
43+
final identityStorage = JourneyIdentityStorage();
4344
final requests = <http.Request>[];
4445
Future<void> Function()? beforeBrowserReturn;
4546
Future<void> Function()? beforeEnrollmentReturn;
@@ -348,6 +349,7 @@ class HostedJourneyFixture {
348349
),
349350
selfHostedEntryProvider.overrideWithValue(
350351
SelfHostedEntry(
352+
accountStore: AccountIdentityStore(storage: identityStorage),
351353
probe: (community) async {
352354
probes.add(community);
353355
if (community.pubkey == refusedSelfHostedPubkey) {
@@ -452,6 +454,25 @@ class HostedJourneyFixture {
452454
}
453455
}
454456

457+
/// Keeps direct reads working when an unrelated secure-storage entry breaks
458+
/// enumeration, as can happen on Android after the identity picker is shown.
459+
class JourneyIdentityStorage extends FlutterSecureStorage {
460+
bool failEnumeration = false;
461+
462+
@override
463+
Future<Map<String, String>> readAll({
464+
AppleOptions? iOptions,
465+
AndroidOptions? aOptions,
466+
LinuxOptions? lOptions,
467+
WebOptions? webOptions,
468+
AppleOptions? mOptions,
469+
WindowsOptions? wOptions,
470+
}) async {
471+
if (failEnumeration) throw StateError('unrelated entry cannot decrypt');
472+
return super.readAll();
473+
}
474+
}
475+
455476
class _NoChannels extends ChannelsNotifier {
456477
@override
457478
Future<List<Channel>> build() async => [];

0 commit comments

Comments
 (0)