Skip to content

Commit

Permalink
fix: ensure event data is sandboxed per app for all platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
justin-fiedler committed Feb 1, 2024
1 parent 2aab265 commit 5aef5ca
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions Sources/Amplitude/Storages/PersistentStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -238,23 +238,36 @@ extension PersistentStorage {
return result
}

private func isSandboxEnabled() -> Bool {
let environment = ProcessInfo.processInfo.environment
return environment["APP_SANDBOX_CONTAINER_ID"] != nil
}

internal func getEventsStorageDirectory(createDirectory: Bool = true) -> URL {
// tvOS doesn't have access to document
// macOS /Documents dir might be synced with iCloud
#if os(tvOS) || os(macOS)
let searchPathDirectory = FileManager.SearchPathDirectory.cachesDirectory
let searchPathDirectory = FileManager.SearchPathDirectory.applicationSupportDirectory

// Make sure Amplitude data is sandboxed per app
#if os(iOS)
// iOS is sandboxed by default
let appPath = ""
#else
let searchPathDirectory = FileManager.SearchPathDirectory.documentDirectory
// macOS/tvOS are not sandboxed automatically
let appPath = isSandboxEnabled() ? "" : "\(Bundle.main.bundleIdentifier!)/"
#endif

let urls = fileManager.urls(for: searchPathDirectory, in: .userDomainMask)
let docUrl = urls[0]
let storageUrl = docUrl.appendingPathComponent("amplitude/\(eventsFileKey)/")
var storageUrl = docUrl.appendingPathComponent("amplitude/\(appPath)\(eventsFileKey)/")
if createDirectory {
// try to create it, will fail if already exists.
// tvOS, watchOS regularly clear out data.
try? FileManager.default.createDirectory(at: storageUrl, withIntermediateDirectories: true, attributes: nil)
var values = URLResourceValues()
values.isExcludedFromBackup = true
try? FileManager.default.createDirectory(
at: storageUrl, withIntermediateDirectories: true, attributes: nil)
try? storageUrl.setResourceValues(values)
}

return storageUrl
}

Expand Down

0 comments on commit 5aef5ca

Please sign in to comment.