Skip to content

Commit 33a779f

Browse files
matt2eclaude
andcommitted
fix(mobile): allow relay-sized join policy responses
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 <[email protected]> Signed-off-by: Matt Toohey <[email protected]>
1 parent 099a0bf commit 33a779f

4 files changed

Lines changed: 62 additions & 8 deletions

File tree

‎mobile/lib/shared/hosted/bounded_http_response.dart‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
import 'dart:async';
2+
import 'dart:typed_data';
23

34
import 'package:http/http.dart' as http;
45

6+
/// The body cap for hosted responses that do not declare their own.
7+
const hostedResponseMaxBytes = 64 * 1024;
8+
59
/// One deadline covers headers and the bounded body. Abort only this request,
610
/// leaving the shared client usable for sign-out and subsequent attempts.
711
Future<http.Response> boundedHttpResponse(
812
http.Client client,
913
http.Request source, {
10-
int maxBytes = 65536,
14+
int maxBytes = hostedResponseMaxBytes,
1115
}) async {
1216
final abort = Completer<void>();
1317
final request =
@@ -27,15 +31,15 @@ Future<http.Response> boundedHttpResponse(
2731
await iterator.cancel();
2832
throw http.RequestAbortedException();
2933
}
30-
final bytes = <int>[];
34+
final bytes = BytesBuilder();
3135
while (await iterator.moveNext()) {
3236
if (bytes.length + iterator.current.length > maxBytes) {
3337
throw const FormatException('Response too large');
3438
}
35-
bytes.addAll(iterator.current);
39+
bytes.add(iterator.current);
3640
}
3741
return http.Response.bytes(
38-
bytes,
42+
bytes.takeBytes(),
3943
response.statusCode,
4044
headers: response.headers,
4145
);

‎mobile/lib/shared/hosted/hosted_join_policy.dart‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ import 'package:http/http.dart' as http;
55
import 'builderlab_account.dart';
66
import 'bounded_http_response.dart';
77

8+
/// Each relay policy document is capped at 256 KiB before JSON encoding. Four
9+
/// MiB covers two maximally escaped documents plus the envelope, as on Desktop.
10+
const _joinPolicyResponseMaxBytes = 4 * 1024 * 1024;
11+
812
/// Public policy text bound to the exact version the user accepts.
913
class HostedJoinPolicy {
1014
const HostedJoinPolicy({
@@ -33,7 +37,7 @@ Future<Map<String, dynamic>> readJoinResponse(
3337
http.Client client,
3438
http.Request request, {
3539
bool allowNotFound = false,
36-
int maxBytes = 65536,
40+
int maxBytes = hostedResponseMaxBytes,
3741
}) async {
3842
final http.Response response;
3943
final Map<String, dynamic> data;
@@ -141,7 +145,7 @@ Future<HostedJoinPolicy?> loadJoinPolicy({
141145
client,
142146
request,
143147
allowNotFound: allowLegacyRelay,
144-
maxBytes: 4 * 1024 * 1024,
148+
maxBytes: _joinPolicyResponseMaxBytes,
145149
);
146150
final raw = data['policy'];
147151
if (raw == null) return null;

‎mobile/test/features/invites/invite_join_provider_test.dart‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ void main() {
2525
final auth = _RecordingAuthNotifier();
2626
final requests = <String>[];
2727
var version = 'v1';
28+
// Relay-valid policy text can exceed the generic 64 KiB hosted cap.
29+
final terms = 'x' * 65536;
2830
final container = ProviderContainer(
2931
overrides: [
3032
communityStorageProvider.overrideWithValue(storage),
@@ -40,7 +42,7 @@ void main() {
4042
jsonEncode({
4143
'policy': {
4244
'version': version,
43-
'terms_markdown': 'Terms',
45+
'terms_markdown': terms,
4446
'age_attestation_required': true,
4547
},
4648
}),
@@ -78,6 +80,7 @@ void main() {
7880
);
7981
await _joinPolicySettled(container);
8082
expect(container.read(inviteJoinProvider).joinPolicy?.version, 'v1');
83+
expect(container.read(inviteJoinProvider).joinPolicy?.terms, terms);
8184
await notifier.confirmJoin();
8285
expect(requests.where((path) => path != '/api/join-policy'), isEmpty);
8386
version = 'v2';

‎mobile/test/shared/hosted/hosted_admission_test.dart‎

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ void main() {
110110
('redirect', '{}', 302, AccountFailure.denied),
111111
(
112112
'oversized policy',
113-
'{}${' ' * (4 * 1024 * 1024)}',
113+
'{"policy":null}${' ' * (4 * 1024 * 1024)}',
114114
200,
115115
AccountFailure.invalidResponse,
116116
),
@@ -143,6 +143,49 @@ void main() {
143143
});
144144
}
145145

146+
test('only policy discovery admits relay-sized responses', () async {
147+
await store.create(keys.nsec);
148+
await backup();
149+
final terms = 'x' * 65536;
150+
HostedJoinPolicy? confirmed;
151+
admission.dispose();
152+
admission = HostedAdmission(
153+
account: account,
154+
relayAuth: HostedRelayAuth(account, enabled: false),
155+
store: store,
156+
probe: (_) async {},
157+
confirmPolicy: (policy) async {
158+
confirmed = policy;
159+
return true;
160+
},
161+
client: MockClient((request) async {
162+
if (request.url.path == '/api/join-policy') {
163+
return http.Response(
164+
jsonEncode({
165+
'policy': {
166+
'version': 'v1',
167+
'age_attestation_required': false,
168+
'terms_markdown': terms,
169+
},
170+
}),
171+
200,
172+
);
173+
}
174+
if (request.url.path == '/api/invites/accept-policy') {
175+
return http.Response('{"receipt":"fresh"}${' ' * 65536}', 200);
176+
}
177+
claims.add(request);
178+
return http.Response('{}', 200);
179+
}),
180+
);
181+
await expectLater(
182+
admission.prepare(invite, 'owner', keys.public),
183+
throwsA(AccountFailure.invalidResponse),
184+
);
185+
expect(confirmed?.terms, terms);
186+
expect(claims, isEmpty);
187+
});
188+
146189
testWidgets('total policy deadline cancels body and permits policy retry', (
147190
tester,
148191
) async {

0 commit comments

Comments
 (0)