Skip to content

Commit d5a9ecf

Browse files
browstudioclaude
committed
fix(mobile): keep the hosted session from leaking through redirects or flag-off builds
The extension's downloader dropped only Authorization when following a redirect, so the identity check could forward X-BB-Session-Credential to whatever host the account origin redirected to. It now refuses redirects for any request carrying a relay proof, an assertion or the account session. A build without hosted relay auth installed no session callback, so a hosted session an earlier flag-on build shared stayed in the keychain and the extension could keep minting after sign-out. Such builds now clear it. Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@localhost> Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Tom Brow <[email protected]>
1 parent 6dbb15a commit d5a9ecf

4 files changed

Lines changed: 54 additions & 10 deletions

File tree

‎mobile/ios/BuzzPushKit/Sources/BuzzPushKit/BuzzNotificationDownload.swift‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,13 @@ final class BuzzNotificationDownload: NSObject, URLSessionDataDelegate, @uncheck
8484
willPerformHTTPRedirection response: HTTPURLResponse, newRequest request: URLRequest,
8585
completionHandler: @escaping (URLRequest?) -> Void
8686
) {
87-
// Signed relay requests must not follow redirects or forward their proof.
88-
guard task.originalRequest?.value(forHTTPHeaderField: "Authorization") == nil,
87+
// Signed relay requests and account requests must not follow redirects or
88+
// forward their proof or session.
89+
let original = task.originalRequest
90+
guard
91+
["Authorization", "X-BB-Session-Credential", "Nostr-Federated-Identity"].allSatisfy({
92+
original?.value(forHTTPHeaderField: $0) == nil
93+
}),
8994
let url = request.url, ["https", "http"].contains(url.scheme?.lowercased() ?? ""),
9095
url.user == nil, url.password == nil,
9196
!(task.originalRequest?.url?.scheme == "https" && url.scheme != "https")

‎mobile/ios/BuzzPushKit/Tests/BuzzPushKitTests/BuzzPushNotificationResolverTests.swift‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1298,6 +1298,20 @@ final class BuzzPushNotificationResolverTests: XCTestCase {
12981298
// Signing out of the hosted account revokes delivery of what its assertion fetched.
12991299
sharedHosted = nil
13001300
XCTAssertFalse(enforced.isStillAuthorized())
1301+
1302+
// An account origin that redirects never gets the session forwarded: the redirect is
1303+
// refused, so the relay stub above never sees the credential.
1304+
let redirecting = BuzzRelayAssertionIssuer(
1305+
session: URLSession(configuration: configuration), loadSession: { nil },
1306+
loadHostedSession: { hosted }, now: { clock })
1307+
URLProtocolStub.redirect = { request in
1308+
request.url?.host == "app.example" ? URL(string: "https://elsewhere.example/steal") : nil
1309+
}
1310+
defer { URLProtocolStub.redirect = nil }
1311+
XCTAssertNotEqual(resolve(makeResolver(
1312+
communitiesData: try snapshotData([community(relayUrl: "wss://team.communities.example")]),
1313+
now: clock, assertionIssuer: redirecting))?.body, "Hosted hello")
1314+
XCTAssertNil(relayAssertions["elsewhere.example"])
13011315
}
13021316

13031317
/// A refused or malformed issuance never produces a header.

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,16 @@ import '../push/push_bridge.dart';
1515
final builderlabAccountProvider = Provider<BuilderlabAccountClient>((ref) {
1616
final account = BuilderlabAccountClient(
1717
// Hosted communities that enforce NIP-FI need the extension to mint too.
18-
onSessionChanged: HostedRelayAuth.configured && HostedEnvironment.enabled
19-
? (credential) => shareHostedSessionWithNotifications(
20-
origin: HostedEnvironment.origin,
21-
credential: credential,
22-
relayDomains: HostedEnvironment.authDomains,
23-
communityDomain: HostedEnvironment.communitySuffix,
24-
)
25-
: null,
18+
// A build without hosted relay auth still clears a copy an earlier build
19+
// shared, so the extension cannot keep minting after sign-out.
20+
onSessionChanged: (credential) => shareHostedSessionWithNotifications(
21+
origin: HostedEnvironment.origin,
22+
credential: HostedRelayAuth.configured && HostedEnvironment.enabled
23+
? credential
24+
: null,
25+
relayDomains: HostedEnvironment.authDomains,
26+
communityDomain: HostedEnvironment.communitySuffix,
27+
),
2628
);
2729
ref.onDispose(account.dispose);
2830
return account;

‎mobile/test/shared/push/push_bridge_test.dart‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@ import 'dart:async';
22

33
import 'package:buzz/shared/community/community.dart';
44
import 'package:buzz/shared/deeplink/deep_link.dart';
5+
import 'package:buzz/shared/hosted/hosted_environment.dart';
6+
import 'package:buzz/shared/hosted/hosted_relay_auth.dart';
57
import 'package:buzz/shared/push/push_bridge.dart';
68
import 'package:buzz/shared/relay/relay_provider.dart';
79
import 'package:flutter/foundation.dart';
810
import 'package:flutter/services.dart';
911
import 'package:flutter_test/flutter_test.dart';
12+
import 'package:hooks_riverpod/hooks_riverpod.dart';
1013

1114
const _channel = MethodChannel('buzz/push');
1215

@@ -207,6 +210,26 @@ void main() {
207210
]);
208211
});
209212

213+
test(
214+
'a build without hosted relay auth clears a shared hosted session',
215+
() async {
216+
expect(HostedRelayAuth.configured && HostedEnvironment.enabled, isFalse);
217+
debugDefaultTargetPlatformOverride = TargetPlatform.iOS;
218+
final shared = <Object?>[];
219+
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
220+
.setMockMethodCallHandler(_channel, (call) async {
221+
shared.add((call.arguments as Map)['credential']);
222+
return null;
223+
});
224+
final container = ProviderContainer();
225+
addTearDown(container.dispose);
226+
227+
// A copy an earlier flag-on build shared is removed instead of kept.
228+
await container.read(builderlabAccountProvider).onSessionChanged!('live');
229+
expect(shared, [null]);
230+
},
231+
);
232+
210233
test('development push gateway matches the compiled configuration', () {
211234
const expectedGateway = String.fromEnvironment(
212235
'BUZZ_PUSH_GATEWAY_URL',

0 commit comments

Comments
 (0)