Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

fix(auth): add security-scoped resource access for iOS local backup

Prateek Sunaldc0db6d8def3
diff --git a/mobile/apps/auth/lib/l10n/arb/app_en.arb b/mobile/apps/auth/lib/l10n/arb/app_en.arb
index 07332f16c2..6ddfd526d7 100644
--- a/mobile/apps/auth/lib/l10n/arb/app_en.arb
+++ b/mobile/apps/auth/lib/l10n/arb/app_en.arb
@@ -622,5 +622,6 @@
"chooseIcon": "Choose icon",
"customOrder": "Custom order",
"selectAnOption": "Select an option",
- "todaysLogs": "Today's logs"
+ "todaysLogs": "Today's logs",
+ "iCloudNotSupported": "iCloud Drive folder selection is not supported. Please select a local folder instead."
}
diff --git a/mobile/apps/auth/lib/services/local_backup_service.dart b/mobile/apps/auth/lib/services/local_backup_service.dart
index 33e6ed4e73..02f2fab6fe 100644
--- a/mobile/apps/auth/lib/services/local_backup_service.dart
+++ b/mobile/apps/auth/lib/services/local_backup_service.dart
@@ -10,6 +10,7 @@ import 'package:intl/intl.dart';
import 'package:logging/logging.dart';
import 'package:saf_stream/saf_stream.dart';
import 'package:saf_util/saf_util.dart';
+import 'package:security_scoped_resource/security_scoped_resource.dart';
import 'package:shared_preferences/shared_preferences.dart';
class LocalBackupService {
@@ -80,14 +81,30 @@ class LocalBackupService {
}
final dir = Directory(target.path!);
- await dir.create(recursive: true);
- final filePath = '${dir.path}/$fileName';
- final backupFile = File(filePath);
- await backupFile.create(recursive: true);
- await backupFile.writeAsString(content);
- await _manageOldBackups(dir.path);
- _logger.info('Automatic encrypted backup successful! Saved to: $filePath');
- return true;
+
+ // On iOS, we need to access security-scoped resources for directories
+ // outside the app sandbox (e.g., user-selected folders)
+ if (Platform.isIOS) {
+ await SecurityScopedResource.instance
+ .startAccessingSecurityScopedResource(dir);
+ }
+
+ try {
+ await dir.create(recursive: true);
+ final filePath = '${dir.path}/$fileName';
+ final backupFile = File(filePath);
+ await backupFile.create(recursive: true);
+ await backupFile.writeAsString(content);
+ await _manageOldBackups(dir.path);
+ _logger
+ .info('Automatic encrypted backup successful! Saved to: $filePath');
+ return true;
+ } finally {
+ if (Platform.isIOS) {
+ await SecurityScopedResource.instance
+ .stopAccessingSecurityScopedResource(dir);
+ }
+ }
} catch (e, s) {
_logger.severe('Failed to write backup', e, s);
return false;
@@ -170,31 +187,44 @@ class LocalBackupService {
Future<void> deleteAllBackupsIn(String path) async {
try {
final directory = Directory(path);
- if (!await directory.exists()) {
- _logger.warning('Old backup directory not found. Nothing to delete.');
- return;
+
+ // On iOS, we need to access security-scoped resources
+ if (Platform.isIOS) {
+ await SecurityScopedResource.instance
+ .startAccessingSecurityScopedResource(directory);
}
- final files = directory
- .listSync()
- .where(
- (entity) =>
- entity is File &&
- _isBackupFile(entity.path.split('/').last),
- )
- .map((entity) => entity as File)
- .toList();
+ try {
+ if (!await directory.exists()) {
+ _logger.warning('Old backup directory not found. Nothing to delete.');
+ return;
+ }
- if (files.isEmpty) {
- _logger.info('No old backup files found to delete.');
- return;
- }
+ final files = directory
+ .listSync()
+ .where(
+ (entity) =>
+ entity is File && _isBackupFile(entity.path.split('/').last),
+ )
+ .map((entity) => entity as File)
+ .toList();
+
+ if (files.isEmpty) {
+ _logger.info('No old backup files found to delete.');
+ return;
+ }
- for (final file in files) {
- await file.delete();
- _logger.info('Deleted: ${file.path}');
+ for (final file in files) {
+ await file.delete();
+ _logger.info('Deleted: ${file.path}');
+ }
+ _logger.info('Successfully cleaned up old backup location.');
+ } finally {
+ if (Platform.isIOS) {
+ await SecurityScopedResource.instance
+ .stopAccessingSecurityScopedResource(directory);
+ }
}
- _logger.info('Successfully cleaned up old backup location.');
} catch (e, s) {
_logger.severe('Error during full backup cleanup of old directory', e, s);
}
diff --git a/mobile/apps/auth/lib/ui/settings/data/local_backup/local_backup_experience.dart b/mobile/apps/auth/lib/ui/settings/data/local_backup/local_backup_experience.dart
index fdb0aeb0a0..ae8687ca4b 100644
--- a/mobile/apps/auth/lib/ui/settings/data/local_backup/local_backup_experience.dart
+++ b/mobile/apps/auth/lib/ui/settings/data/local_backup/local_backup_experience.dart
@@ -526,6 +526,11 @@ class _LocalBackupExperienceState extends State<LocalBackupExperience> {
if (result?.action == ButtonAction.second) {
final pickedPath = await FilePicker.platform.getDirectoryPath();
if (pickedPath != null) {
+ // Reject iCloud Drive paths on iOS
+ if (_isICloudPath(pickedPath)) {
+ _showSnackBar(context.l10n.iCloudNotSupported);
+ return false;
+ }
return _persistLocation(
pickedPath,
successMessage: context.l10n.initialBackupCreated,
@@ -657,6 +662,12 @@ class _LocalBackupExperienceState extends State<LocalBackupExperience> {
String? directoryPath = await FilePicker.platform.getDirectoryPath();
if (directoryPath != null) {
+ // On iOS, reject iCloud Drive paths as they don't work reliably
+ if (Platform.isIOS && _isICloudPath(directoryPath)) {
+ _showSnackBar(context.l10n.iCloudNotSupported);
+ return false;
+ }
+
final saved = await _persistLocation(
directoryPath,
successMessage:
@@ -678,6 +689,12 @@ class _LocalBackupExperienceState extends State<LocalBackupExperience> {
}
}
+ /// Check if the path is an iCloud Drive path
+ bool _isICloudPath(String path) {
+ return path.contains('/Mobile Documents/') ||
+ path.contains('com~apple~CloudDocs');
+ }
+
Future<bool> _persistLocation(
String path, {
String? successMessage,
diff --git a/mobile/apps/auth/pubspec.lock b/mobile/apps/auth/pubspec.lock
index 301a54eedc..789955c69b 100644
--- a/mobile/apps/auth/pubspec.lock
+++ b/mobile/apps/auth/pubspec.lock
@@ -1455,6 +1455,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "0.2.0"
+ security_scoped_resource:
+ dependency: "direct main"
+ description:
+ name: security_scoped_resource
+ sha256: "82279652f4932530f67cf848a55117bb2dcb5a542cb95a84b6ee78ecec9ee31f"
+ url: "https://pub.dev"
+ source: hosted
+ version: "0.0.2"
sentry:
dependency: "direct main"
description:
diff --git a/mobile/apps/auth/pubspec.yaml b/mobile/apps/auth/pubspec.yaml
index 72a4fa742d..d5ff7bd079 100644
--- a/mobile/apps/auth/pubspec.yaml
+++ b/mobile/apps/auth/pubspec.yaml
@@ -54,6 +54,7 @@ dependencies:
figma_squircle: ^0.6.3
file_picker: ^10.3.2
file_saver: ^0.3.1
+ security_scoped_resource: ^0.0.2
fixnum: ^1.1.0
fk_user_agent: # no package updates on pub.dev
git: