Commit Graph

2 Commits

Author SHA1 Message Date
Tom Brow
d4c0729a86 fix(mobile): Android push follow-ups from enrollment review (#9892)
Follow-ups from the review of #9811 (Android push enrollment): the
review items that were safe to leave for after merge.

- **A timed-out gateway call is now aborted.** The 15 s timeout used to
abandon the POST while it kept running, so a retry could race the
original request at the gateway. The gateway client now sends through
`boundedHttpResponse`, which aborts the request at the deadline (and
caps the reply size).
- **An Android gateway reply the app can't read is reported to
diagnostics,** as #9806 does for iOS. Malformed replies (no JSON object,
missing fields, or an oversized reply with the expected status) throw
`AndroidPushGatewayResponseInvalid`, which `enrollBuzzPush` reports
under the push gateway operation. Refusals and transport failures stay
unreported, and a refusal keeps its status even when its body is over
the cap.
- **The delegation sweep after enrollment keeps the enrolling relay
while it is still wanted.** It now sweeps with the desired relays plus
the enrolling origin, but only while `stillWanted()` holds, so an
opt-out that landed during the delegation still sees that delegation
revoked.
- **Tests for two paths that had none:** an opt-out that lands while the
gateway answers the delegation ends the attempt quietly (no lease,
delegation revoked, no failure retry), and an opted-out iOS community
enrolls nothing.

Two other items from the review, both Codex findings on an earlier head
of #9811 (reusing a revoked grant after a lost re-delegation; deleting a
key after a lost renewal), were already fixed and tested before #9811
merged, so nothing changes here for them.

### Validation

Added tests:
- [the enrolling relay keeps its delegation before the caller lists
it](157d6d4759/mobile/test/shared/push/android_push_authority_test.dart):
the sweep keeps a still-wanted relay that the desired list doesn't show
yet, and revokes a delegation whose community opted out while it was in
flight.
- [turning push off while it delegates ends the attempt
quietly](157d6d4759/mobile/test/features/settings/android_notifications_test.dart):
production bootstrap, opt-out while the delegation response is held.
- [only a gateway reply the app cannot read is
reported](157d6d4759/mobile/test/shared/push/push_bridge_android_test.dart)
and [a gateway call that times out is aborted, not
abandoned](157d6d4759/mobile/test/shared/push/push_bridge_android_test.dart).
- [a community that opted out enrolls
nothing](157d6d4759/mobile/test/shared/push/push_bridge_test.dart)
(iOS).
- [an unavailable gateway keeps the journal and a refusal clears
it](157d6d4759/mobile/test/shared/push/android_push_authority_test.dart):
now also with refusal bodies over the 64 KiB cap.

Each test was checked to fail with its guard removed.

No device or full-app run: every change is on the Android push gateway
path (which needs a gateway-configured Android build) or is a test, and
the behavior changed is timeout, malformed-reply and race handling that
the tests above drive directly against the production authority and
bootstrap.

### Regressions

- iOS is unchanged apart from the new test; the abort and diagnostics
changes are Android-only code.
- `boundedHttpResponse` gains an optional `timeout` that defaults to its
old 30 s, so existing callers behave as before.
- Gateway replies are capped at 64 KiB. Gateway responses are small JSON
objects, so an oversized reply with the expected status is itself a
defect and is reported. An oversized refusal is handled as that refusal
(a 503 stays retryable, a 403 discards the pending enrollment).

---------

