Skip to content

Commit bde482d

Browse files
committed
fix(mobile): double the wait between rejected-enrollment replacements
Each replacement of a gateway-rejected journal generates and attests a new App Attest key. A fixed hourly wait still allowed one new key every hour for as long as the gateway kept rejecting the device. The journal now counts consecutive rejected replacements, and the wait doubles with each one, so a persistently rejected device generates about ten keys in its 30-day journal lifetime instead of hundreds. Signed-off-by: Tom Brow <[email protected]>
1 parent 186aeae commit bde482d

3 files changed

Lines changed: 81 additions & 22 deletions

File tree

‎mobile/ios/BuzzPushKit/Sources/BuzzPushKit/BuzzDevPushEnrollmentDriver.swift‎

Lines changed: 55 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,8 @@ struct BuzzDCAppAttestProvider: BuzzDevAppAttesting {
327327
public final class BuzzDevPushEnrollmentDriver {
328328
public static let endpointEpoch: Int64 = 1
329329
/// Minimum age before a journaled installation request the gateway rejected
330-
/// is replaced, which bounds fresh App Attest keys while rejections persist.
330+
/// is replaced. It doubles with each consecutive replacement, which bounds
331+
/// fresh App Attest keys while rejections persist.
331332
static let rejectedEnrollmentReplacementSeconds: Int64 = 3_600
332333

333334
private let gatewayBaseURL: URL
@@ -402,6 +403,14 @@ public final class BuzzDevPushEnrollmentDriver {
402403
public func enroll(
403404
deviceToken: Data,
404405
relayURL: URL
406+
) async throws -> BuzzPushEndpointGrantRecord {
407+
try await enroll(deviceToken: deviceToken, relayURL: relayURL, rejectedReplacements: 0)
408+
}
409+
410+
private func enroll(
411+
deviceToken: Data,
412+
relayURL: URL,
413+
rejectedReplacements: Int
405414
) async throws -> BuzzPushEndpointGrantRecord {
406415
precondition(!deviceToken.isEmpty, "The APNs device token must not be empty")
407416
let relayOrigin = try Self.relayOrigin(relayURL)
@@ -426,19 +435,26 @@ public final class BuzzDevPushEnrollmentDriver {
426435
// Finish an unexpired request using its exact journaled endpoint before
427436
// replacing it. A token/key change does not make a response-lost request
428437
// safe to discard, nor does it require revoking sibling installations.
438+
var replacements = 0
429439
if pending.expiresAt > nowSeconds {
430-
_ = try await resumeEnrollment(
440+
if case .replaced(let count) = try await resumeEnrollment(
431441
pending,
432442
storedRecords: storedRecords,
433443
relayMetadataPubkey: nil
434-
)
444+
) {
445+
replacements = count
446+
}
435447
} else {
436448
try store.removePendingEnrollment(
437449
gatewayOrigin: gatewayOrigin,
438450
relayOrigin: relayOrigin.text
439451
)
440452
}
441-
return try await enroll(deviceToken: deviceToken, relayURL: relayURL)
453+
return try await enroll(
454+
deviceToken: deviceToken,
455+
relayURL: relayURL,
456+
rejectedReplacements: replacements
457+
)
442458
}
443459
let newestSharedGrant = storedRecords.filter {
444460
$0.gatewayOrigin == gatewayOrigin && $0.relayPubkey == relayPubkey
@@ -544,21 +560,28 @@ public final class BuzzDevPushEnrollmentDriver {
544560
challenge: enrollmentChallenge.value,
545561
keyId: attestation.keyId,
546562
attestation: attestation.attestation,
547-
preparedAt: nowSeconds
563+
preparedAt: nowSeconds,
564+
rejectedReplacements: rejectedReplacements
548565
)
549566
// The exact signed request is durable before the first network attempt.
550567
try store.savePendingEnrollment(pending)
551568
}
552569

553570
if pendingEnrollment != nil {
554-
if let record = try await resumeEnrollment(
571+
switch try await resumeEnrollment(
555572
pending,
556573
storedRecords: storedRecords,
557574
relayMetadataPubkey: relayKeys.metadataPubkey
558575
) {
576+
case .completed(let record):
559577
return record
578+
case .replaced(let count):
579+
return try await enroll(
580+
deviceToken: deviceToken,
581+
relayURL: relayURL,
582+
rejectedReplacements: count
583+
)
560584
}
561-
return try await enroll(deviceToken: deviceToken, relayURL: relayURL)
562585
}
563586
return try await completeEnrollment(
564587
pending,
@@ -567,16 +590,24 @@ public final class BuzzDevPushEnrollmentDriver {
567590
)
568591
}
569592

593+
private enum Resumption {
594+
case completed(BuzzPushEndpointGrantRecord)
595+
/// The journal was discarded; a fresh request carries this rejection count.
596+
case replaced(rejectedReplacements: Int)
597+
}
598+
570599
private func resumeEnrollment(
571600
_ pending: BuzzPushPendingEnrollmentRecord,
572601
storedRecords: [BuzzPushEndpointGrantRecord],
573602
relayMetadataPubkey: String?
574-
) async throws -> BuzzPushEndpointGrantRecord? {
603+
) async throws -> Resumption {
575604
do {
576-
return try await completeEnrollment(
577-
pending,
578-
storedRecords: storedRecords,
579-
relayMetadataPubkey: relayMetadataPubkey
605+
return .completed(
606+
try await completeEnrollment(
607+
pending,
608+
storedRecords: storedRecords,
609+
relayMetadataPubkey: relayMetadataPubkey
610+
)
580611
)
581612
} catch BuzzDevPushEnrollmentError.unexpectedStatus(
582613
route: "v1/installations", _, let actual, _
@@ -588,15 +619,18 @@ public final class BuzzDevPushEnrollmentDriver {
588619
gatewayOrigin: gatewayOrigin,
589620
relayOrigin: pending.relayOrigin
590621
)
591-
return nil
622+
return .replaced(
623+
rejectedReplacements: actual == 404 ? 0 : (pending.rejectedReplacements ?? 0) + 1
624+
)
592625
}
593626
}
594627

595628
/// 404 means the exact replay found no committed installation and its
596629
/// challenge expired. The gateway returns 400 and 401 before looking up a
597630
/// committed installation, so the same bytes can never recover one; those
598-
/// journals are replaced only once old enough, because every replacement
599-
/// generates and attests a new App Attest key.
631+
/// journals are replaced only once old enough, with the wait doubling for
632+
/// each consecutive replacement, because every replacement generates and
633+
/// attests a new App Attest key.
600634
private func replacesRejectedJournal(
601635
_ pending: BuzzPushPendingEnrollmentRecord,
602636
_ status: Int
@@ -607,7 +641,8 @@ public final class BuzzDevPushEnrollmentDriver {
607641
case 400, 401:
608642
guard let preparedAt = pending.preparedAt else { return true }
609643
let nowSeconds = Int64(now().timeIntervalSince1970)
610-
return nowSeconds - preparedAt >= Self.rejectedEnrollmentReplacementSeconds
644+
let doublings = min(pending.rejectedReplacements ?? 0, 20)
645+
return nowSeconds - preparedAt >= Self.rejectedEnrollmentReplacementSeconds << doublings
611646
default:
612647
return false
613648
}
@@ -664,7 +699,8 @@ public final class BuzzDevPushEnrollmentDriver {
664699
keyId: pending.keyId,
665700
attestation: pending.attestation,
666701
delegationGeneration: pending.delegationGeneration,
667-
preparedAt: pending.preparedAt
702+
preparedAt: pending.preparedAt,
703+
rejectedReplacements: pending.rejectedReplacements
668704
)
669705
try store.savePendingEnrollment(pending)
670706
}
@@ -703,7 +739,8 @@ public final class BuzzDevPushEnrollmentDriver {
703739
keyId: pending.keyId,
704740
attestation: pending.attestation,
705741
delegationGeneration: generation,
706-
preparedAt: pending.preparedAt
742+
preparedAt: pending.preparedAt,
743+
rejectedReplacements: pending.rejectedReplacements
707744
)
708745
// Reserve before delegation so a committed delegation followed by a local
709746
// save failure is retried at a strictly higher generation.

‎mobile/ios/BuzzPushKit/Sources/BuzzPushKit/BuzzPushPendingEnrollmentRecord.swift‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ public struct BuzzPushPendingEnrollmentRecord: Codable, Equatable, Sendable {
3131
/// Unix time the journaled installation request was prepared; nil when there
3232
/// is none or the journal predates this field.
3333
public let preparedAt: Int64?
34+
/// Consecutive gateway-rejected journals this one replaced; nil means none.
35+
public let rejectedReplacements: Int?
3436

3537
/// Creates a gateway-scoped durable record from authenticated enrollment state.
3638
public init(
@@ -47,7 +49,8 @@ public struct BuzzPushPendingEnrollmentRecord: Codable, Equatable, Sendable {
4749
keyId: String? = nil,
4850
attestation: String? = nil,
4951
delegationGeneration: Int64 = 0,
50-
preparedAt: Int64? = nil
52+
preparedAt: Int64? = nil,
53+
rejectedReplacements: Int? = nil
5154
) {
5255
self.gatewayOrigin = gatewayOrigin
5356
self.relayOrigin = relayOrigin
@@ -63,6 +66,7 @@ public struct BuzzPushPendingEnrollmentRecord: Codable, Equatable, Sendable {
6366
self.attestation = attestation
6467
self.delegationGeneration = delegationGeneration
6568
self.preparedAt = preparedAt
69+
self.rejectedReplacements = rejectedReplacements
6670
}
6771

6872
}

‎mobile/ios/BuzzPushKit/Tests/BuzzPushKitTests/BuzzDevPushEnrollmentDriverTests.swift‎

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,8 @@ final class BuzzDevPushEnrollmentDriverTests: XCTestCase {
410410
let driver = try makeDriver(
411411
store: store,
412412
appAttest: appAttest,
413-
now: { Date(timeIntervalSince1970: TimeInterval(clock)) }
413+
now: { Date(timeIntervalSince1970: TimeInterval(clock)) },
414+
lifetimeSeconds: 30 * 86_400
414415
)
415416
var challengeCount = 0
416417
var replayRequests = 0
@@ -488,6 +489,21 @@ final class BuzzDevPushEnrollmentDriverTests: XCTestCase {
488489
}
489490
XCTAssertEqual(freshRequests, freshStatus == nil ? 0 : freshStatus == 201 ? 1 : 2, label)
490491
XCTAssertEqual(appAttest.preparedAttestations.count, freshStatus == nil ? 1 : 2, label)
492+
493+
guard freshStatus == 401 else { continue }
494+
// Each consecutive replacement doubles the wait before the next one.
495+
XCTAssertEqual(store.pending.first?.rejectedReplacements, 1)
496+
for (advance, keys) in [(interval, 2), (interval, 3)] {
497+
clock += advance
498+
do {
499+
_ = try await driver.enroll(deviceToken: token, relayURL: Self.relayURL)
500+
XCTFail("Expected the persistent rejection")
501+
} catch BuzzDevPushEnrollmentError.unexpectedStatus(
502+
route: "v1/installations", _, actual: 401, _
503+
) {}
504+
XCTAssertEqual(appAttest.preparedAttestations.count, keys)
505+
}
506+
XCTAssertEqual(store.pending.first?.rejectedReplacements, 2)
491507
}
492508
}
493509

@@ -1219,7 +1235,9 @@ final class BuzzDevPushEnrollmentDriverTests: XCTestCase {
12191235
},
12201236
now: @escaping () -> Date = {
12211237
Date(timeIntervalSince1970: TimeInterval(BuzzDevPushEnrollmentDriverTests.now))
1222-
}
1238+
},
1239+
lifetimeSeconds: Int64 = BuzzDevPushEnrollmentDriverTests.expiresAt
1240+
- BuzzDevPushEnrollmentDriverTests.now
12231241
) throws -> BuzzDevPushEnrollmentDriver {
12241242
let configuration = URLSessionConfiguration.ephemeral
12251243
configuration.protocolClasses = [URLProtocolStub.self]
@@ -1229,7 +1247,7 @@ final class BuzzDevPushEnrollmentDriverTests: XCTestCase {
12291247
session: URLSession(configuration: configuration),
12301248
appAttest: appAttest,
12311249
now: now,
1232-
lifetimeSeconds: Self.expiresAt - Self.now,
1250+
lifetimeSeconds: lifetimeSeconds,
12331251
installationIdBytes: installationIdBytes
12341252
)
12351253
}

0 commit comments

Comments
 (0)