From d63c5680b73227ff94470e590b2484bb2b7d41ea Mon Sep 17 00:00:00 2001 From: Efe Unverdi Date: Fri, 4 Sep 2026 23:06:56 +0300 Subject: [PATCH] fix(iOS): arm TLS before connectToHost so connect is never announced pre-handshake `connect:` called `startTLS:` after `connectToHost:`, and `startTLS:` set `_tls` only at its end, after building the TLS settings. With client-certificate auth that build imports a PKCS#12 and touches the keychain, which can outlast a LAN connect. `didConnectToHost:` runs on the delegate queue (the main queue) and is gated on `if (!_tls)`, so a connect that completed first announced a plain `connect` for a socket whose handshake had not begun. `connectTLS` wires `secureConnect` to that event, so callers were told the socket was secure and anything written from the callback went out ahead of the TLS packet. Build the settings and set `_tls` before `connectToHost:`, then queue `startTLS:` on the socket right behind the connect; GCDAsyncSocket starts the handshake once connected. `_tls` is cleared again if the connect never started. Also clear `_connecting` on the plain-connect path, from #217: without it a later `new TLSSocket(socket)` upgrade announces a second `connect` from `socketDidSecure`. Co-Authored-By: Claude Opus 5 --- ios/TcpSocketClient.m | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/ios/TcpSocketClient.m b/ios/TcpSocketClient.m index a3df69c..9186a9e 100644 --- a/ios/TcpSocketClient.m +++ b/ios/TcpSocketClient.m @@ -74,6 +74,7 @@ - (id)initWithClientId:(NSNumber *)clientID andConfig:(id)aDelegate andSocket:(GCDAsyncSocket *)tcpSocket andServer:(NSNumber *)serverID; +- (NSDictionary *)tlsSettingsForOptions:(NSDictionary *)tlsOptions; @end @@ -147,6 +148,14 @@ - (BOOL)connect:(NSString *)host int connectTimeout = options[@"connectTimeout"] ? [options[@"connectTimeout"] intValue] / 1000 : -1; _host = host; + // Arm TLS before the connect starts. Building the client identity touches the keychain and can + // outlast a LAN connect, and `didConnectToHost` reads `_tls` to decide whether to announce a + // plain connect: set after `connectToHost`, it reported a socket whose handshake had not begun. + NSDictionary *tlsSettings = nil; + if (tlsOptions) { + tlsSettings = [self tlsSettingsForOptions:tlsOptions]; + _tls = true; + } _connecting = true; if (!localAddress && !localPort) { result = [_tcpSocket connectToHost:host onPort:port withTimeout:connectTimeout error:error]; @@ -163,8 +172,11 @@ - (BOOL)connect:(NSString *)host withTimeout:connectTimeout error:error]; } - if (result && tlsOptions) { - [self startTLS:tlsOptions]; + if (result && tlsSettings) { + // Queued right behind the connect; GCDAsyncSocket starts the handshake once connected. + [_tcpSocket startTLS:tlsSettings]; + } else if (!result) { + _tls = false; } return result; } @@ -186,6 +198,12 @@ - (ResolvableOption *)getResolvableOption:(NSDictionary *)tlsOptions - (void)startTLS:(NSDictionary *)tlsOptions { if (_tls) return; + NSDictionary *settings = [self tlsSettingsForOptions:tlsOptions]; + _tls = true; + [_tcpSocket startTLS:settings]; +} + +- (NSDictionary *)tlsSettingsForOptions:(NSDictionary *)tlsOptions { NSMutableDictionary *settings = [NSMutableDictionary dictionary]; _resolvableCaCert = [self getResolvableOption:tlsOptions forKey:@"ca"]; BOOL checkValidity = (tlsOptions[@"rejectUnauthorized"] @@ -249,8 +267,7 @@ - (void)startTLS:(NSDictionary *)tlsOptions { } //RCTLogWarn(@"startTLS: Final settings: %@", settings); - _tls = true; - [_tcpSocket startTLS:settings]; + return settings; } - (NSDictionary *)getAddress { @@ -572,9 +589,12 @@ - (void)socket:(GCDAsyncSocket *)sock return; } - // Show up if SSL handsake is done + // Show up if SSL handshake is done if (!_tls) { + // A later `startTLS` upgrade otherwise finds `_connecting` still set and + // announces a second connect from `socketDidSecure`. [_clientDelegate onConnect:self]; + _connecting = false; } [sock readDataWithTimeout:-1 tag:_id.longValue]; }