Skip to content

Commit 1fc9b6e

Browse files
wesbillmanPinky
andcommitted
fix terminal relay auth rejection handling
Reject malformed saved signing keys during restore and treat explicit NIP-42 rejection as terminal while preserving retries for transient connectivity errors. Co-authored-by: Pinky <44b8e82baa6e0e254e0208d68f335c283c94e7b78dd1fa10d5a49d3f13dd0435@sprout-oss.stage.blox.sqprod.co> Signed-off-by: Wes <[email protected]>
1 parent 608048b commit 1fc9b6e

5 files changed

Lines changed: 162 additions & 10 deletions

File tree

‎mobile/lib/shared/auth/auth_provider.dart‎

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'package:hooks_riverpod/hooks_riverpod.dart';
2+
import 'package:nostr/nostr.dart' as nostr;
23

34
import '../workspace/workspace.dart';
45
import '../workspace/workspace_provider.dart';
@@ -26,18 +27,26 @@ class AuthNotifier extends AsyncNotifier<AuthState> {
2627
return const AuthState(status: AuthStatus.unauthenticated);
2728
}
2829

29-
final activeId = await storage.loadActiveId();
30-
final Workspace active;
31-
if (activeId != null && workspaces.any((w) => w.id == activeId)) {
32-
active = workspaces.firstWhere((w) => w.id == activeId);
33-
} else {
34-
// activeId is null or points to a workspace that no longer exists.
35-
// Fall back to first workspace and persist the choice.
36-
active = workspaces.first;
30+
var activeId = await storage.loadActiveId();
31+
while (workspaces.isNotEmpty) {
32+
final active = activeId != null && workspaces.any((w) => w.id == activeId)
33+
? workspaces.firstWhere((w) => w.id == activeId)
34+
: workspaces.first;
3735
await storage.saveActiveId(active.id);
36+
37+
if (_hasValidNsec(active.nsec)) {
38+
return AuthState(status: AuthStatus.authenticated, workspace: active);
39+
}
40+
41+
await storage.remove(active.id);
42+
workspaces.removeWhere((workspace) => workspace.id == active.id);
43+
activeId = null;
44+
ref.invalidate(workspaceListProvider);
45+
ref.invalidate(activeWorkspaceProvider);
3846
}
3947

40-
return AuthState(status: AuthStatus.authenticated, workspace: active);
48+
await storage.clearActiveId();
49+
return const AuthState(status: AuthStatus.unauthenticated);
4150
}
4251

4352
/// Reload the active workspace after a startup error.
@@ -91,6 +100,17 @@ class AuthNotifier extends AsyncNotifier<AuthState> {
91100
}
92101
}
93102

103+
bool _hasValidNsec(String? nsec) {
104+
if (nsec == null || nsec.isEmpty) return false;
105+
try {
106+
final decoded = nostr.Nip19.decode(payload: nsec);
107+
return decoded.prefix == nostr.Nip19Prefix.nsec &&
108+
decoded.data.length == 64;
109+
} catch (_) {
110+
return false;
111+
}
112+
}
113+
94114
final authProvider = AsyncNotifierProvider<AuthNotifier, AuthState>(
95115
AuthNotifier.new,
96116
);

‎mobile/lib/shared/relay/relay_session.dart‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,12 @@ class RelaySessionNotifier extends Notifier<SessionState> {
360360
_eventBuffer.clear();
361361
_flushTimer?.cancel();
362362
_flushTimer = null;
363+
if (error is RelayAuthRejectedException) {
364+
_reconnectTimer?.cancel();
365+
state = const SessionState(status: SessionStatus.disconnected);
366+
unawaited(ref.read(authProvider.notifier).signOut());
367+
return;
368+
}
363369
_scheduleReconnect();
364370
}
365371

‎mobile/lib/shared/relay/relay_socket.dart‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,20 @@ import 'nostr_models.dart';
1414
/// Does NOT handle reconnection — that is [RelaySessionNotifier]'s job.
1515
enum SocketState { disconnected, connecting, authenticating, connected }
1616

