Skip to content

feat: iOS TLS fix — defer startTLS until didConnectToHost#217

Open
manoaxcend wants to merge 6 commits into
Rapsssito:masterfrom
manoaxcend:ios-tls-fix
Open

feat: iOS TLS fix — defer startTLS until didConnectToHost#217
manoaxcend wants to merge 6 commits into
Rapsssito:masterfrom
manoaxcend:ios-tls-fix

Conversation

@manoaxcend

Copy link
Copy Markdown

No description provided.

@manoaxcend

Copy link
Copy Markdown
Author

@Rapsssito , This PR fixes an issue where startTLS: is not called if the connection is established asynchronously in iOS, leading to silent TLS handshake failures.
Request to review & merge the changes.

Problem
In some edge cases—especially with secure connections and client authentication enabled—the call to startTLS: is skipped when the socket connects asynchronously. This results in:

  1. TLS never being initiated.
  2. onConnect being called prematurely.
  3. Silent failure to negotiate a secure connection.

Fix
This PR introduces two changes:

  1. Store TLS Options:
    Save the TLS options in a _tlsOptionsPending dictionary inside connect:.

  2. Invoke startTLS on Connect Completion:
    In socket:didConnectToHost:port:, check if _tlsOptionsPending is set and, if so, call startTLS: there. This ensures TLS is always started after a successful connection.

Tested On

  • iOS 15–17
  • react-native@0.72+
  • Mutual TLS with PEM-encoded RSA 2048-bit certs
  • Both immediate and delayed socket connection cases

Comment thread package.json Outdated
@vricosti

vricosti commented Oct 4, 2025

Copy link
Copy Markdown
Contributor

interesting maybe it will fix one of my issue, I will test it

@Rapsssito

Copy link
Copy Markdown
Owner

@realmano @vricosti, could you test if this PR fixes the issues?

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adjusts the iOS client TLS startup sequence so TLS is initiated only after the underlying TCP connection is established (didConnectToHost), aligning startTLS timing with GCDAsyncSocket’s expected lifecycle.

Changes:

  • Defers client-side startTLS by storing provided TLS options during connect and applying them in socket:didConnectToHost:port:.
  • Introduces _tlsOptionsPending to hold TLS configuration until the TCP connect callback fires.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread ios/TcpSocketClient.m
Comment thread ios/TcpSocketClient.m Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants