π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
[mobile] Block external navigation during Guest view
neeraj-pilot1e80f73b4d63
diff --git a/mobile/apps/photos/lib/app.dart b/mobile/apps/photos/lib/app.dartindex 8b45b2a435..b034105b1b 100644--- a/mobile/apps/photos/lib/app.dart+++ b/mobile/apps/photos/lib/app.dart@@ -176,22 +176,32 @@ class _EnteAppState extends State<EnteApp> with WidgetsBindingObserver {Future<void> _handleAndroidIntentAction(MediaExtentionAction mediaExtentionAction,) async {- AppLifecycleService.instance.setMediaExtensionAction(- _appLifecycleActionFor(mediaExtentionAction),- );+ if (AppNavigationService.instance.isGuestViewActive) return;++ void updateIntentAction() {+ AppLifecycleService.instance.setMediaExtensionAction(+ _appLifecycleActionFor(mediaExtentionAction),+ );+ }+if (mediaExtentionAction.action == IntentAction.pick) {await AppNavigationService.instance.pushPage(ExternalMediaPickerPage(requestedType: mediaExtentionAction.type,allowMultiple: mediaExtentionAction.allowMultiple,),+ onBeforePush: updateIntentAction,);return;}if (!_shouldOpenFileViewer(mediaExtentionAction)) {+ updateIntentAction();return;}- await AppNavigationService.instance.pushPage(const FileViewer());+ await AppNavigationService.instance.pushPage(+ const FileViewer(),+ onBeforePush: updateIntentAction,+ );}Widget _buildInitialAndroidHome() {diff --git a/mobile/apps/photos/lib/main.dart b/mobile/apps/photos/lib/main.dartindex 29d75ad0b3..bcab20cbf9 100644--- a/mobile/apps/photos/lib/main.dart+++ b/mobile/apps/photos/lib/main.dart@@ -30,10 +30,12 @@ import 'package:photos/core/configuration.dart';import 'package:photos/core/constants.dart';import 'package:photos/core/error-reporting/super_logging.dart';import 'package:photos/core/errors.dart';+import 'package:photos/core/event_bus.dart';import 'package:photos/core/network/network.dart';import 'package:photos/db/files_db.dart';import "package:photos/db/ml/db.dart";import 'package:photos/ente_theme_data.dart';+import 'package:photos/events/guest_view_event.dart';import "package:photos/locale.dart";import 'package:photos/module/upload/service/file_uploader.dart';import 'package:photos/module/upload/service/local_file_update_service.dart';@@ -177,7 +179,10 @@ Future<void> _runInForeground(enabled:await LockScreenSettings.instance.shouldShowLockScreen() ||localSettings.isOnGuestView(),- onUnlock: () => unawaited(localSettings.setOnGuestView(false)),+ onUnlock: () {+ unawaited(localSettings.setOnGuestView(false));+ Bus.instance.fire(GuestViewEvent(false, false));+ },locale: locale,supportedLocales: appSupportedLocales,localizationsDelegates: const [diff --git a/mobile/apps/photos/lib/services/app_navigation_service.dart b/mobile/apps/photos/lib/services/app_navigation_service.dartindex a2658a7336..6eb9db6262 100644--- a/mobile/apps/photos/lib/services/app_navigation_service.dart+++ b/mobile/apps/photos/lib/services/app_navigation_service.dart@@ -4,6 +4,7 @@ import 'dart:io';import 'package:ente_pure_utils/ente_pure_utils.dart';import 'package:flutter/material.dart';import 'package:logging/logging.dart';+import 'package:photos/service_locator.dart';// External entry points must use the inner Photos navigator. A BuildContext may// resolve to AppLock's outer navigator while the app is locked or resuming.@@ -23,11 +24,16 @@ class AppNavigationService {NavigatorState? get navigator => navigatorKey.currentState;+ bool get isGuestViewActive => localSettings.isOnGuestView();+Future<T?> pushPage<T extends Object>(Widget page, {bool forceCustomPageRoute = false,bool useFadeTransition = false,+ VoidCallback? onBeforePush,}) {+ if (isGuestViewActive) return Future<T?>.value();+final pushResult = Completer<T?>();final scheduledPush = _lastScheduledPush.catchError((Object _, StackTrace _) {})@@ -43,7 +49,16 @@ class AppNavigationService {return;}+ if (isGuestViewActive) {+ _logger.info("Skipping navigation because Guest View is active");+ if (!pushResult.isCompleted) {+ pushResult.complete(null);+ }+ return;+ }+try {+ onBeforePush?.call();final routeFuture = _pushWithNavigator<T>(navigator,page,diff --git a/mobile/apps/photos/lib/services/home_widget_service.dart b/mobile/apps/photos/lib/services/home_widget_service.dartindex 10ccc12cba..1e6e3bb50d 100644--- a/mobile/apps/photos/lib/services/home_widget_service.dart+++ b/mobile/apps/photos/lib/services/home_widget_service.dart@@ -236,6 +236,11 @@ class HomeWidgetService {}Future<void> onLaunchFromWidget(Uri? uri) async {+ if (AppNavigationService.instance.isGuestViewActive) {+ _logger.info("Ignoring widget launch because Guest View is active");+ return;+ }+if (uri == null) {_logger.warning("Widget launch failed: URI is null");return;@@ -270,7 +275,8 @@ class HomeWidgetService {if (!hasGrantedMLConsent) {final navigator = await AppNavigationService.instance.waitForNavigator();- if (navigator == null ||+ if (AppNavigationService.instance.isGuestViewActive ||+ navigator == null ||!navigator.mounted ||!await showMLConsentSheet(navigator.context)) {break;diff --git a/mobile/apps/photos/lib/ui/tabs/home_widget.dart b/mobile/apps/photos/lib/ui/tabs/home_widget.dartindex af5cd5c1e8..0b5c0fd147 100644--- a/mobile/apps/photos/lib/ui/tabs/home_widget.dart+++ b/mobile/apps/photos/lib/ui/tabs/home_widget.dart@@ -387,11 +387,15 @@ class _HomeWidgetState extends State<HomeWidget> {}final Map<Uri, (bool, int)> _linkedPublicAlbums = {};+ bool get _isGuestViewActive =>+ AppNavigationService.instance.isGuestViewActive;+Future<void> _handlePublicAlbumLink(Uri uri, String via) async {if (!mounted) {_logger.warning("Cannot handle public album link - widget not mounted");return;}+ if (_isGuestViewActive) return;try {_logger.info("Handling public album link: via $via");final int currentTime = DateTime.now().millisecondsSinceEpoch;@@ -410,7 +414,7 @@ class _HomeWidgetState extends State<HomeWidget> {final Collection? collection = await CollectionsService.instance.getCollectionFromPublicLink(context, uri);- if (!mounted) {+ if (!mounted || _isGuestViewActive) {return;}if (collection == null) {@@ -484,7 +488,7 @@ class _HomeWidgetState extends State<HomeWidget> {collection.pubMagicMetadata.asc ?? false,);await dialog.hide();- if (!mounted) return;+ if (!mounted || _isGuestViewActive) return;Navigator.of(context).pop();if (!mounted) return;@@ -518,7 +522,7 @@ class _HomeWidgetState extends State<HomeWidget> {);await dialog.hide();- if (!mounted) return;+ if (!mounted || _isGuestViewActive) return;await routeToPage(context,SharedPublicCollectionPage(@@ -528,7 +532,7 @@ class _HomeWidgetState extends State<HomeWidget> {),);if (sharedFiles.length == 1) {- if (!mounted) return;+ if (!mounted || _isGuestViewActive) return;await routeToPage(context,DetailPage(@@ -637,7 +641,7 @@ class _HomeWidgetState extends State<HomeWidget> {}Future<void> _handleSharedMediaStream(List<SharedMediaFile> value) async {- if (!mounted || value.isEmpty) {+ if (!mounted || _isGuestViewActive || value.isEmpty) {return;}@@ -648,11 +652,12 @@ class _HomeWidgetState extends State<HomeWidget> {}if (await _consumeAndroidMediaViewIntent()) {+ if (!mounted || _isGuestViewActive) return;_scheduleMediaViewFallbackNavigation();return;}- if (!mounted) {+ if (!mounted || _isGuestViewActive) {return;}if (value[0].mimeType != null &&@@ -684,7 +689,7 @@ class _HomeWidgetState extends State<HomeWidget> {);},).then((shouldOpenFile) {- if (!mounted || shouldOpenFile == null) {+ if (!mounted || _isGuestViewActive || shouldOpenFile == null) {return;}if (shouldOpenFile) {@@ -710,7 +715,7 @@ class _HomeWidgetState extends State<HomeWidget> {}Future<void> _handleInitialSharedMedia(List<SharedMediaFile> value) async {- if (!mounted) {+ if (!mounted || _isGuestViewActive) {return;}if (value.isNotEmpty && _isPublicAlbumUrl(value[0].path)) {@@ -720,7 +725,7 @@ class _HomeWidgetState extends State<HomeWidget> {}if (await _consumeAndroidMediaViewIntent()) {- if (!mounted) {+ if (!mounted || _isGuestViewActive) {return;}unawaited(@@ -736,7 +741,7 @@ class _HomeWidgetState extends State<HomeWidget> {return;}- if (value.isEmpty || !mounted) {+ if (value.isEmpty || !mounted || _isGuestViewActive) {return;}@@ -747,20 +752,22 @@ class _HomeWidgetState extends State<HomeWidget> {}Future<bool> _consumeAndroidMediaViewIntent() async {- if (!Platform.isAndroid) {+ if (!Platform.isAndroid || _isGuestViewActive) {return false;}final mediaExtensionAction = await initIntentAction();- AppLifecycleService.instance.setMediaExtensionAction(mediaExtensionAction);+ if (!mounted || _isGuestViewActive) return false;- if (!_isMediaViewAction(mediaExtensionAction)) {- return false;+ final isMediaView = _isMediaViewAction(mediaExtensionAction);+ if (isMediaView) {+ _logger.info("Consuming shared media callback for Android media view");+ await ReceiveSharingIntent.instance.reset();+ if (!mounted || _isGuestViewActive) return false;}- _logger.info("Consuming shared media callback for Android media view");- await ReceiveSharingIntent.instance.reset();- return true;+ AppLifecycleService.instance.setMediaExtensionAction(mediaExtensionAction);+ return isMediaView;}bool _isMediaViewAction(MediaExtentionAction action) {@@ -775,7 +782,9 @@ class _HomeWidgetState extends State<HomeWidget> {_mediaViewFallbackNavigationScheduled = true;Future<void>.delayed(const Duration(milliseconds: 200), () {_mediaViewFallbackNavigationScheduled = false;- if (!mounted || ModalRoute.of(context)?.isCurrent != true) {+ if (!mounted ||+ _isGuestViewActive ||+ ModalRoute.of(context)?.isCurrent != true) {return;}unawaited(@@ -1294,7 +1303,7 @@ class _HomeWidgetState extends State<HomeWidget> {void _onDidReceiveNotificationResponse(NotificationResponse notificationResponse,) async {- if (!mounted) return;+ if (!mounted || _isGuestViewActive) return;final String? payload = notificationResponse.payload;if (payload != null) {debugPrint('notification payload: $payload');diff --git a/mobile/apps/photos/lib/ui/viewer/actions/file_selection_actions_widget.dart b/mobile/apps/photos/lib/ui/viewer/actions/file_selection_actions_widget.dartindex 58ba7e6f81..81053cb5f5 100644--- a/mobile/apps/photos/lib/ui/viewer/actions/file_selection_actions_widget.dart+++ b/mobile/apps/photos/lib/ui/viewer/actions/file_selection_actions_widget.dart@@ -847,10 +847,15 @@ class _FileSelectionActionsWidgetState),);await localSettings.setOnGuestView(true);- if (!mounted) return;+ if (!mounted) {+ await localSettings.setOnGuestView(false);+ return;+ }routeToPage(context, page, forceCustomPageRoute: true).ignore();WidgetsBinding.instance.addPostFrameCallback((_) {- Bus.instance.fire(GuestViewEvent(true, false));+ if (localSettings.isOnGuestView()) {+ Bus.instance.fire(GuestViewEvent(true, false));+ }});} else {await showErrorDialog(diff --git a/mobile/apps/photos/lib/ui/viewer/file/file_app_bar.dart b/mobile/apps/photos/lib/ui/viewer/file/file_app_bar.dartindex b3f5a3bb04..8404801c05 100644--- a/mobile/apps/photos/lib/ui/viewer/file/file_app_bar.dart+++ b/mobile/apps/photos/lib/ui/viewer/file/file_app_bar.dart@@ -703,6 +703,7 @@ class FileAppBarState extends State<FileAppBar> {Future<void> _onTapGuestView() async {if (await LocalAuthentication().isDeviceSupported()) {+ if (!mounted) return;Bus.instance.fire(GuestViewEvent(true, true));await localSettings.setOnGuestView(true);} else {diff --git a/mobile/apps/photos/lib/ui/viewer/gallery/gallery_app_bar_widget.dart b/mobile/apps/photos/lib/ui/viewer/gallery/gallery_app_bar_widget.dartindex 2e9de20558..579e8d4cfa 100644--- a/mobile/apps/photos/lib/ui/viewer/gallery/gallery_app_bar_widget.dart+++ b/mobile/apps/photos/lib/ui/viewer/gallery/gallery_app_bar_widget.dart@@ -1536,10 +1536,15 @@ class _GalleryAppBarWidgetState extends State<GalleryAppBarWidget> {),);await localSettings.setOnGuestView(true);- if (!mounted) return;+ if (!mounted) {+ await localSettings.setOnGuestView(false);+ return;+ }routeToPage(context, page, forceCustomPageRoute: true).ignore();WidgetsBinding.instance.addPostFrameCallback((_) {- Bus.instance.fire(GuestViewEvent(true, false));+ if (localSettings.isOnGuestView()) {+ Bus.instance.fire(GuestViewEvent(true, false));+ }});} else {if (!mounted) return;diff --git a/mobile/apps/photos/test/services/app_navigation_service_test.dart b/mobile/apps/photos/test/services/app_navigation_service_test.dartnew file mode 100644index 0000000000..6a1a1a475d--- /dev/null+++ b/mobile/apps/photos/test/services/app_navigation_service_test.dart@@ -0,0 +1,85 @@+import 'package:flutter/material.dart';+import 'package:flutter_test/flutter_test.dart';+import 'package:photos/service_locator.dart';+import 'package:photos/services/app_navigation_service.dart';+import 'package:photos/settings/local_settings.dart';+import 'package:shared_preferences/shared_preferences.dart';++void main() {+ TestWidgetsFlutterBinding.ensureInitialized();++ setUpAll(() async {+ SharedPreferences.setMockInitialValues({});+ ServiceLocator.instance.localSettings = LocalSettings(+ await SharedPreferences.getInstance(),+ );+ });++ testWidgets('guards external navigation across Guest view transitions', (+ tester,+ ) async {+ final navigation = AppNavigationService.instance;+ var intentUpdates = 0;+ Widget app() => MaterialApp(+ navigatorKey: navigation.navigatorKey,+ home: const Scaffold(body: Text('Selected Guest photos')),+ );++ await tester.pumpWidget(app());+ await localSettings.setOnGuestView(true);++ final activeResult = navigation.pushPage<Object>(+ const Scaffold(body: Text('External content')),+ onBeforePush: () => intentUpdates++,+ );+ await tester.pumpAndSettle();++ expect(find.text('External content'), findsNothing);+ expect(find.text('Selected Guest photos'), findsOneWidget);+ expect(await activeResult, isNull);+ await tester.pumpWidget(const SizedBox.shrink());+ await localSettings.setOnGuestView(false);+ final waitingResult = navigation.pushPage<Object>(+ const Scaffold(body: Text('External content')),+ onBeforePush: () => intentUpdates++,+ );+ await tester.pump();+ await localSettings.setOnGuestView(true);+ await tester.pumpWidget(app());+ await tester.pumpAndSettle();++ expect(find.text('External content'), findsNothing);+ expect(await waitingResult, isNull);+ await tester.pumpWidget(const SizedBox.shrink());+ await localSettings.setOnGuestView(true);+ final ignoredResult = navigation.pushPage<Object>(+ const Scaffold(body: Text('External content')),+ onBeforePush: () => intentUpdates++,+ );+ await localSettings.setOnGuestView(false);+ await tester.pumpWidget(app());+ await tester.pumpAndSettle();++ expect(find.text('External content'), findsNothing);+ expect(await ignoredResult, isNull);+ expect(intentUpdates, 0);+ await tester.pumpWidget(const SizedBox.shrink());+ await tester.pumpWidget(app());+ await localSettings.setOnGuestView(true);+ await localSettings.setOnGuestView(false);++ final result = navigation.pushPage<String>(+ const Scaffold(body: Text('External content')),+ onBeforePush: () => intentUpdates++,+ forceCustomPageRoute: true,+ );+ await tester.pumpAndSettle();+ expect(find.text('External content'), findsOneWidget);++ navigation.navigator!.pop('done');+ await tester.pumpAndSettle();+ expect(await result, 'done');+ expect(intentUpdates, 1);+ expect(find.text('Selected Guest photos'), findsOneWidget);+ });+}