Skip to content

Commit 1ce0aeb

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 f0eb557 commit 1ce0aeb

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
@@ -899,16 +899,27 @@ class PairingNotifier extends Notifier<PairingState> {
899899
final scheme = uri.scheme == 'https' ? 'wss' : 'ws';
900900
final wsUrl = uri.replace(scheme: scheme).toString();
901901

902+
// connect() reports handshake and auth failures via onDisconnected and
903+
// then returns. Pairing must not store credentials when that happens (#8043).
904+
Object? failure;
902905
final socket = _validationSocketFactory(
903906
wsUrl: wsUrl,
904907
nsec: nsec,
905908
onMessage: (_) {},
906909
onConnected: () {},
907-
onDisconnected: (_) {},
910+
onDisconnected: (error) {
911+
failure ??= error ?? Exception('Could not connect to relay');
912+
},
908913
);
909914
_validationSocket = socket;
910915
try {
911916
await socket.connect().timeout(const Duration(seconds: 8));
917+
if (failure != null) {
918+
throw failure!;
919+
}
920+
if (socket.state != SocketState.connected) {
921+
throw Exception('Could not connect to relay');
922+
}
912923
} finally {
913924
if (identical(_validationSocket, socket)) _validationSocket = null;
914925
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
@@ -69,6 +69,50 @@ void main() {
6969
},
7070
);
7171

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

1195+
class _FailingValidationSocket extends RelaySocket {
1196+
_FailingValidationSocket(this.error)
1197+
: super(
1198+
wsUrl: 'wss://relay.example',
1199+
nsec: null,
1200+
onMessage: (_) {},
1201+
onConnected: () {},
1202+
onDisconnected: (_) {},
1203+
);
1204+
1205+
final Object error;
1206+
void Function(Object? error)? onDisconnected;
1207+
bool disposed = false;
1208+
1209+
@override
1210+
Future<void> connect() async {
1211+
// Match RelaySocket: report the failure, then return instead of throwing.
1212+
onDisconnected?.call(error);
1213+
}
1214+
1215+
@override
1216+
Future<void> disconnect() async => disposed = true;
1217+
}
1218+
11511219
class _PendingValidationSocket extends RelaySocket {
11521220
_PendingValidationSocket()
11531221
: super(

0 commit comments

Comments
 (0)