Signed-off-by: Tom Brow <tomb@block.xyz>
Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@localhost>
2026-11-23 16:25:19 -08:00
Tom Brow
2e3bebe9b6 feat(mobile): enroll Android devices for push (#9811)
## Summary

Ticket 7 of the [Android push
plan](https://github.com/block/buzz/blob/main/PLANS/ANDROID_PUSH.md).
Android phones can now enroll with the push gateway and publish a relay
lease, through the same shared push layer iOS uses. The wake handling
side is ticket 6, [#9804 Android push wake
handling](https://github.com/block/buzz/pull/9804).

- The phone gets an FCM token, then generates a sign-only P-256 Keystore
key whose attestation challenge is the SHA-256 of the enrollment
transcript. The chain goes to the gateway at enrollment. Every later
installation call (delegate, rotate endpoint, revoke delegation, revoke
installation) is signed with that key over the transcript bytes the
gateway builds.
- A rotated FCM token rotates the endpoint. Signing out revokes the
delegation and the installation. The first enrollment waits a short
random time.
- Android 13+ asks for the notification permission at the same point iOS
does. Declining keeps the app working.
- A phone that cannot attest (no hardware attestation, unlocked
bootloader, unlisted boot key) keeps working, and Settings says push is
unavailable on this phone.
- The relay lease keeps the fixed `transport` value iOS sends.
- Firebase config is the same build-time setting [#9804 Android push
wake handling](https://github.com/block/buzz/pull/9804) adds: four
`BUZZ_FIREBASE_*` environment variables become the string resources
Firebase reads. Nothing is committed, and builds without them still work
(push reports unavailable). The Gradle hunks (permission, config block,
`firebase-messaging` dependency) are copied from that PR verbatim so the
two merge cleanly, including its `lifecycle-process` line, which this PR
does not use.
- The token and permission calls use their own `buzz/push_registration`
channel, because Android's notification-open handler from that PR
already owns `buzz/push` and a channel has one native handler.
- The gateway gets an `enroll_android` transcript vector so Dart and
Rust agree on the exact bytes.

Enrollment state is journaled before each network attempt, so a lost
response resumes with byte-identical requests instead of leaving an
orphaned installation.

### Related issue

None found. Builds on [#9560 enroll Android
installations](https://github.com/block/buzz/pull/9560) and [#9562
verify Key Attestation](https://github.com/block/buzz/pull/9562).

### Validation

Journey tests cover first enrollment, a second relay, reuse and renewal,
lost responses, token rotation, an installation the gateway dropped, a
lost Keystore key, a phone that cannot attest, and sign-out revocation,
against a fake gateway that keeps the real gateway's semantics.
Transcripts replay the shared vectors byte for byte. A settings journey
checks the Notifications row and the unavailable row on Android, and a
resume table checks the FCM token refresh: it runs while the relay
reconnects, but not when push is off or the relay doesn't offer push.

On a physical Pixel 9a (debug build, a local test gateway on a local
Postgres, reached over `adb reverse`), the real Keystore key and
hardware attestation chain enrolled and delegated, and the gateway
accepted the attestation. The same session then ran: a signed delivery
authorized against the delegation, endpoint rotation and re-delegation
at the next epoch, rotating back, delegation revocation, installation
revocation, and re-enrolling the same FCM token afterwards.

The notification prompt appears when registration starts, before the
token is fetched:

<img
src="https://github.com/user-attachments/assets/2aed92fe-20c1-4658-b990-51c3f3c96120"
width="270" alt="Android notification permission prompt">

Not validated on the device: the FCM wake itself (the local gateway has
no credentials to call FCM, so the delivery was authorized and then
failed at the FCM hop), lease publication to a relay, and the in-app
unavailable row, which only the widget tests exercise. A relay-backed
run needs a signed-in test community on the phone.

### Settings while turning off

Turning notifications off sends a relay tombstone. Until the relay
accepts it, the row now says so, on both platforms. An Android build
without a gateway also keeps a saved opt-in visible so it can still be
turned off; once off, the row goes away. These are rendered from the
Settings journey test.

| | Before | After |
|---|---|---|
| Android build without a gateway, saved opt-in | <img
src="https://raw.githubusercontent.com/block/buzz/47220acf78ad21639d2a3a8b79be3f0272e74db2/pr-9811--01-before-config-less.png"
width="270" alt="Before: no Notifications row"> | <img
src="https://raw.githubusercontent.com/block/buzz/47220acf78ad21639d2a3a8b79be3f0272e74db2/pr-9811--02-after-config-less.png"
width="270" alt="After: Notifications row with the switch on"> |
| After turning off, relay has not accepted yet | <img
src="https://raw.githubusercontent.com/block/buzz/47220acf78ad21639d2a3a8b79be3f0272e74db2/pr-9811--03-before-turning-off.png"
width="270" alt="Before: plain off switch"> | <img
src="https://raw.githubusercontent.com/block/buzz/47220acf78ad21639d2a3a8b79be3f0272e74db2/pr-9811--04-after-turning-off.png"
width="270" alt="After: Turning off. Notifications may continue until
this finishes."> |

### Regressions

- iOS enrollment is unchanged: the shared bridge only branches to the
Dart authority on Android. iOS gains the pending opt-out subtitle, and
its opt-out now also tombstones after a gateway change using the saved
lease id.
- The Android app gains the `firebase-messaging` dependency and a
`POST_NOTIFICATIONS` permission. Backups exclude the push preferences
file.



### Known limits

- An FCM token rotation while the app is running is picked up on the
next resume or launch, because the wake service owns the single
messaging service. It could call into the registration plugin once both
Android push PRs merge.
- After an endpoint rotation, only the active community's lease is
republished. Other enabled communities' grants are void until each is
opened, the same way their leases lapse at grant expiry today.

---------

Signed-off-by: Tom Brow <tomb@block.xyz>
Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@buzz.block.builderlab.xyz>
Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@localhost>
2026-11-23 09:35:56 -07:00