17+
class RelayAuthRejectedException implements Exception {
18+
final String message;
19+
20+
const RelayAuthRejectedException(this.message);
21+
22+
@override
23+
String toString() => 'Relay authentication rejected: $message';
24+
}
25+
26+
Exception classifyRelayAuthFailure(String message) {
27+
if (message.startsWith('error:')) return Exception(message);
28+
return RelayAuthRejectedException(message);
29+
}
30+
1731
class RelaySocket {
1832
final String _wsUrl;
1933
final String? _nsec;
@@ -222,7 +236,7 @@ class RelaySocket {
222236
final message = data.length > 3
223237
? data[3] as String
224238
: 'Auth rejected by relay';
225-
_failAuth(Exception(message));
239+
_failAuth(classifyRelayAuthFailure(message));
226240
}
227241
return;
228242
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import 'package:flutter_test/flutter_test.dart';
2+
import 'package:hooks_riverpod/hooks_riverpod.dart';
3+
import 'package:nostr/nostr.dart' as nostr;
4+
import 'package:buzz/shared/auth/auth_provider.dart';
5+
import 'package:buzz/shared/workspace/workspace.dart';
6+
import 'package:buzz/shared/workspace/workspace_provider.dart';
7+
import 'package:buzz/shared/workspace/workspace_storage.dart';
8+
9+
import '../workspace/workspace_storage_test.dart';
10+
11+
void main() {
12+
test(
13+
'removes an invalid saved workspace instead of authenticating',
14+
() async {
15+
final storage = WorkspaceStorage(secure: FakeSecureStorage());
16+
final invalid = Workspace.create(
17+
name: 'Invalid',
18+
relayUrl: 'https://relay.example',
19+
nsec: 'not-an-nsec',
20+
);
21+
await storage.save(invalid);
22+
await storage.saveActiveId(invalid.id);
23+
final container = ProviderContainer(
24+
overrides: [workspaceStorageProvider.overrideWithValue(storage)],
25+
);
26+
addTearDown(container.dispose);
27+
28+
final auth = await container.read(authProvider.future);
29+
30+
expect(auth.status, AuthStatus.unauthenticated);
31+
expect(await storage.loadAll(), isEmpty);
32+
expect(await storage.loadActiveId(), isNull);
33+
},
34+
);
35+
36+
test('falls through to the next valid saved workspace', () async {
37+
final storage = WorkspaceStorage(secure: FakeSecureStorage());
38+
final invalid = Workspace.create(
39+
name: 'Invalid',
40+
relayUrl: 'https://invalid.example',
41+
);
42+
final valid = Workspace.create(
43+
name: 'Valid',
44+
relayUrl: 'https://valid.example',
45+
nsec: nostr.Keys.generate().nsec,
46+
);
47+
await storage.save(invalid);
48+
await storage.save(valid);
49+
await storage.saveActiveId(invalid.id);
50+
final container = ProviderContainer(
51+
overrides: [workspaceStorageProvider.overrideWithValue(storage)],
52+
);
53+
addTearDown(container.dispose);
54+
55+
final auth = await container.read(authProvider.future);
56+
57+
expect(auth.status, AuthStatus.authenticated);
58+
expect(auth.workspace?.id, valid.id);
59+
expect(await storage.loadActiveId(), valid.id);
60+
});
61+
}

‎mobile/test/shared/relay/relay_session_test.dart‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import 'package:http/http.dart' as http;
77
import 'package:http/testing.dart' as http_testing;
88
import 'package:nostr/nostr.dart' as nostr;
99
import 'package:pointycastle/digests/sha256.dart';
10+
import 'package:buzz/shared/auth/auth_provider.dart';
1011
import 'package:buzz/shared/relay/relay.dart';
1112

1213
void main() {
@@ -153,6 +154,43 @@ void main() {
153154
expect(session.state.reconnectAttempt, 1);
154155
});
155156

157+
test('classifies relay internal auth errors as transient', () {
158+
expect(
159+
classifyRelayAuthFailure(
160+
'error: internal error checking restriction state',
161+
),
162+
isNot(isA<RelayAuthRejectedException>()),
163+
);
164+
expect(
165+
classifyRelayAuthFailure('restricted: access revoked'),
166+
isA<RelayAuthRejectedException>(),
167+
);
168+
});
169+
170+
test(
171+
'stops reconnecting and signs out after explicit auth rejection',
172+
() async {
173+
final session = RelaySessionNotifier();
174+
final auth = _FakeAuthNotifier();
175+
final container = ProviderContainer(
176+
overrides: [
177+
relaySessionProvider.overrideWith(() => session),
178+
authProvider.overrideWith(() => auth),
179+
],
180+
);
181+
addTearDown(container.dispose);
182+
container.read(relaySessionProvider);
183+
184+
session.debugHandleDisconnected(
185+
const RelayAuthRejectedException('invalid key'),
186+
);
187+
await Future<void>.delayed(Duration.zero);
188+
189+
expect(session.state.status, SessionStatus.disconnected);
190+
expect(auth.signOutCount, 1);
191+
},
192+
);
193+
156194
test('does not schedule reconnects after background disconnect', () {
157195
final session = RelaySessionNotifier();
158196
final container = ProviderContainer(
@@ -258,6 +296,19 @@ void main() {
258296
);
259297
}
260298

299+
class _FakeAuthNotifier extends AuthNotifier {
300+
int signOutCount = 0;
301+
302+
@override
303+
Future<AuthState> build() async =>
304+
const AuthState(status: AuthStatus.unauthenticated);
305+
306+
@override
307+
Future<void> signOut() async {
308+
signOutCount++;
309+
}
310+
}
311+
261312
const _channelId = '11111111-1111-4111-8111-111111111111';
262313

263314
class _FakeRelayConfigNotifier extends RelayConfigNotifier {

0 commit comments

Comments
 (0)