Dev.to Security 🔐 Cybersecurity 👁 2 📖 7 min read

What the Mac App Sandbox changed for an app that reads ~/Library

For a while I shipped a Mac recovery app outside the App Store. It read the Apple Notes database, Time Machine backups and iPhone backups directly, and it asked the user for Full Disk Access to do it. Then I built a Mac

For a while I shipped a Mac recovery app outside the App Store. It read the Apple Notes database, Time Machine backups and iPhone backups directly, and it asked the user for Full Disk Access to do it. Then I built a Mac App Store version, which has to run in the App Sandbox, and most of my assumptions about how a recovery tool reads ~/Library stopped being true.

This is what changed, what I tested, and the pattern I ended up with. The findings come from a sandboxed test build that I ran in a macOS 26.6 virtual machine with Full Disk Access switched off, before the store release. The Swift snippets compile against the current SDK.

What a sandboxed app can see by default

Almost nothing outside its own container. My test build had only three entitlements:

<key>com.apple.security.app-sandbox</key>
<true/>
<key>com.apple.security.files.user-selected.read-write</key>
<true/>
<key>com.apple.security.files.bookmarks.app-scope</key>
<true/>

Before the user chose anything, this is what the app got:

  • ~/Library/Group Containers (where Notes keeps group.com.apple.notes): permission denied.
  • ~/Library/Application Support/MobileSync/Backup (Finder's iPhone backups): not readable.
  • tmutil listbackups: refused. tmutil destinationinfo: saw nothing.
  • /Volumes: the names of mounted disks could be listed, but not their contents.

The first surprise was NSHomeDirectory(). Inside the sandbox it returns the app's container, ~/Library/Containers/<bundle id>/Data, not the user's home. Every path my old code built from "home" pointed into an empty container. To get the real home folder, ask the user database:

enum RealHome {
    static var url: URL {
        if let pw = getpwuid(getuid()), let dir = pw.pointee.pw_dir {
            return URL(fileURLWithPath: String(cString: dir), isDirectory: true)
        }
        return FileManager.default.homeDirectoryForCurrentUser
    }
}

You need the real path to point an open panel at the right place, not to read from it. Knowing the path grants nothing.

A quick way to confirm your build really is sandboxed while testing: the environment has APP_SANDBOX_CONTAINER_ID set.

Full Disk Access does not help

My first idea was to keep the old onboarding: ask for Full Disk Access, then read everything as before. It doesn't work that way. Full Disk Access is a privacy (TCC) permission. The sandbox is a separate layer. Granting a sandboxed app Full Disk Access does not let it open paths the sandbox has not been given. So in the store build I removed every mention of Full Disk Access and every "Open System Settings" button. Telling users to grant a permission that changes nothing is worse than not asking.

The user's choice is the permission

What does work is files.user-selected. When the user picks a file or folder in an NSOpenPanel (or drags it onto the app), the sandbox extends to that item and everything inside it. This is the part that surprised me in a good way: it works for protected folders too.

When the user chose ~/Library/Group Containers/group.com.apple.notes in the open panel, the app could read the live Notes database. No Full Disk Access, and no extra privacy prompt. The same held for a Time Machine disk (the app read a real backup made with tmutil once the user picked the backup disk), external APFS and exFAT drives, and copies of iPhone backups.

So the design question changes from "how do I get access to everything" to "how do I make one or two choices easy". The answer is to open the panel already in the right folder, with a message that says what will happen:

@MainActor
func chooseNotesFolder() -> URL? {
    let panel = NSOpenPanel()
    panel.message = "Click Open to let the app read your Notes library (read only)."
    panel.prompt = "Open"
    panel.canChooseDirectories = true
    panel.canChooseFiles = false
    panel.allowsMultipleSelection = false
    panel.directoryURL = RealHome.url.appendingPathComponent(
        "Library/Group Containers/group.com.apple.notes", isDirectory: true)
    return panel.runModal() == .OK ? panel.url : nil
}

For the user it is one click on Open. I do the same for MobileSync/Backup and offer a "Choose your Time Machine disk" button for backups.

Two details worth knowing:

  • Choosing a single file grants that file only. In a related app I tested writing a new file next to a file the user had chosen; the sandbox refused, even with the exact output name. To write somewhere, ask for the destination with an NSSavePanel, or have the user choose the folder.
  • If you want to read the Notes folder but your app only ever writes exports elsewhere, consider files.user-selected.read-only for a smaller footprint. I needed read-write for export destinations, so I kept one entitlement.

Remembering the choice: security-scoped bookmarks

Access from the open panel lasts until the app quits. To keep it, store a security-scoped bookmark, which is what files.bookmarks.app-scope is for:

func makeBookmark(for url: URL) throws -> Data {
    try url.bookmarkData(options: [.withSecurityScope],
                         includingResourceValuesForKeys: nil, relativeTo: nil)
}

Save the Data in your container (UserDefaults is fine for a handful). On the next launch, resolve it, and refresh it if the system marks it stale:

struct ResolvedPlace { let url: URL; let refreshed: Data? }

func resolve(_ bookmark: Data) throws -> ResolvedPlace {
    var stale = false
    let url = try URL(resolvingBookmarkData: bookmark, options: [.withSecurityScope],
                      relativeTo: nil, bookmarkDataIsStale: &stale)
    var refreshed: Data? = nil
    if stale, url.startAccessingSecurityScopedResource() {
        defer { url.stopAccessingSecurityScopedResource() }
        refreshed = try? makeBookmark(for: url)
    }
    return ResolvedPlace(url: url, refreshed: refreshed)
}

Then wrap every use in start and stop calls:

func withAccess<T>(to url: URL, _ body: () throws -> T) rethrows -> T? {
    guard url.startAccessingSecurityScopedResource() else { return nil }
    defer { url.stopAccessingSecurityScopedResource() }
    return try body()
}

In my test, all seven saved places (the live Notes folder, two external drives, backup copies and an export destination) resolved after a relaunch with stale == false, and also after the binary was re-signed. When an external drive is not connected, resolving fails or the URL is unreachable; I show "Connect the drive named X" instead of treating it as an error.

Apple's documentation asks you to balance every startAccessing call with a stopAccessing; unbalanced calls leak kernel resources until the app can no longer get new access. For a recovery app that is easy: start access when a scan or export begins, stop when it ends.

Automatic discovery is gone, and that is fine

The old app scanned on launch: it found every Time Machine destination, every local snapshot, every iPhone backup, and showed what it found. None of that works in the sandbox. Instead the home screen offers three buttons: choose a Time Machine disk, choose a folder or disk with a copy or a backup, and compare with today's notes. Once chosen, they are remembered, and the next launch looks like the old one.

Inside a chosen folder or disk I still search a few levels deep for known layouts (a Notes container, a MobileSync/Backup folder, Time Machine folder structures), so the user does not have to find the exact subfolder.

Things I had to replace, not port

"Restore into Notes" used AppleScript in the direct version. In the sandbox, sending Apple Events to Notes worked only with a temporary-exception.apple-events entitlement. Temporary exceptions need a justification and App Review often rejects them, and Notes offers no scripting targets you could request instead. So I replaced it: the store version writes an Evernote-format .enex file and hands it to Notes, which asks once whether to import and then adds the notes, with their original dates and attachments, into an "Imported Notes" folder. No extra entitlement, and in my tests the dates were better preserved than with the AppleScript route, which set them to the restore time.

Local APFS snapshots are another one: the app cannot mount them. The user can open them through the Time Machine browser, after which the app can read them through a choice like any other folder.

Summary

  • In the sandbox, NSHomeDirectory() is your container. Use getpwuid for the real path, only to point panels at it.
  • Full Disk Access does not lift the sandbox. Don't ask for it.
  • A user choice in NSOpenPanel grants access, even to the Notes container, without a privacy prompt.
  • Keep choices with security-scoped bookmarks; refresh stale ones; balance start and stop.
  • Replace Apple Events automation with a file hand-off where you can.

Disclosure: I make NoteRescue, the app this was about. It finds notes that are missing or changed by comparing Time Machine and iPhone backups with today's Notes, and in the Mac App Store edition it works with exactly the choose-and-remember pattern above, no Full Disk Access needed: NoteRescue on the Mac App Store, or direct from my site.

Written with help from an AI assistant; code and commands were tested on macOS.

📰 Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.