Skip to content

Commit cde3773

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 4dea51f commit cde3773

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

937+
// connect() reports handshake and auth failures via onDisconnected and
938+
// then returns. Pairing must not store credentials when that happens (#8043).
939+
Object? failure;
937940
final socket = _validationSocketFactory(
938941
wsUrl: wsUrl,
939942
nsec: nsec,
940943
onMessage: (_) {},
941944
onConnected: () {},
942-
onDisconnected: (_) {},
945+
onDisconnected: (error) {
946+
failure ??= error ?? Exception('Could not connect to relay');
947+
},
943948
);
944949
_validationSocket = socket;
945950
try {
946951
await socket.connect().timeout(const Duration(seconds: 8));
952+
if (failure != null) {
953+
throw failure!;
954+
}
955+
if (socket.state != SocketState.connected) {
956+
throw Exception('Could not connect to relay');
957+
}
947958
} finally {
948959
if (identical(_validationSocket, socket)) _validationSocket = null;
949960
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);
@@ -1457,6 +1501,30 @@ class _ControllableSocket extends PairingSocket {
14571501
}
14581502
}
14591503

1504+
class _FailingValidationSocket extends RelaySocket {
1505+
_FailingValidationSocket(this.error)
1506+
: super(
1507+
wsUrl: 'wss://relay.example',
1508+
nsec: null,
1509+
onMessage: (_) {},
1510+
onConnected: () {},
1511+
onDisconnected: (_) {},
1512+
);
1513+
1514+
final Object error;
1515+
void Function(Object? error)? onDisconnected;
1516+
bool disposed = false;
1517+
1518+
@override
1519+
Future<void> connect() async {
1520+
// Match RelaySocket: report the failure, then return instead of throwing.
1521+
onDisconnected?.call(error);
1522+
}
1523+
1524+
@override
1525+
Future<void> disconnect() async => disposed = true;
1526+
}
1527+
14601528
class _PendingValidationSocket extends RelaySocket {
14611529
_PendingValidationSocket()
14621530
: super(

0 commit comments

Comments
 (0)