Repository navigation
Stop exposing app data to the iOS Files app #96531
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
9564ca3
Stop exposing app data to the iOS Files app
NicolasBonet c30cf1c
Fix Jest mocks for new file storage locations
NicolasBonet bdbf8f3
Move nitro-sqlite patch to subdirectory and document it
NicolasBonet 065abaf
Link upstream and tracking issues in nitro-sqlite patch details
NicolasBonet 9e66396
Bump Mobile-Expensify to restore documentsDirectory declaration
NicolasBonet 68ac36d
Preserve queued receipts and rewrite their persisted paths in the iOS…
NicolasBonet bb1ce6b
Rewrite odometer image paths in the Documents migration
NicolasBonet aa78425
Keep Documents visible in the Files app for user downloads
NicolasBonet 9961ef4
Keep queued receipts in Documents so users can see them
NicolasBonet b69a861
Merge branch 'main' into nicolas-ios-files-app-data-exposure
NicolasBonet f0135ef
Update Mobile-Expensify submodule to 3c66ebd
NicolasBonet 8247c01
Update Mobile-Expensify submodule to 5b97cab
NicolasBonet 850a538
Merge remote-tracking branch 'origin/main' into nicolas-ios-files-app…
NicolasBonet 1f490f5
Merge branch 'main' into nicolas-ios-files-app-data-exposure
NicolasBonet 12edd32
Merge remote-tracking branch 'origin/main' into nicolas-ios-files-app…
NicolasBonet 4e8c768
Merge remote-tracking branch 'origin/main' into nicolas-ios-files-app…
NicolasBonet 951f42f
Merge remote-tracking branch 'origin/main' into nicolas-ios-files-app…
NicolasBonet ce966b3
Address review findings on file migration safety
NicolasBonet ac763e6
Fix CI: restore OSBotify-managed submodule pointer and spellcheck words
NicolasBonet 7c588bc
Split semicolon-joined comments into separate sentences
NicolasBonet 6b78934
Fail migration on attachment record rewrite errors and delete source …
NicolasBonet a301333
Delete the Onyx state dump once sharing completes
NicolasBonet 16d8f91
Skip attachment record path scan once it has completed
NicolasBonet ac542bc
Fix lint directive placement and export bucket for migration flag
NicolasBonet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
...e-nitro-sqlite/react-native-nitro-sqlite+9.6.0+002+store-database-outside-documents.patch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| diff --git a/node_modules/react-native-nitro-sqlite/ios/OnLoad.mm b/node_modules/react-native-nitro-sqlite/ios/OnLoad.mm | ||
| index 6ce7258..65c29cb 100644 | ||
| --- a/node_modules/react-native-nitro-sqlite/ios/OnLoad.mm | ||
| +++ b/node_modules/react-native-nitro-sqlite/ios/OnLoad.mm | ||
| @@ -10,6 +10,63 @@ | ||
| using namespace margelo::nitro; | ||
| using namespace margelo::nitro::rnnitrosqlite; | ||
|
|
||
| +// The Documents directory can be exposed to the user (Files app) when file sharing | ||
| +// is enabled, so databases are stored in Library/Application Support instead. | ||
| +// Databases created by older app versions are moved out of Documents on first launch. | ||
| +// | ||
| +// The "OnyxDB" name must match the database the app opens: DEFAULT_DB_NAME in | ||
| +// src/CONST/index.ts and DB_NAME in react-native-onyx's SQLiteProvider. A Jest test | ||
| +// (MoveFilesOutOfDocumentsTest) fails if the names drift apart. | ||
| +static void migrateDatabaseFiles(NSString *fromDirectory, NSString *toDirectory) { | ||
| + NSFileManager *fileManager = [NSFileManager defaultManager]; | ||
| + NSArray<NSString *> *databaseFiles = @[ @"OnyxDB", @"OnyxDB-wal", @"OnyxDB-shm" ]; | ||
| + | ||
| + if (![fileManager fileExistsAtPath:[fromDirectory stringByAppendingPathComponent:@"OnyxDB"]]) { | ||
| + // Nothing to migrate. A previous run may have been interrupted after copying the set but | ||
| + // before removing the journal files, so sweep any leftovers out of Documents. | ||
| + [fileManager removeItemAtPath:[fromDirectory stringByAppendingPathComponent:@"OnyxDB-wal"] error:nil]; | ||
| + [fileManager removeItemAtPath:[fromDirectory stringByAppendingPathComponent:@"OnyxDB-shm"] error:nil]; | ||
| + return; | ||
| + } | ||
| + | ||
| + // A database in Documents means an older build was writing there, so it is the live copy. | ||
| + // Remove whatever sits at the destination (e.g. after a downgrade and re-upgrade) so a -wal | ||
| + // from one database generation is never replayed into a database from another. | ||
| + for (NSString *file in databaseFiles) { | ||
| + [fileManager removeItemAtPath:[toDirectory stringByAppendingPathComponent:file] error:nil]; | ||
| + } | ||
| + | ||
| + // Copy the whole set before deleting anything: committed-but-uncheckpointed writes live in | ||
| + // the -wal file and SQLite only replays it when it sits next to its database, so the files | ||
| + // must never be separated. If any copy fails, the intact originals in Documents keep being | ||
| + // used (see the fallback in +load) and the migration retries on the next launch. | ||
| + for (NSString *file in databaseFiles) { | ||
| + NSString *sourcePath = [fromDirectory stringByAppendingPathComponent:file]; | ||
| + if (![fileManager fileExistsAtPath:sourcePath]) { | ||
| + continue; | ||
| + } | ||
| + | ||
| + NSError *error = nil; | ||
| + if (![fileManager copyItemAtPath:sourcePath toPath:[toDirectory stringByAppendingPathComponent:file] error:&error]) { | ||
| + NSLog(@"Failed to migrate database file %@: %@", file, error.localizedDescription); | ||
| + return; | ||
| + } | ||
| + } | ||
| + | ||
| + // The database file is deleted first, and the journals only once that succeeds: if the | ||
| + // database cannot be removed, the existence check in +load keeps using the source database | ||
| + // on this launch, so its -wal must stay next to it or committed writes would be lost. | ||
| + // An interruption after the first delete can only leave journal files behind, which the | ||
| + // sweep above removes on the next launch. | ||
| + NSError *removeError = nil; | ||
| + if (![fileManager removeItemAtPath:[fromDirectory stringByAppendingPathComponent:@"OnyxDB"] error:&removeError]) { | ||
| + NSLog(@"Failed to remove migrated database from its old location: %@", removeError.localizedDescription); | ||
| + return; | ||
| + } | ||
| + [fileManager removeItemAtPath:[fromDirectory stringByAppendingPathComponent:@"OnyxDB-wal"] error:nil]; | ||
| + [fileManager removeItemAtPath:[fromDirectory stringByAppendingPathComponent:@"OnyxDB-shm"] error:nil]; | ||
| +} | ||
| + | ||
| + (void)load { | ||
| // Get appGroupID value from Info.plist using key "AppGroup" | ||
| NSString *appGroupID = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"RNNitroSQLite_AppGroup"]; | ||
| @@ -30,9 +80,24 @@ | ||
|
|
||
| documentPath = [storeUrl path]; | ||
| } else { | ||
| - // Get iOS app's document directory (to safely store database .sqlite3 file) | ||
| - NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, true); | ||
| + // Store databases in Library/Application Support, which is persistent, backed up, | ||
| + // and never exposed to the user via the Files app (unlike the Documents directory) | ||
| + NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, true); | ||
| documentPath = [paths objectAtIndex:0]; | ||
| + | ||
| + NSFileManager *fileManager = [NSFileManager defaultManager]; | ||
| + if (![fileManager fileExistsAtPath:documentPath]) { | ||
| + [fileManager createDirectoryAtPath:documentPath withIntermediateDirectories:YES attributes:nil error:nil]; | ||
| + } | ||
| + | ||
| + NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, true) objectAtIndex:0]; | ||
| + migrateDatabaseFiles(documentsDirectory, documentPath); | ||
| + | ||
| + // If the database could not be moved out of Documents, keep opening it there rather than | ||
| + // creating a fresh empty one; the migration retries on the next launch. | ||
| + if ([fileManager fileExistsAtPath:[documentsDirectory stringByAppendingPathComponent:@"OnyxDB"]]) { | ||
| + documentPath = documentsDirectory; | ||
| + } | ||
| } | ||
|
|
||
| HybridNitroSQLite::docPath = [documentPath UTF8String]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,7 +10,9 @@ import Onyx from 'react-native-onyx'; | |
|
|
||
| import type {CacheAttachmentProps, GetCachedAttachmentProps, RemoveCachedAttachmentProps} from './types'; | ||
|
|
||
| const ATTACHMENT_DIR = `${RNFS.DocumentDirectoryPath}/attachments`; | ||
| // Cached attachments are re-downloadable, so they live in Caches, which the OS may purge | ||
| // and which is never exposed to the user via the iOS Files app (unlike Documents) | ||
| const ATTACHMENT_DIR = `${RNFS.CachesDirectoryPath}/attachments`; | ||
|
|
||
| async function cacheAttachment({attachmentID, uri, mimeType}: CacheAttachmentProps) { | ||
| const isLocalFile = uri.startsWith('file://'); | ||
|
|
@@ -22,6 +24,8 @@ async function cacheAttachment({attachmentID, uri, mimeType}: CacheAttachmentPro | |
| const destPath = `${ATTACHMENT_DIR}/${fileName}`; | ||
|
|
||
| try { | ||
| // The OS can purge Caches wholesale, so the directory may need recreating | ||
| await RNFS.mkdir(ATTACHMENT_DIR); | ||
| await RNFS.copyFile(uri, destPath); | ||
| await Onyx.set(`${ONYXKEYS.COLLECTION.ATTACHMENT}${attachmentID}`, { | ||
| attachmentID, | ||
|
|
@@ -56,6 +60,8 @@ async function cacheAttachment({attachmentID, uri, mimeType}: CacheAttachmentPro | |
|
|
||
| const fileName = `${attachmentID}.${attachmentFileExtension}`; | ||
| const filePath = `${ATTACHMENT_DIR}/${fileName}`; | ||
| // The OS can purge Caches wholesale, so the directory may need recreating | ||
| await RNFS.mkdir(ATTACHMENT_DIR); | ||
| await RNFetchBlob.config({path: filePath}).fetch('GET', uri); | ||
|
|
||
| await Onyx.set(`${ONYXKEYS.COLLECTION.ATTACHMENT}${attachmentID}`, { | ||
|
|
@@ -78,7 +84,13 @@ async function getCachedAttachment({attachmentID, attachment, currentSource}: Ge | |
|
|
||
| const localSource = attachment?.source; | ||
| if (localSource) { | ||
| return localSource; | ||
| // The OS can purge Caches while the Onyx record survives, so verify the file still | ||
| // exists. If it was purged, fall back to the current source and re-cache it. | ||
| const localFileExists = await RNFS.exists(localSource); | ||
| if (localFileExists) { | ||
| return localSource; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| } | ||
| cacheAttachment({attachmentID, uri: currentSource}); | ||
| } | ||
|
|
||
| return currentSource; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.