Repository navigation
iOS/macOS transport: survive wake-time broker 401s without endpoint teardown #9263
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9d5b75a
Add failing wake-auth transport regressions
azooz2003-bit 562fc11
Survive wake-time broker auth rejections without endpoint teardown
azooz2003-bit 1197b17
Move 401/403 to the preserved-policy foreground fixtures
azooz2003-bit 3e53ad9
Keep dial-time cached-policy fallbacks availability-only
azooz2003-bit 7063d0f
Fix leftover recoversWithCachedPolicy reference in discovery fallback
azooz2003-bit File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
...hTransport/Tests/CmuxIrohTransportTests/CmxIrohTrustBrokerClientAuthClassifierTests.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| import Foundation | ||
| import Testing | ||
| @testable import CmuxIrohTransport | ||
|
|
||
| /// Regression coverage for the wake-time authorization outage: a broker 401 | ||
| /// used to tear down the whole verified runtime (endpoint, routes, offline | ||
| /// cache) and nap for 30s+ of backoff, turning a seconds-long token rotation | ||
| /// race into a multi-minute connectivity gap on every app foreground. | ||
| struct CmxIrohTrustBrokerClientAuthClassifierTests { | ||
| @Test | ||
| func unauthorizedRejectionPreservesVerifiedPolicyDuringRefresh() { | ||
| #expect(CmxIrohTrustBrokerClientError.preservesVerifiedPolicyDuringRefresh( | ||
| CmxIrohTrustBrokerClientError.rejected( | ||
| statusCode: 401, | ||
| code: "unauthorized" | ||
| ) | ||
| )) | ||
| #expect(CmxIrohTrustBrokerClientError.preservesVerifiedPolicyDuringRefresh( | ||
| CmxIrohTrustBrokerClientError.rejected(statusCode: 403, code: nil) | ||
| )) | ||
| } | ||
|
|
||
| @Test | ||
| func unauthorizedRejectionRetriesInitialActivation() { | ||
| #expect(CmxIrohTrustBrokerClientError.retriesInitialActivation( | ||
| CmxIrohTrustBrokerClientError.rejected( | ||
| statusCode: 401, | ||
| code: "unauthorized" | ||
| ) | ||
| )) | ||
| // 403 can be a durable permission denial; initial activation must not | ||
| // spin on it. | ||
| #expect(!CmxIrohTrustBrokerClientError.retriesInitialActivation( | ||
| CmxIrohTrustBrokerClientError.rejected(statusCode: 403, code: nil) | ||
| )) | ||
| } | ||
| } |
205 changes: 205 additions & 0 deletions
205
...rohTransport/Tests/CmuxIrohTransportTests/CmxIrohTrustBrokerClientAuthRecoveryTests.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,205 @@ | ||
| import Foundation | ||
| import Testing | ||
| @testable import CmuxIrohTransport | ||
|
|
||
| /// The broker client's exactly-once credential recovery: a pair that was | ||
| /// coherent at capture can still be rejected when another lane rotates the | ||
| /// session between capture and server validation (the wake-time RPC force | ||
| /// refresh, most commonly). One retry with a pair minted after the rejection | ||
| /// absorbs the race; a second rejection is authoritative. | ||
| @Suite(.serialized) | ||
| struct CmxIrohTrustBrokerClientAuthRecoveryTests { | ||
| private actor RecoveryRecorder { | ||
| private(set) var rejectedPairs: [CmxIrohBrokerCredentials] = [] | ||
| private let recovered: CmxIrohBrokerCredentials? | ||
|
|
||
| init(recovered: CmxIrohBrokerCredentials?) { | ||
| self.recovered = recovered | ||
| } | ||
|
|
||
| func recover( | ||
| _ rejected: CmxIrohBrokerCredentials | ||
| ) -> CmxIrohBrokerCredentials? { | ||
| rejectedPairs.append(rejected) | ||
| return recovered | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| func unauthorizedRejectionRetriesOnceWithRecoveredPair() async throws { | ||
| let transport = RecordingBrokerTransport(responses: [ | ||
| .json(status: 401, body: #"{"error":"unauthorized"}"#), | ||
| .json(status: 201, body: Self.challengeBody), | ||
| ]) | ||
| let recorder = RecoveryRecorder(recovered: CmxIrohBrokerCredentials( | ||
| accessToken: "fresh-access", | ||
| refreshToken: "fresh-refresh" | ||
| )) | ||
| let client = try makeClient(transport: transport, recorder: recorder) | ||
|
|
||
| let response = try await client.issueChallenge(try Self.challengeRequest) | ||
|
|
||
| #expect(response.challengeID == "123e4567-e89b-42d3-a456-426614174000") | ||
| let requests = await transport.requests() | ||
| #expect(requests.count == 2) | ||
| #expect(requests.first?.value( | ||
| forHTTPHeaderField: "Authorization" | ||
| ) == "Bearer stale-access") | ||
| #expect(requests.last?.value( | ||
| forHTTPHeaderField: "Authorization" | ||
| ) == "Bearer fresh-access") | ||
| #expect(requests.last?.value( | ||
| forHTTPHeaderField: "X-Stack-Refresh-Token" | ||
| ) == "fresh-refresh") | ||
| let rejected = await recorder.rejectedPairs | ||
| #expect(rejected.count == 1) | ||
| #expect(rejected.first?.accessToken == "stale-access") | ||
| #expect(rejected.first?.refreshToken == "stale-refresh") | ||
| } | ||
|
|
||
| @Test | ||
| func unauthorizedRejectionWithoutRecoveryPropagates() async throws { | ||
| let transport = RecordingBrokerTransport(responses: [ | ||
| .json(status: 401, body: #"{"error":"unauthorized"}"#), | ||
| ]) | ||
| let client = try CmxIrohTrustBrokerClient( | ||
| baseURL: #require(URL(string: "https://cmux.example")), | ||
| tokenSource: CmxIrohBrokerTokenSource(credentialPair: { | ||
| CmxIrohBrokerCredentials( | ||
| accessToken: "stale-access", | ||
| refreshToken: "stale-refresh" | ||
| ) | ||
| }), | ||
| transport: transport | ||
| ) | ||
|
|
||
| await #expect(throws: CmxIrohTrustBrokerClientError.rejected( | ||
| statusCode: 401, | ||
| code: "unauthorized" | ||
| )) { | ||
| _ = try await client.issueChallenge(try Self.challengeRequest) | ||
| } | ||
| #expect(await transport.requests().count == 1) | ||
| } | ||
|
|
||
| @Test | ||
| func repeatedUnauthorizedRejectionStopsAfterOneRetry() async throws { | ||
| let transport = RecordingBrokerTransport(responses: [ | ||
| .json(status: 401, body: #"{"error":"unauthorized"}"#), | ||
| .json(status: 401, body: #"{"error":"unauthorized"}"#), | ||
| ]) | ||
| let recorder = RecoveryRecorder(recovered: CmxIrohBrokerCredentials( | ||
| accessToken: "fresh-access", | ||
| refreshToken: "fresh-refresh" | ||
| )) | ||
| let client = try makeClient(transport: transport, recorder: recorder) | ||
|
|
||
| await #expect(throws: CmxIrohTrustBrokerClientError.rejected( | ||
| statusCode: 401, | ||
| code: "unauthorized" | ||
| )) { | ||
| _ = try await client.issueChallenge(try Self.challengeRequest) | ||
| } | ||
| #expect(await transport.requests().count == 2) | ||
| #expect(await recorder.rejectedPairs.count == 1) | ||
| } | ||
|
|
||
| @Test | ||
| func forbiddenRejectionDoesNotInvokeRecovery() async throws { | ||
| let transport = RecordingBrokerTransport(responses: [ | ||
| .json(status: 403, body: #"{"error":"forbidden"}"#), | ||
| ]) | ||
| let recorder = RecoveryRecorder(recovered: CmxIrohBrokerCredentials( | ||
| accessToken: "fresh-access", | ||
| refreshToken: "fresh-refresh" | ||
| )) | ||
| let client = try makeClient(transport: transport, recorder: recorder) | ||
|
|
||
| await #expect(throws: CmxIrohTrustBrokerClientError.rejected( | ||
| statusCode: 403, | ||
| code: "forbidden" | ||
| )) { | ||
| _ = try await client.issueChallenge(try Self.challengeRequest) | ||
| } | ||
| #expect(await transport.requests().count == 1) | ||
| #expect(await recorder.rejectedPairs.isEmpty) | ||
| } | ||
|
|
||
| @Test | ||
| func authRejectionsRecoverWithCachedPolicy() { | ||
| #expect(CmxIrohClientRuntime.recoversWithCachedPolicy( | ||
| CmxIrohTrustBrokerClientError.connectivity | ||
| )) | ||
| #expect(CmxIrohClientRuntime.recoversWithCachedPolicy( | ||
| CmxIrohTrustBrokerClientError.rejected( | ||
| statusCode: 401, | ||
| code: "unauthorized" | ||
| ) | ||
| )) | ||
| #expect(CmxIrohClientRuntime.recoversWithCachedPolicy( | ||
| CmxIrohTrustBrokerClientError.rejected(statusCode: 403, code: nil) | ||
| )) | ||
| #expect(!CmxIrohClientRuntime.recoversWithCachedPolicy( | ||
| CmxIrohTrustBrokerClientError.rejected(statusCode: 500, code: nil) | ||
| )) | ||
| #expect(!CmxIrohClientRuntime.recoversWithCachedPolicy( | ||
| CmxIrohTrustBrokerClientError.invalidResponse | ||
| )) | ||
| } | ||
|
|
||
| private func makeClient( | ||
| transport: RecordingBrokerTransport, | ||
| recorder: RecoveryRecorder | ||
| ) throws -> CmxIrohTrustBrokerClient { | ||
| try CmxIrohTrustBrokerClient( | ||
| baseURL: #require(URL(string: "https://cmux.example")), | ||
| tokenSource: CmxIrohBrokerTokenSource( | ||
| credentialPair: { | ||
| CmxIrohBrokerCredentials( | ||
| accessToken: "stale-access", | ||
| refreshToken: "stale-refresh" | ||
| ) | ||
| }, | ||
| recoveredCredentialPair: { rejected in | ||
| await recorder.recover(rejected) | ||
| } | ||
| ), | ||
| transport: transport | ||
| ) | ||
| } | ||
|
|
||
| private static let challengeBody = | ||
| #"{"challenge_id":"123e4567-e89b-42d3-a456-426614174000","nonce":"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA","expires_at":"2026-07-10T01:00:00.000Z"}"# | ||
|
|
||
| private static let endpointID = | ||
| "03a107bff3ce10be1d70dd18e74bc09967e4d6309ba50d5f1ddc8664125531b8" | ||
|
|
||
| private static var challengeRequest: CmxIrohChallengeRequest { | ||
| get throws { | ||
| let secret = try CmxIrohSecretKey( | ||
| bytes: Data((0 ..< 32).map(UInt8.init)) | ||
| ) | ||
| let identity = try CmxIrohIdentityMaterial( | ||
| secretKey: secret, | ||
| generation: 1 | ||
| ) | ||
| let signer = try CmxIrohRegistrationSigner( | ||
| identity: identity, | ||
| endpointID: endpointID | ||
| ) | ||
| let payload = try CmxIrohRegistrationPayload( | ||
| deviceID: "123e4567-e89b-42d3-a456-426614174001", | ||
| appInstanceID: "123e4567-e89b-42d3-a456-426614174002", | ||
| tag: "stable", | ||
| platform: .ios, | ||
| endpointID: endpointID, | ||
| identityGeneration: 1, | ||
| pairingEnabled: false, | ||
| capabilities: ["control"], | ||
| pathHints: [], | ||
| now: Date(timeIntervalSince1970: 1_782_000_000) | ||
| ) | ||
| return try signer.prepare(payload: payload).challengeRequest | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.