From eef279b90956b33a89bf66bb69070c9382c92fd2 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 9 Nov 2026 17:57:48 +0000 Subject: [PATCH 1/7] Stop routing TLS trust checks through the bridge queue; retry the protocol case on fresh clients Run 42427208429 failed "negotiates the expected protocol" on main: its client's first connection went over HTTP/2, and the case then made 10 more calls on that same pooled connection, which can never move to HTTP/3. The simulator's network log shows why the race went to TCP. The server answered the QUIC Initial in 4 ms, but the client's certificate check ("external verify block") took 151 ms, against 3-8 ms for every other connection in the run. URLSession started TCP 100 ms in, and TCP finished first. The check goes through the app because URLSessionBridge's delegate implemented the session trust challenge for every session, even in the plugin, where it only answered performDefaultHandling. CFNetwork then sends every handshake's certificate check (QUIC and TCP alike) to the delegate queue: the bridge's single serial queue, which also carries every call's callbacks. Only a bridge with its own trusted roots (the native tests) now implements the challenge, in a TrustingDelegate subclass. Tests: the protocol case now uses a fresh client per attempt, sharing the helper the HTTP/3-only cases already used (harnessOverHttp3, now harnessNegotiating). Each attempt is a client's first call, so on iOS reaching HTTP/3 still shows that http3Origins made it try QUIC first. That covers the strict "first call goes over HTTP/3" case, which is dropped: a single fallback under CI load is logged, not failed. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01BqmpXW1H7XRFuEgBWW3ppW --- README.md | 5 +- example/integration_test/eliza_test.dart | 54 ++++++++----------- .../URLSessionBridge.swift | 33 +++++++----- 3 files changed, 44 insertions(+), 48 deletions(-) diff --git a/README.md b/README.md index a29061b..9167620 100644 --- a/README.md +++ b/README.md @@ -206,8 +206,9 @@ Neither platform finishes a stream on its own while the client still has its req It covers unary calls, errors, deadlines, server streaming, and full-duplex bidi where the server speaks first and finishes first. It also checks the failure classes (`notSent`, `untrusted`) and the negotiated protocol: HTTP/3 on - iOS, and HTTP/2 on Android, since the server's private CA keeps Cronet off QUIC. On iOS a fresh client's first call - must already be HTTP/3: on this local path, a fallback to TCP is a failure. + iOS, and HTTP/2 on Android, since the server's private CA keeps Cronet off QUIC. On iOS a fresh client's first call, + before any Alt-Svc, must reach HTTP/3 within a few fresh clients. A loaded simulator sometimes lets TCP win + URLSession's race even locally, so a single fallback is logged rather than failed. The script starts the server with a 3 s idle timeout and 1 s pings. Over HTTP/3 (iOS) the tests also check two things: a quiet stream outlives the idle timeout, and a frozen path fails an open stream within it. diff --git a/example/integration_test/eliza_test.dart b/example/integration_test/eliza_test.dart index 2f7a994..329a8b6 100644 --- a/example/integration_test/eliza_test.dart +++ b/example/integration_test/eliza_test.dart @@ -9,6 +9,7 @@ import 'package:connect_native_http_example/gen/connectrpc/eliza/v1/eliza.pb.dar import 'package:connectrpc/connect.dart'; import 'package:connectrpc/protobuf.dart'; import 'package:connectrpc/protocol/connect.dart' as connect_protocol; +import 'package:flutter/foundation.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:integration_test/integration_test.dart'; @@ -89,26 +90,31 @@ Future<(StreamController, StreamIterator)> op return (requests, responses); } -/// A client whose pooled connection to the server negotiated HTTP/3, for the cases that only apply over QUIC. +/// A client whose pooled connection to the server negotiated [protocol], such as `HTTP/3.0`. /// /// URLSession races QUIC against TCP for each new connection and keeps whichever connects first, so on a loaded /// device a connection sometimes lands on HTTP/2 even for an HTTP/3 origin. A client keeps reusing the connection it -/// has, so each attempt uses a fresh client, closing the ones that got HTTP/2. -Future harnessOverHttp3() async { +/// has, so each attempt uses a fresh client, closing the ones that got another protocol. Each attempt is that +/// client's first call, before any Alt-Svc: reaching HTTP/3 shows the client tried QUIC because of http3Origins. +Future harnessNegotiating(String protocol) async { final deadline = DateTime.now().add(const Duration(seconds: 5)); final protocols = []; while (true) { final harness = await Harness.create(serverUrl, http3Origins: {Uri.parse(serverUrl)}); await harness.eliza.say(SayRequest(sentence: 'which protocol'), onHeader: harness.recordHeaders); - final protocol = harness.serverProtocols.last; - if (protocol == 'HTTP/3.0') { + final negotiated = harness.serverProtocols.last; + if (negotiated == protocol) { + if (protocols.isNotEmpty) { + // Not a failure, but worth seeing in CI's log: how often a local HTTP/3 connection lost the race to TCP. + debugPrint('negotiated $protocol after ${protocols.length} other connections: $protocols'); + } addTearDown(harness.native.close); return harness; } - protocols.add(protocol); + protocols.add(negotiated); await harness.native.close(); if (DateTime.now().isAfter(deadline)) { - fail('no connection negotiated HTTP/3 in ${protocols.length} attempts: $protocols'); + fail('no connection negotiated $protocol in ${protocols.length} attempts: $protocols'); } } } @@ -227,28 +233,10 @@ void main() { markTestSkipped('EXPECT_PROTOCOL not set'); return; } - // URLSession only moves to HTTP/3 on a new connection after an Alt-Svc response, so keep calling for a while. - final deadline = DateTime.now().add(const Duration(seconds: 5)); - while (DateTime.now().isBefore(deadline) && !harness.serverProtocols.contains(expectedProtocol)) { - await harness.eliza.say(SayRequest(sentence: 'which protocol'), onHeader: harness.recordHeaders); - await Future.delayed(const Duration(milliseconds: 500)); - } - expect( - harness.serverProtocols, - contains(expectedProtocol), - reason: 'server saw ${harness.serverProtocols}; platform reported ${harness.platformProtocols}', - ); - }); - - testCase('the first call to an HTTP/3 origin goes over HTTP/3', (_) async { - if (expectedProtocol != 'HTTP/3.0') { - markTestSkipped('needs EXPECT_PROTOCOL=HTTP/3.0'); - return; - } - // The origin is in http3Origins, so the client tries QUIC from its first connection. Real networks can block or - // slow QUIC and fall back to TCP, but on this local path a fallback means something is wrong. - await harness.eliza.say(SayRequest(sentence: 'first'), onHeader: harness.recordHeaders); - expect(harness.serverProtocols.single, 'HTTP/3.0', reason: 'the first connection fell back to TCP'); + // Fresh clients, since one that connected over HTTP/2 keeps that connection. A loaded simulator sometimes lets + // TCP win the race even on this local path (a QUIC handshake held 150 ms in certificate verification while TCP + // started 100 ms in and finished first), so one fallback isn't a failure; never reaching HTTP/3 is. + await harnessNegotiating(expectedProtocol); }); // The idle-timeout cases freeze the server's UDP path, so they only apply where calls go over HTTP/3. @@ -260,14 +248,14 @@ void main() { return false; } - // Up to 5 s of harnessOverHttp3 on top of the case itself, which waits up to 7 s for the idle timeout. + // Up to 5 s of harnessNegotiating on top of the case itself, which waits up to 7 s for the idle timeout. const http3CaseTimeout = Duration(seconds: 20); testCase('a quiet HTTP/3 stream outlives the idle timeout while the server pings', (_) async { if (skipUnlessHttp3()) { return; } - final h3 = await harnessOverHttp3(); + final h3 = await harnessNegotiating('HTTP/3.0'); final (requests, responses) = await openQuietConverse(h3); expect(h3.serverProtocols.last, 'HTTP/3.0'); await Future.delayed(serverMaxIdle * 2); @@ -283,7 +271,7 @@ void main() { if (skipUnlessHttp3()) { return; } - final h3 = await harnessOverHttp3(); + final h3 = await harnessNegotiating('HTTP/3.0'); final hold = serverMaxIdle * 2 + const Duration(seconds: 1); final res = await h3.eliza.say( SayRequest(sentence: 'sleep:${hold.inMilliseconds}'), @@ -298,7 +286,7 @@ void main() { if (skipUnlessHttp3()) { return; } - final h3 = await harnessOverHttp3(); + final h3 = await harnessNegotiating('HTTP/3.0'); final (requests, responses) = await openQuietConverse(h3); expect(h3.serverProtocols.last, 'HTTP/3.0'); addTearDown(() => admin('/unfreeze')); diff --git a/ios/connect_native_http/Sources/ConnectNativeHttpCore/URLSessionBridge.swift b/ios/connect_native_http/Sources/ConnectNativeHttpCore/URLSessionBridge.swift index 7c17c89..7324c78 100644 --- a/ios/connect_native_http/Sources/ConnectNativeHttpCore/URLSessionBridge.swift +++ b/ios/connect_native_http/Sources/ConnectNativeHttpCore/URLSessionBridge.swift @@ -69,8 +69,11 @@ public final class URLSessionBridge { // URLSession fails a task that receives nothing for this long. Its default of 60 s would end a quiet stream // or a long-running unary call; callers bound calls with deadlines instead. configuration.timeoutIntervalForRequest = 24 * 60 * 60 - let session = URLSession( - configuration: configuration, delegate: Delegate(bridge: self), delegateQueue: self.delegateQueue) + // Only a bridge with its own roots answers trust challenges. A delegate that implements the challenge at all + // gets every TLS handshake's certificate check, QUIC and TCP alike, sent through `queue`, behind every call's + // callbacks; on a loaded device that held a QUIC handshake long enough for the TCP attempt to win the race. + let delegate = self.trustedRoots.isEmpty ? Delegate(bridge: self) : TrustingDelegate(bridge: self) + let session = URLSession(configuration: configuration, delegate: delegate, delegateQueue: self.delegateQueue) let id = self.nextSessionId self.nextSessionId += 1 self.sessions[id] = Session(urlSession: session, http3Origins: Set(origins)) @@ -226,8 +229,7 @@ public final class URLSessionBridge { fileprivate func serverTrust(_ challenge: URLAuthenticationChallenge) -> ( URLSession.AuthChallengeDisposition, URLCredential? ) { - guard !trustedRoots.isEmpty, - challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust, + guard challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust, let trust = challenge.protectionSpace.serverTrust else { return (.performDefaultHandling, nil) @@ -243,21 +245,13 @@ public final class URLSessionBridge { } // URLSession keeps its delegate until the session is invalidated, so this holds the bridge weakly. -private final class Delegate: NSObject, URLSessionDataDelegate { +private class Delegate: NSObject, URLSessionDataDelegate { weak var bridge: URLSessionBridge? init(bridge: URLSessionBridge) { self.bridge = bridge } - func urlSession( - _ session: URLSession, didReceive challenge: URLAuthenticationChallenge, - completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void - ) { - let (disposition, credential) = bridge?.serverTrust(challenge) ?? (.performDefaultHandling, nil) - completionHandler(disposition, credential) - } - func urlSession( _ session: URLSession, task: URLSessionTask, willPerformHTTPRedirection response: HTTPURLResponse, newRequest request: URLRequest, completionHandler: @escaping (URLRequest?) -> Void @@ -287,3 +281,16 @@ private final class Delegate: NSObject, URLSessionDataDelegate { bridge?.didComplete(task, error: error) } } + +// A Delegate that also checks server certificates against the bridge's own roots. URLSession looks the method up by +// its Objective-C selector, hence the explicit name: a subclass doesn't inherit the protocol's @objc inference. +private final class TrustingDelegate: Delegate { + @objc(URLSession:didReceiveChallenge:completionHandler:) + func urlSession( + _ session: URLSession, didReceive challenge: URLAuthenticationChallenge, + completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void + ) { + let (disposition, credential) = bridge?.serverTrust(challenge) ?? (.performDefaultHandling, nil) + completionHandler(disposition, credential) + } +} From 2aac9d2de29de83e30f530b447154e99a391e97c Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 9 Nov 2026 18:03:42 +0000 Subject: [PATCH 2/7] Check certificates against the system trust store only, with no delegate callback URLSessionBridge's delegate no longer implements the authentication challenge at all. URLSession has no other way to add trust anchors (pinning in Info.plist can only narrow the system's trust), so any custom roots mean a callback that every TLS handshake's certificate check waits on, through the bridge's serial queue. The previous commit kept that path for the native XCTests' own CA; this drops it and trustedRoots with it, so the tests exercise the same handshake path the plugin ships. The tests trust the server's CA through the trust store instead: run_integration_tests.sh already adds it to the simulator's keychain before either suite, so the native suite just stops passing SERVER_CA. The macOS swift-core job adds it to the runner's System keychain, and the README's local loop says how, and how to remove it again. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01BqmpXW1H7XRFuEgBWW3ppW --- .github/workflows/ci.yaml | 7 ++- README.md | 9 ++-- .../URLSessionBridge.swift | 51 ++++--------------- .../URLSessionBridgeTests.swift | 10 ++-- run_integration_tests.sh | 2 +- 5 files changed, 28 insertions(+), 51 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 58ce57f..55e2831 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -95,13 +95,18 @@ jobs: echo "test server did not come up" >&2 cat server.log >&2 exit 1 + # The bridge checks certificates against the system trust store only, with no delegate callback, so the + # server's CA (written on its first start, above) has to be in it. The runner is thrown away after the job. + - name: Trust the test server's CA + timeout-minutes: 2 + run: | + sudo security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain test_server/certs/ca.pem - name: Swift tests timeout-minutes: 5 working-directory: ios/connect_native_http env: CONNECT_NATIVE_HTTP_CORE_ONLY: "1" SERVER_URL: https://localhost:8443 - SERVER_CA: ${{ github.workspace }}/test_server/certs/ca.der run: swift test - name: Upload test server log if: failure() diff --git a/README.md b/README.md index 9167620..df29284 100644 --- a/README.md +++ b/README.md @@ -218,11 +218,14 @@ Neither platform finishes a stream on its own while the client still has its req - **iOS (`ios/connect_native_http/Tests`, XCTest):** a write stuck on a full buffer returns once the call is canceled or its connection fails; a redirect of a request with a body is returned, not followed; shutting a session down lets its calls finish and refuses new ones. The script runs them on the simulator. For a quick loop - on the Mac, against a running test server: + on the Mac, against a running test server, the bridge needs the server's CA in the Mac's trust store: it checks + certificates against that store only, with no callback of its own. Anyone with the CA's key, in + `test_server/certs`, can then impersonate any site to your Mac, so remove it afterwards, or prefer the simulator: + `sudo security delete-certificate -c "connect_native_http test CA" /Library/Keychains/System.keychain`. ```bash - cd ios/connect_native_http && CONNECT_NATIVE_HTTP_CORE_ONLY=1 SERVER_URL=https://localhost:8443 \ - SERVER_CA=../../test_server/certs/ca.der swift test + sudo security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain test_server/certs/ca.pem + cd ios/connect_native_http && CONNECT_NATIVE_HTTP_CORE_ONLY=1 SERVER_URL=https://localhost:8443 swift test ``` - **Android (`android/src/androidTest`, instrumented):** writes sent before Cronet is ready go out once it is; a diff --git a/ios/connect_native_http/Sources/ConnectNativeHttpCore/URLSessionBridge.swift b/ios/connect_native_http/Sources/ConnectNativeHttpCore/URLSessionBridge.swift index 7324c78..8a9d2f7 100644 --- a/ios/connect_native_http/Sources/ConnectNativeHttpCore/URLSessionBridge.swift +++ b/ios/connect_native_http/Sources/ConnectNativeHttpCore/URLSessionBridge.swift @@ -10,7 +10,6 @@ public final class URLSessionBridge { } private let emit: ([String: Any]) -> Void - private let trustedRoots: [SecCertificate] // URLSession delegate callbacks and all state below run on this queue, so none of it needs locking. private let queue = DispatchQueue(label: "connect_native_http") @@ -49,13 +48,7 @@ public final class URLSessionBridge { } } - public convenience init(emit: @escaping ([String: Any]) -> Void) { - self.init(trustedRoots: [], emit: emit) - } - - /// `trustedRoots`, when not empty, replaces the system trust store; tests use it for their own CA. - init(trustedRoots: [SecCertificate], emit: @escaping ([String: Any]) -> Void) { - self.trustedRoots = trustedRoots + public init(emit: @escaping ([String: Any]) -> Void) { self.emit = emit } @@ -69,11 +62,8 @@ public final class URLSessionBridge { // URLSession fails a task that receives nothing for this long. Its default of 60 s would end a quiet stream // or a long-running unary call; callers bound calls with deadlines instead. configuration.timeoutIntervalForRequest = 24 * 60 * 60 - // Only a bridge with its own roots answers trust challenges. A delegate that implements the challenge at all - // gets every TLS handshake's certificate check, QUIC and TCP alike, sent through `queue`, behind every call's - // callbacks; on a loaded device that held a QUIC handshake long enough for the TCP attempt to win the race. - let delegate = self.trustedRoots.isEmpty ? Delegate(bridge: self) : TrustingDelegate(bridge: self) - let session = URLSession(configuration: configuration, delegate: delegate, delegateQueue: self.delegateQueue) + let session = URLSession( + configuration: configuration, delegate: Delegate(bridge: self), delegateQueue: self.delegateQueue) let id = self.nextSessionId self.nextSessionId += 1 self.sessions[id] = Session(urlSession: session, http3Origins: Set(origins)) @@ -226,26 +216,18 @@ public final class URLSessionBridge { emit(failure(c.callId, domain: error.domain, code: error.code, message: error.localizedDescription)) } - fileprivate func serverTrust(_ challenge: URLAuthenticationChallenge) -> ( - URLSession.AuthChallengeDisposition, URLCredential? - ) { - guard challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust, - let trust = challenge.protectionSpace.serverTrust - else { - return (.performDefaultHandling, nil) - } - SecTrustSetAnchorCertificates(trust, trustedRoots as CFArray) - SecTrustSetAnchorCertificatesOnly(trust, true) - return SecTrustEvaluateWithError(trust, nil) - ? (.useCredential, URLCredential(trust: trust)) : (.cancelAuthenticationChallenge, nil) - } - // 64 KiB, the bound pair's buffer: how much request body may wait for URLSession to read it. private static let bodyBufferBytes = 64 * 1024 } // URLSession keeps its delegate until the session is invalidated, so this holds the bridge weakly. -private class Delegate: NSObject, URLSessionDataDelegate { +// +// It deliberately has no authentication challenge method: certificates are checked against the system trust store +// only, inside the network stack. A delegate that implements the challenge at all gets every TLS handshake's +// certificate check, QUIC and TCP alike, sent through `queue`, behind every call's callbacks; on a loaded device that +// held a QUIC handshake long enough for URLSession's TCP attempt to win the race. Tests trust their server's CA by +// installing it in the device's (or Mac's) trust store. +private final class Delegate: NSObject, URLSessionDataDelegate { weak var bridge: URLSessionBridge? init(bridge: URLSessionBridge) { @@ -281,16 +263,3 @@ private class Delegate: NSObject, URLSessionDataDelegate { bridge?.didComplete(task, error: error) } } - -// A Delegate that also checks server certificates against the bridge's own roots. URLSession looks the method up by -// its Objective-C selector, hence the explicit name: a subclass doesn't inherit the protocol's @objc inference. -private final class TrustingDelegate: Delegate { - @objc(URLSession:didReceiveChallenge:completionHandler:) - func urlSession( - _ session: URLSession, didReceive challenge: URLAuthenticationChallenge, - completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void - ) { - let (disposition, credential) = bridge?.serverTrust(challenge) ?? (.performDefaultHandling, nil) - completionHandler(disposition, credential) - } -} diff --git a/ios/connect_native_http/Tests/ConnectNativeHttpCoreTests/URLSessionBridgeTests.swift b/ios/connect_native_http/Tests/ConnectNativeHttpCoreTests/URLSessionBridgeTests.swift index 58dedf3..b93117e 100644 --- a/ios/connect_native_http/Tests/ConnectNativeHttpCoreTests/URLSessionBridgeTests.swift +++ b/ios/connect_native_http/Tests/ConnectNativeHttpCoreTests/URLSessionBridgeTests.swift @@ -4,7 +4,8 @@ import XCTest @testable import ConnectNativeHttpCore // Bridge behavior the Dart integration tests can't reach, against test_server/. run_integration_tests.sh starts the -// server and passes its URL as SERVER_URL and its CA certificate (DER) as SERVER_CA. +// server, passes its URL as SERVER_URL and installs its CA in the simulator's trust store; the bridge only uses that +// store. final class URLSessionBridgeTests: XCTestCase { private static let converse = "/connectrpc.eliza.v1.ElizaService/Converse" private static let say = "/connectrpc.eliza.v1.ElizaService/Say" @@ -17,12 +18,11 @@ final class URLSessionBridgeTests: XCTestCase { override func setUpWithError() throws { let env = ProcessInfo.processInfo.environment - guard let url = env["SERVER_URL"], let caPath = env["SERVER_CA"] else { - throw XCTSkip("SERVER_URL and SERVER_CA are not set; run these through run_integration_tests.sh") + guard let url = env["SERVER_URL"] else { + throw XCTSkip("SERVER_URL is not set; run these through run_integration_tests.sh") } serverURL = url - let ca = try XCTUnwrap(SecCertificateCreateWithData(nil, try Data(contentsOf: URL(fileURLWithPath: caPath)) as CFData)) - bridge = URLSessionBridge(trustedRoots: [ca]) { [weak self] event in + bridge = URLSessionBridge { [weak self] event in guard let self else { return } lock.withLock { self.events.append(event) } } diff --git a/run_integration_tests.sh b/run_integration_tests.sh index c84a52d..8cda010 100755 --- a/run_integration_tests.sh +++ b/run_integration_tests.sh @@ -155,7 +155,7 @@ if [[ " $SUITES " == *" native "* ]]; then # when CI has already run it. with_timeout 300 "$ROOT/ios/connect_native_http/build_core_tests.sh" "$ROOT/build/ios_core_tests" # The tests take about a second. - with_timeout 60 env SIMCTL_CHILD_SERVER_URL="$SERVER_URL" SIMCTL_CHILD_SERVER_CA="$ROOT/test_server/certs/ca.der" \ + with_timeout 60 env SIMCTL_CHILD_SERVER_URL="$SERVER_URL" \ xcrun simctl spawn "$DEVICE" \ "$(xcrun --sdk iphonesimulator --show-sdk-platform-path)/Developer/Library/Xcode/Agents/xctest" \ "$ROOT/build/ios_core_tests/Build/Products/Debug-iphonesimulator/ConnectNativeHttpCoreTests.xctest" From 9f0ffe617db484db9c37a8656b70164ebbe9748d Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 9 Nov 2026 18:05:08 +0000 Subject: [PATCH 3/7] Revert the URLSessionBridge trust changes; keep only the test fix The delegate wasn't what held the QUIC handshake. The simulator log shows CFNetwork's "auth completion disp=1" (performDefaultHandling) in the same millisecond it asked the delegate; the 151 ms went to the system's own default trust evaluation, during which the app logged nothing at all for 88 ms: a CPU-starved simulator, not the bridge. The same evaluation took 218 ms at app launch. Restore the delegate, trustedRoots and the native tests' SERVER_CA as they are on main, and the swift-core job without the keychain step. What stays from this branch is the test fix: the protocol case tries fresh clients, since a client that connected over HTTP/2 keeps that connection. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01BqmpXW1H7XRFuEgBWW3ppW --- .github/workflows/ci.yaml | 7 +--- README.md | 9 ++--- .../URLSessionBridge.swift | 38 +++++++++++++++---- .../URLSessionBridgeTests.swift | 10 ++--- run_integration_tests.sh | 2 +- 5 files changed, 41 insertions(+), 25 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 55e2831..58ce57f 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -95,18 +95,13 @@ jobs: echo "test server did not come up" >&2 cat server.log >&2 exit 1 - # The bridge checks certificates against the system trust store only, with no delegate callback, so the - # server's CA (written on its first start, above) has to be in it. The runner is thrown away after the job. - - name: Trust the test server's CA - timeout-minutes: 2 - run: | - sudo security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain test_server/certs/ca.pem - name: Swift tests timeout-minutes: 5 working-directory: ios/connect_native_http env: CONNECT_NATIVE_HTTP_CORE_ONLY: "1" SERVER_URL: https://localhost:8443 + SERVER_CA: ${{ github.workspace }}/test_server/certs/ca.der run: swift test - name: Upload test server log if: failure() diff --git a/README.md b/README.md index df29284..9167620 100644 --- a/README.md +++ b/README.md @@ -218,14 +218,11 @@ Neither platform finishes a stream on its own while the client still has its req - **iOS (`ios/connect_native_http/Tests`, XCTest):** a write stuck on a full buffer returns once the call is canceled or its connection fails; a redirect of a request with a body is returned, not followed; shutting a session down lets its calls finish and refuses new ones. The script runs them on the simulator. For a quick loop - on the Mac, against a running test server, the bridge needs the server's CA in the Mac's trust store: it checks - certificates against that store only, with no callback of its own. Anyone with the CA's key, in - `test_server/certs`, can then impersonate any site to your Mac, so remove it afterwards, or prefer the simulator: - `sudo security delete-certificate -c "connect_native_http test CA" /Library/Keychains/System.keychain`. + on the Mac, against a running test server: ```bash - sudo security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain test_server/certs/ca.pem - cd ios/connect_native_http && CONNECT_NATIVE_HTTP_CORE_ONLY=1 SERVER_URL=https://localhost:8443 swift test + cd ios/connect_native_http && CONNECT_NATIVE_HTTP_CORE_ONLY=1 SERVER_URL=https://localhost:8443 \ + SERVER_CA=../../test_server/certs/ca.der swift test ``` - **Android (`android/src/androidTest`, instrumented):** writes sent before Cronet is ready go out once it is; a diff --git a/ios/connect_native_http/Sources/ConnectNativeHttpCore/URLSessionBridge.swift b/ios/connect_native_http/Sources/ConnectNativeHttpCore/URLSessionBridge.swift index 8a9d2f7..7c17c89 100644 --- a/ios/connect_native_http/Sources/ConnectNativeHttpCore/URLSessionBridge.swift +++ b/ios/connect_native_http/Sources/ConnectNativeHttpCore/URLSessionBridge.swift @@ -10,6 +10,7 @@ public final class URLSessionBridge { } private let emit: ([String: Any]) -> Void + private let trustedRoots: [SecCertificate] // URLSession delegate callbacks and all state below run on this queue, so none of it needs locking. private let queue = DispatchQueue(label: "connect_native_http") @@ -48,7 +49,13 @@ public final class URLSessionBridge { } } - public init(emit: @escaping ([String: Any]) -> Void) { + public convenience init(emit: @escaping ([String: Any]) -> Void) { + self.init(trustedRoots: [], emit: emit) + } + + /// `trustedRoots`, when not empty, replaces the system trust store; tests use it for their own CA. + init(trustedRoots: [SecCertificate], emit: @escaping ([String: Any]) -> Void) { + self.trustedRoots = trustedRoots self.emit = emit } @@ -216,17 +223,26 @@ public final class URLSessionBridge { emit(failure(c.callId, domain: error.domain, code: error.code, message: error.localizedDescription)) } + fileprivate func serverTrust(_ challenge: URLAuthenticationChallenge) -> ( + URLSession.AuthChallengeDisposition, URLCredential? + ) { + guard !trustedRoots.isEmpty, + challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust, + let trust = challenge.protectionSpace.serverTrust + else { + return (.performDefaultHandling, nil) + } + SecTrustSetAnchorCertificates(trust, trustedRoots as CFArray) + SecTrustSetAnchorCertificatesOnly(trust, true) + return SecTrustEvaluateWithError(trust, nil) + ? (.useCredential, URLCredential(trust: trust)) : (.cancelAuthenticationChallenge, nil) + } + // 64 KiB, the bound pair's buffer: how much request body may wait for URLSession to read it. private static let bodyBufferBytes = 64 * 1024 } // URLSession keeps its delegate until the session is invalidated, so this holds the bridge weakly. -// -// It deliberately has no authentication challenge method: certificates are checked against the system trust store -// only, inside the network stack. A delegate that implements the challenge at all gets every TLS handshake's -// certificate check, QUIC and TCP alike, sent through `queue`, behind every call's callbacks; on a loaded device that -// held a QUIC handshake long enough for URLSession's TCP attempt to win the race. Tests trust their server's CA by -// installing it in the device's (or Mac's) trust store. private final class Delegate: NSObject, URLSessionDataDelegate { weak var bridge: URLSessionBridge? @@ -234,6 +250,14 @@ private final class Delegate: NSObject, URLSessionDataDelegate { self.bridge = bridge } + func urlSession( + _ session: URLSession, didReceive challenge: URLAuthenticationChallenge, + completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void + ) { + let (disposition, credential) = bridge?.serverTrust(challenge) ?? (.performDefaultHandling, nil) + completionHandler(disposition, credential) + } + func urlSession( _ session: URLSession, task: URLSessionTask, willPerformHTTPRedirection response: HTTPURLResponse, newRequest request: URLRequest, completionHandler: @escaping (URLRequest?) -> Void diff --git a/ios/connect_native_http/Tests/ConnectNativeHttpCoreTests/URLSessionBridgeTests.swift b/ios/connect_native_http/Tests/ConnectNativeHttpCoreTests/URLSessionBridgeTests.swift index b93117e..58dedf3 100644 --- a/ios/connect_native_http/Tests/ConnectNativeHttpCoreTests/URLSessionBridgeTests.swift +++ b/ios/connect_native_http/Tests/ConnectNativeHttpCoreTests/URLSessionBridgeTests.swift @@ -4,8 +4,7 @@ import XCTest @testable import ConnectNativeHttpCore // Bridge behavior the Dart integration tests can't reach, against test_server/. run_integration_tests.sh starts the -// server, passes its URL as SERVER_URL and installs its CA in the simulator's trust store; the bridge only uses that -// store. +// server and passes its URL as SERVER_URL and its CA certificate (DER) as SERVER_CA. final class URLSessionBridgeTests: XCTestCase { private static let converse = "/connectrpc.eliza.v1.ElizaService/Converse" private static let say = "/connectrpc.eliza.v1.ElizaService/Say" @@ -18,11 +17,12 @@ final class URLSessionBridgeTests: XCTestCase { override func setUpWithError() throws { let env = ProcessInfo.processInfo.environment - guard let url = env["SERVER_URL"] else { - throw XCTSkip("SERVER_URL is not set; run these through run_integration_tests.sh") + guard let url = env["SERVER_URL"], let caPath = env["SERVER_CA"] else { + throw XCTSkip("SERVER_URL and SERVER_CA are not set; run these through run_integration_tests.sh") } serverURL = url - bridge = URLSessionBridge { [weak self] event in + let ca = try XCTUnwrap(SecCertificateCreateWithData(nil, try Data(contentsOf: URL(fileURLWithPath: caPath)) as CFData)) + bridge = URLSessionBridge(trustedRoots: [ca]) { [weak self] event in guard let self else { return } lock.withLock { self.events.append(event) } } diff --git a/run_integration_tests.sh b/run_integration_tests.sh index 8cda010..c84a52d 100755 --- a/run_integration_tests.sh +++ b/run_integration_tests.sh @@ -155,7 +155,7 @@ if [[ " $SUITES " == *" native "* ]]; then # when CI has already run it. with_timeout 300 "$ROOT/ios/connect_native_http/build_core_tests.sh" "$ROOT/build/ios_core_tests" # The tests take about a second. - with_timeout 60 env SIMCTL_CHILD_SERVER_URL="$SERVER_URL" \ + with_timeout 60 env SIMCTL_CHILD_SERVER_URL="$SERVER_URL" SIMCTL_CHILD_SERVER_CA="$ROOT/test_server/certs/ca.der" \ xcrun simctl spawn "$DEVICE" \ "$(xcrun --sdk iphonesimulator --show-sdk-platform-path)/Developer/Library/Xcode/Agents/xctest" \ "$ROOT/build/ios_core_tests/Build/Products/Debug-iphonesimulator/ConnectNativeHttpCoreTests.xctest" From 8e535b6d2fbf56cd8fcc9ea473bbc55386842459 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 9 Nov 2026 18:07:11 +0000 Subject: [PATCH 4/7] Record trustd in the iOS simulator's network log In run 42427208429 one certificate evaluation took 151 ms, against 2-10 ms for the rest of that run and two earlier green runs, and URLSession's TCP attempt won the race. The bridge's delegate answered performDefaultHandling within a millisecond; the rest happened in the system's evaluation, which runs in trustd, outside the recorded processes. With trustd recorded, the next slow evaluation shows what it waited on. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01BqmpXW1H7XRFuEgBWW3ppW --- .github/workflows/ci.yaml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 58ce57f..8f205dd 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -183,12 +183,13 @@ jobs: # Every step targets $udid; anything else booted could answer in its place. xcrun simctl list devices booted # The network stack's view of every connection the tests make (QUIC vs TCP races, handshakes), uploaded below. - # Cheap, and the only way to see why a connection stalled or fell back to HTTP/2. + # Cheap, and the only way to see why a connection stalled or fell back to HTTP/2. trustd too: certificate + # evaluation happens there, and in run 42427208429 one took 151 ms (2-10 ms usually) and let TCP win the race. - name: Record the simulator's network log timeout-minutes: 2 run: | nohup xcrun simctl spawn "${{ steps.simulator.outputs.udid }}" log stream --level debug --style compact \ - --predicate '(process == "Runner" OR process == "xctest") AND (subsystem == "com.apple.network" OR subsystem == "com.apple.CFNetwork")' \ + --predicate '((process == "Runner" OR process == "xctest") AND (subsystem == "com.apple.network" OR subsystem == "com.apple.CFNetwork")) OR process == "trustd"' \ > "$RUNNER_TEMP/sim-network.log" 2>&1 & # First, since it also warms up the simulator: right after boot, the first minute of connections from the Dart # suite stalled before the TLS handshake on every run, and passed when the suite ran again. From 4beab36eb5b8a2717674b7be9055c0ab437086e3 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 9 Nov 2026 19:08:33 +0000 Subject: [PATCH 5/7] Don't implement the trust challenge when the bridge uses the system's roots The plugin's delegate implemented urlSession(_:didReceive:completionHandler:) even though, with no trusted roots of its own, it only ever answered performDefaultHandling. Implementing it at all makes CFNetwork ask the delegate before its own certificate check on every TLS handshake ("Need to invoke to satisfy trust callback"), so each handshake waits for the bridge's serial queue. In run 42671165122 (attempt 2), at app launch, that answer came 126 ms after CFNetwork asked, while trustd was idle. The app's network thread did nothing in between, the server's QUIC reply sat unprocessed, and the first call went over HTTP/2. Only a bridge with its own trusted roots (the native XCTests) now implements the challenge, in a TrustingDelegate subclass; the shipped plugin leaves certificate checks to the network stack. The subclass names the Objective-C selector explicitly, since URLSession finds optional delegate methods by selector. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01BqmpXW1H7XRFuEgBWW3ppW --- .../URLSessionBridge.swift | 34 ++++++++++++------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/ios/connect_native_http/Sources/ConnectNativeHttpCore/URLSessionBridge.swift b/ios/connect_native_http/Sources/ConnectNativeHttpCore/URLSessionBridge.swift index 7c17c89..fbb933b 100644 --- a/ios/connect_native_http/Sources/ConnectNativeHttpCore/URLSessionBridge.swift +++ b/ios/connect_native_http/Sources/ConnectNativeHttpCore/URLSessionBridge.swift @@ -69,8 +69,12 @@ public final class URLSessionBridge { // URLSession fails a task that receives nothing for this long. Its default of 60 s would end a quiet stream // or a long-running unary call; callers bound calls with deadlines instead. configuration.timeoutIntervalForRequest = 24 * 60 * 60 - let session = URLSession( - configuration: configuration, delegate: Delegate(bridge: self), delegateQueue: self.delegateQueue) + // Only a bridge with its own roots implements the trust challenge. With the system's roots the network stack + // checks certificates itself; a delegate that implements the challenge makes every TLS handshake, QUIC and + // TCP alike, wait for an answer from `queue` first. At app launch on CI that answer once took 126 ms while + // the QUIC handshake waited, and URLSession's TCP attempt won the race. + let delegate = self.trustedRoots.isEmpty ? Delegate(bridge: self) : TrustingDelegate(bridge: self) + let session = URLSession(configuration: configuration, delegate: delegate, delegateQueue: self.delegateQueue) let id = self.nextSessionId self.nextSessionId += 1 self.sessions[id] = Session(urlSession: session, http3Origins: Set(origins)) @@ -226,8 +230,7 @@ public final class URLSessionBridge { fileprivate func serverTrust(_ challenge: URLAuthenticationChallenge) -> ( URLSession.AuthChallengeDisposition, URLCredential? ) { - guard !trustedRoots.isEmpty, - challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust, + guard challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust, let trust = challenge.protectionSpace.serverTrust else { return (.performDefaultHandling, nil) @@ -243,21 +246,13 @@ public final class URLSessionBridge { } // URLSession keeps its delegate until the session is invalidated, so this holds the bridge weakly. -private final class Delegate: NSObject, URLSessionDataDelegate { +private class Delegate: NSObject, URLSessionDataDelegate { weak var bridge: URLSessionBridge? init(bridge: URLSessionBridge) { self.bridge = bridge } - func urlSession( - _ session: URLSession, didReceive challenge: URLAuthenticationChallenge, - completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void - ) { - let (disposition, credential) = bridge?.serverTrust(challenge) ?? (.performDefaultHandling, nil) - completionHandler(disposition, credential) - } - func urlSession( _ session: URLSession, task: URLSessionTask, willPerformHTTPRedirection response: HTTPURLResponse, newRequest request: URLRequest, completionHandler: @escaping (URLRequest?) -> Void @@ -287,3 +282,16 @@ private final class Delegate: NSObject, URLSessionDataDelegate { bridge?.didComplete(task, error: error) } } + +// A Delegate that also checks server certificates against the bridge's own roots. URLSession looks the method up by +// its Objective-C selector, hence the explicit name: a subclass doesn't inherit the protocol's @objc inference. +private final class TrustingDelegate: Delegate { + @objc(URLSession:didReceiveChallenge:completionHandler:) + func urlSession( + _ session: URLSession, didReceive challenge: URLAuthenticationChallenge, + completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void + ) { + let (disposition, credential) = bridge?.serverTrust(challenge) ?? (.performDefaultHandling, nil) + completionHandler(disposition, credential) + } +} From 71e7fb56555a46df12a65d90384727eb8c33ca4e Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 9 Nov 2026 19:22:13 +0000 Subject: [PATCH 6/7] Correct the reason given for not implementing the trust challenge Run 42680510479 still logs CFNetwork's "Need to invoke to satisfy trust callback" and "auth completion disp=1" with no challenge method on the delegate, so that step is CFNetwork's own and not caused by implementing the method; the 126 ms before it in the previous run wasn't the bridge's queue. In both runs the launch-time first call lost the race because the whole runner was slow then: the client took 66 ms to set up its QUIC connection, and the Go server processed its first QUIC packet 130 ms after it was sent, against 1-5 ms for every later connection. Not implementing the challenge with the system's roots stays, as the simpler setup; the comment no longer claims it fixes the race. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01BqmpXW1H7XRFuEgBWW3ppW --- .../Sources/ConnectNativeHttpCore/URLSessionBridge.swift | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/ios/connect_native_http/Sources/ConnectNativeHttpCore/URLSessionBridge.swift b/ios/connect_native_http/Sources/ConnectNativeHttpCore/URLSessionBridge.swift index fbb933b..a9f9729 100644 --- a/ios/connect_native_http/Sources/ConnectNativeHttpCore/URLSessionBridge.swift +++ b/ios/connect_native_http/Sources/ConnectNativeHttpCore/URLSessionBridge.swift @@ -69,10 +69,8 @@ public final class URLSessionBridge { // URLSession fails a task that receives nothing for this long. Its default of 60 s would end a quiet stream // or a long-running unary call; callers bound calls with deadlines instead. configuration.timeoutIntervalForRequest = 24 * 60 * 60 - // Only a bridge with its own roots implements the trust challenge. With the system's roots the network stack - // checks certificates itself; a delegate that implements the challenge makes every TLS handshake, QUIC and - // TCP alike, wait for an answer from `queue` first. At app launch on CI that answer once took 126 ms while - // the QUIC handshake waited, and URLSession's TCP attempt won the race. + // Only a bridge with its own roots implements the trust challenge. With the system's roots there is nothing + // for the delegate to decide, so certificate checks are left entirely to the network stack. let delegate = self.trustedRoots.isEmpty ? Delegate(bridge: self) : TrustingDelegate(bridge: self) let session = URLSession(configuration: configuration, delegate: delegate, delegateQueue: self.delegateQueue) let id = self.nextSessionId From da1671c129f5c84ac00b54c0659f5ee4cd3ae8b1 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 9 Nov 2026 19:23:29 +0000 Subject: [PATCH 7/7] Warm up the app and test server with one call before the Dart cases The first call after the app launches lost URLSession's QUIC/TCP race in both of the last two runs: the simulator took 66 ms to set up its QUIC connection and the test server, on the same runner, answered its first QUIC packet 130 ms after it was sent. setUpAll now makes one call on a throwaway client first and logs which protocol it got, without checking it, so the cases start on a warm app and server. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01BqmpXW1H7XRFuEgBWW3ppW --- example/integration_test/eliza_test.dart | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/example/integration_test/eliza_test.dart b/example/integration_test/eliza_test.dart index 329a8b6..1a581bd 100644 --- a/example/integration_test/eliza_test.dart +++ b/example/integration_test/eliza_test.dart @@ -137,6 +137,25 @@ void main() { IntegrationTestWidgetsFlutterBinding.ensureInitialized(); late Harness harness; + // One call on a throwaway client before any case. Right after the app launches, the simulator and the test server + // on the same CI runner are both slow (a QUIC connection took 66 ms to set up, and the server answered its first + // packet 130 ms after it was sent), so the first call's QUIC attempt loses URLSession's race to TCP. The cases then + // start on a warm app and server. The protocol it got is logged, not checked. + setUpAll(() async { + if (serverUrl.isEmpty) { + return; // setUp reports it. + } + final warmup = await Harness.create(serverUrl, http3Origins: {Uri.parse(serverUrl)}); + try { + await warmup.eliza + .say(SayRequest(sentence: 'warm up'), onHeader: warmup.recordHeaders) + .timeout(const Duration(seconds: 10)); + debugPrint('warm-up call went over ${warmup.serverProtocols.single}'); + } finally { + await warmup.native.close(); + } + }); + setUp(() async { expect(serverUrl, isNotEmpty, reason: 'run with --dart-define=SERVER_URL=...'); final origin = Uri.parse(serverUrl);