mirror of
https://github.com/block/buzz.git
synced 2026-11-25 02:31:36 +00:00
fix(mobile): classify gateway 2xx drift and fast clocks
An unexpected 2xx from a gateway route now reports as contract drift, since each route answers with exactly one success status. A challenge that already looks expired stays routine, since it usually means the phone's clock runs fast. Driver tests cover each classification. Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@localhost> Signed-off-by: Tom Brow <tomb@block.xyz>
This commit is contained in:
@@ -867,11 +867,15 @@ public final class BuzzDevPushEnrollmentDriver {
|
|||||||
)
|
)
|
||||||
guard let id = UUID(uuidString: response.challengeId),
|
guard let id = UUID(uuidString: response.challengeId),
|
||||||
response.challengeId == id.uuidString.lowercased(),
|
response.challengeId == id.uuidString.lowercased(),
|
||||||
Self.isBase64URLChallenge(response.challenge),
|
Self.isBase64URLChallenge(response.challenge)
|
||||||
response.expiresAt > Int64(now().timeIntervalSince1970)
|
|
||||||
else {
|
else {
|
||||||
throw BuzzDevPushEnrollmentError.invalidGatewayResponse(route: "v1/installations/challenges")
|
throw BuzzDevPushEnrollmentError.invalidGatewayResponse(route: "v1/installations/challenges")
|
||||||
}
|
}
|
||||||
|
// A challenge that already looks expired usually means the phone's clock
|
||||||
|
// runs fast, not that the gateway broke its contract.
|
||||||
|
guard response.expiresAt > Int64(now().timeIntervalSince1970) else {
|
||||||
|
throw BuzzDevPushEnrollmentError.invalidResponse(route: "v1/installations/challenges")
|
||||||
|
}
|
||||||
return Challenge(id: id, value: response.challenge)
|
return Challenge(id: id, value: response.challenge)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -976,6 +980,13 @@ public final class BuzzDevPushEnrollmentDriver {
|
|||||||
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
|
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
|
||||||
request.httpBody = try JSONEncoder().encode(body)
|
request.httpBody = try JSONEncoder().encode(body)
|
||||||
let (data, response) = try await session.data(for: request)
|
let (data, response) = try await session.data(for: request)
|
||||||
|
// The gateway answers each route with exactly one success status, so any
|
||||||
|
// other 2xx means its contract drifted rather than a routine refusal.
|
||||||
|
if let http = response as? HTTPURLResponse, (200..<300).contains(http.statusCode),
|
||||||
|
http.statusCode != expectedStatus
|
||||||
|
{
|
||||||
|
throw BuzzDevPushEnrollmentError.invalidGatewayResponse(route: route)
|
||||||
|
}
|
||||||
try Self.expectStatus(response, data: data, route: route, expected: expectedStatus)
|
try Self.expectStatus(response, data: data, route: route, expected: expectedStatus)
|
||||||
do {
|
do {
|
||||||
return try JSONDecoder().decode(Response.self, from: data)
|
return try JSONDecoder().decode(Response.self, from: data)
|
||||||
|
|||||||
@@ -1308,6 +1308,72 @@ final class BuzzDevPushEnrollmentDriverTests: XCTestCase {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func testMalformedGatewayReplyIsContractDrift() async throws {
|
||||||
|
let error = try await challengeFailure { request in
|
||||||
|
let response = HTTPURLResponse(
|
||||||
|
url: request.url!, statusCode: 200, httpVersion: "HTTP/1.1",
|
||||||
|
headerFields: ["Content-Type": "application/json"])!
|
||||||
|
return (response, Data("not json".utf8))
|
||||||
|
}
|
||||||
|
XCTAssertEqual(error, .invalidGatewayResponse(route: "v1/installations/challenges"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func testUnexpectedGatewaySuccessStatusIsContractDrift() async throws {
|
||||||
|
let error = try await challengeFailure { request in
|
||||||
|
Self.response(request, status: 201, json: Self.challengeJSON(expiresAt: Self.expiresAt))
|
||||||
|
}
|
||||||
|
XCTAssertEqual(error, .invalidGatewayResponse(route: "v1/installations/challenges"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func testGatewayRefusalIsRoutineStatusMismatch() async throws {
|
||||||
|
let error = try await challengeFailure { request in
|
||||||
|
Self.response(request, status: 503, json: ["error": "unavailable"])
|
||||||
|
}
|
||||||
|
XCTAssertEqual(
|
||||||
|
error,
|
||||||
|
.unexpectedStatus(
|
||||||
|
route: "v1/installations/challenges", expected: 200, actual: 503,
|
||||||
|
body: "{\"error\":\"unavailable\"}"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func testExpiredLookingChallengeFromFastClockIsNotContractDrift() async throws {
|
||||||
|
let error = try await challengeFailure { request in
|
||||||
|
// The driver's clock reads `now` plus the skew allowance, past this expiry.
|
||||||
|
Self.response(request, status: 200, json: Self.challengeJSON(expiresAt: Self.now))
|
||||||
|
}
|
||||||
|
XCTAssertEqual(error, .invalidResponse(route: "v1/installations/challenges"))
|
||||||
|
}
|
||||||
|
|
||||||
|
private func challengeFailure(
|
||||||
|
_ gateway: @escaping (URLRequest) -> (HTTPURLResponse, Data)
|
||||||
|
) async throws -> BuzzDevPushEnrollmentError {
|
||||||
|
let driver = try makeDriver(store: MemoryGrantStore(), appAttest: RecordingAppAttest())
|
||||||
|
URLProtocolStub.handler = { request in
|
||||||
|
if request.httpMethod == "GET" {
|
||||||
|
return Self.response(
|
||||||
|
request,
|
||||||
|
status: 200,
|
||||||
|
json: [
|
||||||
|
"self": Self.relayPubkey,
|
||||||
|
"push": ["keys": [["pubkey": Self.relayPubkey, "current": true]]],
|
||||||
|
]
|
||||||
|
)
|
||||||
|
}
|
||||||
|
return gateway(request)
|
||||||
|
}
|
||||||
|
do {
|
||||||
|
_ = try await driver.enroll(deviceToken: Data([1]), relayURL: Self.relayURL)
|
||||||
|
} catch let error as BuzzDevPushEnrollmentError {
|
||||||
|
return error
|
||||||
|
}
|
||||||
|
XCTFail("Expected enrollment to fail at the challenge")
|
||||||
|
throw CancellationError()
|
||||||
|
}
|
||||||
|
|
||||||
|
private static func challengeJSON(expiresAt: Int64) -> [String: Any] {
|
||||||
|
["challenge_id": firstChallengeId, "challenge": challenge, "expires_at": expiresAt]
|
||||||
|
}
|
||||||
|
|
||||||
private func makeDriver(
|
private func makeDriver(
|
||||||
gatewayBaseURL: URL = BuzzDevPushEnrollmentDriverTests.gatewayURL,
|
gatewayBaseURL: URL = BuzzDevPushEnrollmentDriverTests.gatewayURL,
|
||||||
store: BuzzPushEndpointGrantStore,
|
store: BuzzPushEndpointGrantStore,
|
||||||
|
|||||||
Reference in New Issue
Block a user