mirror of
https://github.com/block/buzz.git
synced 2026-11-27 19:52:31 +00:00
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>
109 lines
3.6 KiB
Dart
109 lines
3.6 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:http/http.dart' as http;
|
|
|
|
import '../hosted/bounded_http_response.dart';
|
|
import '../http/app_http_client.dart';
|
|
|
|
/// The gateway answered with a refusal. Signed installation calls answer every
|
|
/// refusal with `404 not_authorized`, so [status] and [code] say what the
|
|
/// gateway said and nothing more.
|
|
class AndroidPushGatewayRejection implements Exception {
|
|
const AndroidPushGatewayRejection(this.route, this.status, this.code);
|
|
|
|
final String route;
|
|
final int status;
|
|
final String? code;
|
|
|
|
bool get isNotAuthorized => status == 404 && code == 'not_authorized';
|
|
|
|
/// A refusal of the attestation itself, which only a different phone or boot
|
|
/// state changes.
|
|
bool get isInvalidAttestation =>
|
|
status == 401 && code == 'invalid_attestation';
|
|
|
|
/// Refusals worth retrying later with the same request: throttling and
|
|
/// gateway-side trouble. Every other refusal is final for that request.
|
|
bool get isTransient => status == 429 || status >= 500;
|
|
|
|
@override
|
|
String toString() => 'Push gateway refused $route: $status ${code ?? ''}';
|
|
}
|
|
|
|
/// The gateway answered with something the app cannot read: no JSON object,
|
|
/// or one missing a field the call needs. Unlike a refusal, this points at a
|
|
/// gateway or protocol defect, so it is reported to diagnostics.
|
|
class AndroidPushGatewayResponseInvalid extends FormatException {
|
|
const AndroidPushGatewayResponseInvalid(super.message);
|
|
}
|
|
|
|
/// One round trip to the gateway's JSON API.
|
|
class AndroidPushGatewayClient {
|
|
AndroidPushGatewayClient({
|
|
required this.baseUrl,
|
|
http.Client? client,
|
|
this.timeout = const Duration(seconds: 15),
|
|
}) : _client = client ?? newAppHttpClient();
|
|
|
|
final Uri baseUrl;
|
|
final Duration timeout;
|
|
final http.Client _client;
|
|
|
|
/// Posts [body] to [route] and returns the JSON object answered with
|
|
/// [expectedStatus]. Transport failures propagate unchanged. At [timeout]
|
|
/// the request is aborted, not just abandoned, so a retry never races a
|
|
/// copy of itself still on its way to the gateway.
|
|
Future<Map<String, Object?>> post(
|
|
String route,
|
|
Map<String, Object?> body, {
|
|
int expectedStatus = 200,
|
|
}) async {
|
|
final http.Response response;
|
|
int? status;
|
|
try {
|
|
response = await boundedHttpResponse(
|
|
_client,
|
|
http.Request('POST', baseUrl.resolve(route))
|
|
..headers['content-type'] = 'application/json'
|
|
..body = jsonEncode(body),
|
|
timeout: timeout,
|
|
onHeaders: (headers) => status = headers.statusCode,
|
|
);
|
|
} on FormatException catch (error) {
|
|
// The only one a bounded read throws: a body over its limit. A refusal
|
|
// keeps its status, so an oversized 503 still reads as transient.
|
|
if (status case final status? when status != expectedStatus) {
|
|
throw AndroidPushGatewayRejection(route, status, null);
|
|
}
|
|
throw AndroidPushGatewayResponseInvalid(
|
|
'Push gateway answered $route unreadably: ${error.message}',
|
|
);
|
|
}
|
|
final decoded = _object(response.bodyBytes);
|
|
if (response.statusCode != expectedStatus) {
|
|
throw AndroidPushGatewayRejection(
|
|
route,
|
|
response.statusCode,
|
|
decoded?['error'] as String?,
|
|
);
|
|
}
|
|
if (decoded == null) {
|
|
throw AndroidPushGatewayResponseInvalid(
|
|
'Push gateway answered $route without a JSON object.',
|
|
);
|
|
}
|
|
return decoded;
|
|
}
|
|
|
|
Map<String, Object?>? _object(List<int> bytes) {
|
|
try {
|
|
final value = jsonDecode(utf8.decode(bytes));
|
|
return value is Map<String, Object?> ? value : null;
|
|
} on FormatException {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
void close() => _client.close();
|
|
}
|