mirror of
https://github.com/block/buzz.git
synced 2026-11-26 19:21:50 +00:00
Mobile read GET /api/join-policy through the generic 64 KiB hosted response cap, but the relay allows each policy markdown document up to 256 KiB and Desktop accepts a 4 MiB policy response. A self-hosted relay with a valid policy over 64 KiB was joinable on Desktop but failed on Mobile with an invalid-response error before consent could render, and every retry hit the same cap. boundedHttpResponse and readJoinResponse now take a maxBytes that defaults to the existing 64 KiB hostedResponseMaxBytes. Only loadJoinPolicy opts into the 4 MiB cap that matches Desktop, so the accept-policy, claim, and other hosted replies keep their limit. The reader accumulates into a BytesBuilder so a multi-MiB body is not held as a list of ints. The invite consent journey now serves a policy over 64 KiB and asserts that Mobile loads its full terms and claims only after native acceptance. The hosted-admission "oversized policy" row used bare whitespace, which failed as invalid JSON whatever the cap was. It now sends valid JSON padded past 4 MiB. A focused test serves a relay-sized policy alongside an accept-policy reply over 64 KiB: the policy reaches consent, the reply is rejected, and no claim is sent. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Matt Toohey <contact@matttoohey.com>
167 lines
5.0 KiB
Dart
167 lines
5.0 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:http/http.dart' as http;
|
|
|
|
import 'builderlab_account.dart';
|
|
import 'bounded_http_response.dart';
|
|
|
|
/// Each relay policy document is capped at 256 KiB before JSON encoding. Four
|
|
/// MiB covers two maximally escaped documents plus the envelope, as on Desktop.
|
|
const _joinPolicyResponseMaxBytes = 4 * 1024 * 1024;
|
|
|
|
/// Public policy text bound to the exact version the user accepts.
|
|
class HostedJoinPolicy {
|
|
const HostedJoinPolicy({
|
|
required this.version,
|
|
required this.ageRequired,
|
|
this.terms,
|
|
this.privacy,
|
|
});
|
|
final String version;
|
|
final bool ageRequired;
|
|
final String? terms;
|
|
final String? privacy;
|
|
}
|
|
|
|
enum HostedInviteFailure {
|
|
policyRequired,
|
|
expired,
|
|
exhausted,
|
|
invalid,
|
|
restricted,
|
|
ownerMismatch,
|
|
}
|
|
|
|
/// Reads only a bounded JSON document, never follows redirects or logs bodies.
|
|
Future<Map<String, dynamic>> readJoinResponse(
|
|
http.Client client,
|
|
http.Request request, {
|
|
bool allowNotFound = false,
|
|
int maxBytes = hostedResponseMaxBytes,
|
|
}) async {
|
|
final http.Response response;
|
|
final Map<String, dynamic> data;
|
|
try {
|
|
response = await boundedHttpResponse(client, request, maxBytes: maxBytes);
|
|
if (allowNotFound && response.statusCode == 404) return {};
|
|
if (response.statusCode == 429 ||
|
|
(response.statusCode >= 500 && response.statusCode < 600)) {
|
|
throw AccountFailure.unavailable;
|
|
}
|
|
data = jsonDecode(utf8.decode(response.bodyBytes)) as Map<String, dynamic>;
|
|
} on FormatException {
|
|
throw AccountFailure.invalidResponse;
|
|
} on TypeError {
|
|
throw AccountFailure.invalidResponse;
|
|
}
|
|
if (response.statusCode < 200 || response.statusCode >= 300) {
|
|
throw switch (data['error']) {
|
|
'join_policy_required' ||
|
|
'join_policy_not_accepted' => HostedInviteFailure.policyRequired,
|
|
'invite_expired' => HostedInviteFailure.expired,
|
|
'invite_exhausted' => HostedInviteFailure.exhausted,
|
|
'invite_invalid' => HostedInviteFailure.invalid,
|
|
'invite_restricted' => HostedInviteFailure.restricted,
|
|
'invite_owner_mismatch' => HostedInviteFailure.ownerMismatch,
|
|
_ => AccountFailure.denied,
|
|
};
|
|
}
|
|
return data;
|
|
}
|
|
|
|
Future<String?> freshJoinPolicyReceipt({
|
|
required http.Client client,
|
|
required Uri origin,
|
|
required String code,
|
|
required Future<bool> Function(HostedJoinPolicy) confirm,
|
|
required void Function() checkCurrent,
|
|
bool allowLegacyRelay = false,
|
|
}) async {
|
|
Future<Map<String, dynamic>> send(
|
|
String method,
|
|
String path, [
|
|
Map<String, dynamic>? body,
|
|
]) async {
|
|
checkCurrent();
|
|
final request = http.Request(
|
|
method,
|
|
origin.replace(
|
|
scheme: origin.scheme == 'ws' || origin.scheme == 'http'
|
|
? 'http'
|
|
: 'https',
|
|
path: path,
|
|
),
|
|
)..followRedirects = false;
|
|
if (body != null) {
|
|
request.headers['Content-Type'] = 'application/json';
|
|
request.body = jsonEncode(body);
|
|
}
|
|
final data = await readJoinResponse(client, request);
|
|
checkCurrent();
|
|
return data;
|
|
}
|
|
|
|
checkCurrent();
|
|
final policy = await loadJoinPolicy(
|
|
client: client,
|
|
origin: origin,
|
|
allowLegacyRelay: allowLegacyRelay,
|
|
);
|
|
checkCurrent();
|
|
if (policy == null) return null;
|
|
if (!await confirm(policy)) throw HostedInviteFailure.policyRequired;
|
|
checkCurrent();
|
|
final accepted = await send('POST', '/api/invites/accept-policy', {
|
|
'code': code,
|
|
'policy_version': policy.version,
|
|
'age_confirmed': policy.ageRequired,
|
|
});
|
|
final receipt = accepted['receipt'];
|
|
if (receipt is! String || receipt.isEmpty || receipt.length > 8192) {
|
|
throw AccountFailure.invalidResponse;
|
|
}
|
|
return receipt;
|
|
}
|
|
|
|
/// Fetch a policy without accepting it; legacy self-hosted relays may omit it.
|
|
Future<HostedJoinPolicy?> loadJoinPolicy({
|
|
required http.Client client,
|
|
required Uri origin,
|
|
bool allowLegacyRelay = false,
|
|
}) async {
|
|
final request = http.Request(
|
|
'GET',
|
|
origin.replace(
|
|
scheme: origin.scheme == 'ws' || origin.scheme == 'http'
|
|
? 'http'
|
|
: 'https',
|
|
path: '/api/join-policy',
|
|
),
|
|
)..followRedirects = false;
|
|
// The relay permits two 256 KiB Markdown documents. Allow their JSON
|
|
// escaping (up to six bytes per input byte), plus the policy envelope.
|
|
// Receipt and claim responses retain the ordinary 64 KiB bound.
|
|
final data = await readJoinResponse(
|
|
client,
|
|
request,
|
|
allowNotFound: allowLegacyRelay,
|
|
maxBytes: _joinPolicyResponseMaxBytes,
|
|
);
|
|
final raw = data['policy'];
|
|
if (raw == null) return null;
|
|
if (raw is! Map ||
|
|
raw['version'] is! String ||
|
|
(raw['version'] as String).isEmpty ||
|
|
raw['age_attestation_required'] is! bool ||
|
|
(raw['terms_markdown'] != null && raw['terms_markdown'] is! String) ||
|
|
(raw['privacy_markdown'] != null && raw['privacy_markdown'] is! String)) {
|
|
throw AccountFailure.invalidResponse;
|
|
}
|
|
return HostedJoinPolicy(
|
|
version: raw['version'] as String,
|
|
ageRequired: raw['age_attestation_required'] as bool,
|
|
terms: raw['terms_markdown'] as String?,
|
|
privacy: raw['privacy_markdown'] as String?,
|
|
);
|
|
}
|