Skip to content

Commit 067866a

Browse files
committed
fix(mobile): fail pairing when relay connect or auth fails
RelaySocket.connect() reports handshake and NIP-42 failures through onDisconnected and then returns. Credential validation treated that return as success and stored the community anyway. Fixes block#8043 Signed-off-by: Bartok9 <[email protected]>
1 parent bd1ff00 commit 067866a

2 files changed

Lines changed: 80 additions & 1 deletion

File tree

‎mobile/lib/features/pairing/pairing_provider.dart‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -913,16 +913,27 @@ class PairingNotifier extends Notifier<PairingState> {
913913
final scheme = uri.scheme == 'https' ? 'wss' : 'ws';
914914
final wsUrl = uri.replace(scheme: scheme).toString();
915915

916+
// connect() reports handshake and auth failures via onDisconnected and
917+
// then returns. Pairing must not store credentials when that happens (#8043).
918+
Object? failure;
916919
final socket = _validationSocketFactory(
917920
wsUrl: wsUrl,
918921
nsec: nsec,
919922
onMessage: (_) {},
920923
onConnected: () {},
921-
onDisconnected: (_) {},
924+
onDisconnected: (error) {
925+
failure ??= error ?? Exception('Could not connect to relay');
926+
},
922927
);
923928
_validationSocket = socket;
924929
try {
925930
await socket.connect().timeout(const Duration(seconds: 8));
931+
if (failure != null) {
932+
throw failure!;
933+
}
934+
if (socket.state != SocketState.connected) {
935+
throw Exception('Could not connect to relay');
936+
}
926937
} finally {
927938
if (identical(_validationSocket, socket)) _validationSocket = null;
928939
await socket.disconnect();

‎mobile/test/features/pairing/pairing_provider_test.dart‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,50 @@ void main() {
7373
},
7474
);
7575

76+
test('legacy pairing fails when relay connect or auth fails', () async {
77+
final socket = _FailingValidationSocket(
78+
Exception('restricted: not a relay member'),
79+
);
80+
final auth = FakeAuthNotifier();
81+
final notifier = PairingNotifier(
82+
validationSocketFactory:
83+
({
84+
required wsUrl,
85+
required nsec,
86+
required onMessage,
87+
required onConnected,
88+
required onDisconnected,
89+
}) {
90+
socket.onDisconnected = onDisconnected;
91+
return socket;
92+
},
93+
);
94+
container = ProviderContainer(
95+
overrides: [
96+
pairingProvider.overrideWith(() => notifier),
97+
authProvider.overrideWith(() => auth),
98+
],
99+
);
100+
final input = base64Url.encode(
101+
utf8.encode(
102+
jsonEncode({
103+
'relayUrl': 'https://relay.example',
104+
'nsec': 'rejected-key',
105+
}),
106+
),
107+
);
108+
109+
await container.read(pairingProvider.notifier).pair(input);
110+
111+
expect(auth.lastCommunity, isNull);
112+
expect(container.read(pairingProvider).status, PairingStatus.error);
113+
expect(
114+
container.read(pairingProvider).errorMessage,
115+
contains('not a relay member'),
116+
);
117+
expect(socket.disposed, isTrue);
118+
});
119+
76120
test('starts in idle state', () {
77121
container = createContainer();
78122
final state = container.read(pairingProvider);
@@ -1396,6 +1440,30 @@ class _ControllableSocket extends PairingSocket {
13961440
}
13971441
}
13981442

1443+
class _FailingValidationSocket extends RelaySocket {
1444+
_FailingValidationSocket(this.error)
1445+
: super(
1446+
wsUrl: 'wss://relay.example',
1447+
nsec: null,
1448+
onMessage: (_) {},
1449+
onConnected: () {},
1450+
onDisconnected: (_) {},
1451+
);
1452+
1453+
final Object error;
1454+
void Function(Object? error)? onDisconnected;
1455+
bool disposed = false;
1456+
1457+
@override
1458+
Future<void> connect() async {
1459+
// Match RelaySocket: report the failure, then return instead of throwing.
1460+
onDisconnected?.call(error);
1461+
}
1462+
1463+
@override
1464+
Future<void> disconnect() async => disposed = true;
1465+
}
1466+
13991467
class _PendingValidationSocket extends RelaySocket {
14001468
_PendingValidationSocket()
14011469
: super(

0 commit comments

Comments
 (0)