Repository navigation
feat(push): register Apple push through v2 - #393
Conversation
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Advanced Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Quick104 has reached the 50-credit limit for trial accounts. To continue receiving code reviews, upgrade your plan.
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
Code review summaryReviewed head: Process: an adversarial review of this PR's diff with each candidate finding independently verified before any fix, a simplify pass for medium and larger PRs, and a whole-stack review across contract conformance, authority and failure handling, data integrity, user-visible behavior and cross-PR integration. Verified findings, all fixed on this branch:
Known deviation from the plan: the push installation key is stored in the shared Keychain access group, the same group that already holds the account tokens. Moving it to an app-only group would need a new entitlement and a migration, with no practical security gain. Validation: this branch builds on its own for iOS, tvOS and macOS, and its focused tests passed. The completed stack passed the full iOS suite (1981 tests) and tvOS suite (1792 tests) with 0 failures, the macOS build and the v1 path guard. The final stack was also exercised on iOS and tvOS simulators against a v1/v2 bridge server: sign-in (password and QR), restore, browse, search, detail, favorites and watchlist, playback (direct play, transcode, subtitles, stop), audiobooks, downloads with offline playback, settings, requests, profiles, push registration and diagnostics. Server logs showed no Remaining: no open verified findings. Merge in stack order. Mergeability: 8/10 — conditional. The approach holds up and every verified finding is fixed. Conditions: this PR's CI must pass (it was pending at review time), and the owner still plans a physical-device check of the completed stack. Review by Claude Opus 5.5 ( |
27dac38 to
5b65565
Compare
308d4ae to
5f61261
Compare
5b65565 to
6e408ef
Compare
The app still registered its APNs token with POST /api/v1/devices/push/apple. The v2 operation is an ordered installation protocol: every request carries a secret per-server installation key and a strictly increasing generation, and a lost answer must be replayed with the same generation and body. Nothing on main generated, stored or sent either value, and the v2 client methods had no callers. Register through registerApplePushDevice instead. A new Keychain journal holds one record per server: the installation key (32 random bytes, base64url, created once and never rotated) and the latest intent with its generation and outcome. A new intent (APNs token, account, login or profile change) takes the next generation, persisted in the same TokenStore turn as the owner check and before the request is sent. A lost answer, 401, 408, 429 or 5xx keeps the intent pending and the next trigger replays it exactly. 400, 403, 404, 406, 409, 413, 415, 422 and a receipt for another generation are recorded as refusals and not resent; there is no generation rebase and no new key. The capability read gates each send on allowed, state available and registration_available. Display tokens are stored or cleared under the durable owner, and removed or disabled registrations are not renewed. Delete the v1 endpoint, request and response models and the v1 receipt test. Stack-PR: W5-T Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Stack-PR: W5-T Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Every 403 was recorded as a durable refusal, and so was any refusal of a display-token renewal. The server answers 403 permission_denied both for a bad installation proof and for a login-authority check that can fail transiently, so one slow lookup stopped registration for the rest of the login. A renewal is an exact replay the server never acts on, so recording its refusal made the journal forget a working accepted registration, and the display token was never renewed again. A 403, and a renewal refusal other than 409, now hold the generation in memory for six hours (the existing no-display-token back-off, now keyed per server) and leave the journal as it was: a pending intent stays pending and an accepted one stays accepted. After the hold the same key, generation and body are sent again. A 409 on a renewal is still recorded as a refusal, because the server has moved past that generation. Stack-PR: W5-T Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
5f61261 to
17c0244
Compare
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
The iOS app now registers its APNs token through the ordered v2 push protocol, with a persisted per-server installation key and generation journal.
Problem
The iOS app still registers its APNs token with
POST /api/v1/devices/push/apple(rows 127 and 137). The v2 operation,registerApplePushDevice, is not a path swap. It is an ordered installation protocol with two more required headers:X-Push-Installation-Key: a secret 32-byte key in unpadded base64url (43 characters). It is persisted per server and device installation, kept across account and profile switches, and never rotated.X-Push-Generation: a positive decimal int64. It increases strictly for each new registration intent and is reused exactly, with the same body and owner, after an uncertain outcome.The server keys the installation by
device_id. It answers 403 when the key does not match the one it stored, and 409 when the generation goes backwards or reuses a generation for a different intent. The contract has no automatic recovery for a lost key or a reset counter.On main,
APIv2Client.registerApplePushandapplePushRegistrationCapabilityhad no callers. Nothing generated, stored or sent a key or a generation. The coordinator also had a v1-only 404/405 fallback (endpointUnsupportedForContext).Solution
Notifications/ApplePushInstallationJournal.swift): one Keychain item per server registry ID. It holds the installation key and the latest intent with its generation and outcome (pending,accepted, orrefused). An intent is the durable owner (server, verified account ID, account epoch, profile) plus the exact request body. The item uses the same Keychain group and accessibility as the device ID fromAppleDeviceIdentity, so the two are kept or lost together. A missing item gets a fresh key, but only on its first allocation. An unreadable or undecodable item is never overwritten. Registration for that server stops, and the reason is logged.ApplePushInstallationJournal.plan):TokenStore.withCurrentDurableAuthority, before the request is sent, so a generation is never allocated for an owner that has already been replaced.ApplePushRegistrar, an actor inApplePushRegistration.swift): checks the body against theApplePushRegistrationBodyschema before allocating, so a build with another bundle ID never uses up a generation. Next it reads the capability and requiresallowed,state == "available"andregistration_available. The revision stays opaque, so the legacyordered_apple_v1comparison is not used. Then it sends the registration and records the outcome. The actor keeps Keychain I/O off the main actor.APIv2Client.registerApplePush): sends both ordered headers.HTTPClientaddsX-Profile-Id, andX-Profile-Tokenfor PIN-locked profiles, from the captured owner. The existingwithOwnerFenceinapplePushRequestguards both calls. The client throwsApplePushRegistrationError.invalidReceiptunless the receipt names the sent generation and push mode and has non-emptyidandserver_device_id.withCurrentDurableAuthority, in the same TokenStore turn as the owner check. A late answer therefore cannot write into a slot that a sign-out or profile switch already cleared. On main, the write ran after the fence returned. A disabled or removed registration clears the token.hasCurrentToken(forServerID:)now also checks which server issued the token. The six-hour back-off for servers that return no token stays, and now also covers 403s and refused renewals.TokenStore.captureDurableAccountAuth(). A call that arrives while a registration is running queues one more pass, so two sends never race for a generation.APIv2ApplePushRegistrationand the nestedApplePushCapabilitymoved intoAPIv2PushModels.swiftasAPIv2ApplePushRegistrationReceiptandAPIv2ApplePushCapability(all four schema fields).The Android reference is
AndroidPushRegistrar.kt:15-123. This differs from Android in two ways:blockedScopesjournal was not harvested.Failure outcomes
registerApplePushDeviceisx-silo-retry-safety: domain_identity. The contract (docs/notifications-push-api.md) asks for an exact replay after uncertainty and forbids a new generation just to retry.acceptedwith the registration ID,enabledandremoved. The display token is stored, or cleared when the registration is disabled or removed. A later trigger sends nothing unless the display token needs renewal.refusedwith the status and problem type, and the real error is logged once. The same intent is never resent. The next changed intent takes the next generation with the same key. No rebase, no new key.permission_deniedboth for a bad installation proof and for a login-authority check that can fail transiently (a 2-second lookup timeout, an access token that expires during the lock wait, a stale role claim). Any refusal of a display-token renewal other than 409, including an invalid receiptpending, an accepted one staysaccepted, because the server never changes a registration for an exact replay. The generation is held in memory for six hours, then the same key, generation and body are sent again. A changed intent takes the next generation right away.authorityChanged)pendingwith its generation. No display token is written. The next trigger under the same durable owner replays it exactly. A new intent supersedes it with the next generation.Lost key. The record and the device ID share one Keychain group and accessibility, so a Keychain reset normally loses both. The server then sees a new device ID, which counts as a new installation. If only the record is lost, every later registration for this device on that server fails with 403, or with 409 when the generation falls behind. The client records a 409 as a refusal. It holds a 403 and retries it every six hours (or on relaunch), because the server uses the same problem type for a transient login-authority failure. It does not guess replacement credentials. If the record exists but cannot be read or decoded, the client leaves it in place and sends nothing for that server. The Notification Service extension falls back to the access token for display fetches. Recovery needs a server-side way to drop this device's installation. No v2 operation for that exists for Apple.
Validation
ApplePushOrderedRegistrationTests(19 tests),ApplePushRegistrationTests,CredentialIdentityTestsandAPIv2ContractTests, 0 failures. Push code is iOS-only: the new test file compiles to nothing on tvOS, andApplePushRegistrationTestsis excluded from SiloTVTests. On tvOS,CredentialIdentityTestsandAPIv2ContractTestspass (34 tests).scripts/ci/check-no-api-v1.shpasses.ApplePushRegistration.swiftno longer contains a v1 path. The one v1 literal left inApplePushRegistrationTests.swiftis the display URL test, which W5-U owns.ApplePushOrderedRegistrationTests, onStubURLProtocolwith a realTokenStoreandHTTPClient) cover:"1", andX-Profile-Id/X-Profile-Token.accepted, and the renewal is sent again after the hold. A 409 on a renewal is recorded as a refusal.Risks and follow-ups
SharedStorage.keychainAccessGroup, user-independent audience), the same group that holds the account tokens and the device ID. Every app and extension in that group can read it; it is not an app-private item.captureDurableAccountAuth(), which needs a verified account ID on the session. W1-B (restore validation throughaccount/me) and W5-A (binding fromTokenPair.user.id), earlier in the stack, supply it; a session installed without one does not register. Temporary playback credentials never register, as before.device_idalone. Two registry entries for one deployment (for example LAN and public addresses) get two keys, so the second one to register gets 403 and is held and retried every six hours. Registration still works from the first address.verifiedServerIdis self-asserted and never authorizes anything, so it is not used to share a secret. Android keys by server URL in the same way. A follow-up could share the record across entries once the deployment identity is verified.AndroidPushRegistrar.kt:115) records 401 and 403 as durable refusals and has the same problem. The server could giveErrPushInstallationProofits own problem type so clients can refuse it durably.APIv2Client.swift, inside the existing iOS-only block where main already had them, instead of moving to anAPIv2Client+Push.swiftextension. New wire models are inAPIv2PushModels.swift.withOwnerFence|sameCredentialIdentityiniosApp/iosAppdrop from 69 to 67 on the stacked branch: one call and one comment that described it. The call moved from the coordinator into the existingapplePushRequestfence, which now guards both push calls. The path also gains twowithCurrentDurableAuthoritychecks (1 to 3 in the tree), one for the allocation write and one for the display-token write.AI disclosure
Code and description written by Claude Opus 5.5 (
claude-opus-5-5) agents in Claude Code (T3 Code), orchestrated by a Claude Code workflow.Part 45 of 50 in the Apple API v2 stack. Base:
apiv2-stack/44-subtitle-ai-jobs-v2. Merge in order from the bottom of the stack. The plan and full index are in PR 01.🤖 Generated with Claude Code