Skip to main content
coreydaley.dev
Docs menu

Temp folders, helper tools and extensions in the App Sandbox

Your container is yours: full read and write access, and you can run programs there, per Apple. The catch is that it’s somewhere the person will never look, so nothing they asked for should end up there. Commands you launch work within limits, and each extension has a sandbox of its own.

The Purrchiver details below come from its code, tests and commit history. I didn’t re-run them for this guide.

Temp and the container

macOS creates the container in ~/Library/Containers the first time a sandboxed app launches. Use FileManager.default.temporaryDirectory rather than a path you’ve assumed. Purrchiver’s sandboxed test host treats that as inside the container; I didn’t print the path from a sandboxed process myself.

Apple’s rules for that folder: it may be purged while your app isn’t running, with no promise of when, it isn’t backed up, and you should delete files as soon as you’re done.

The pattern that works for output:

  1. Do the work in a private folder in temp.
  2. Copy the result to the URL the Save panel gave you.

This one came from a test. A Save panel grants the file it names, not its folder, so a decompress can’t do its work beside that name. A UI test caught it, and the decompress now runs in a private folder and is copied over.

Temp can hold private data. An archive app stages decrypted files there, and a crash or force-quit leaves them behind. Purrchiver now makes those folders owner-only, and sweeps its known leftovers at launch, touching only what’s older than that launch. A test checks that every temp name in the source is on the sweep list.

External commands

You can launch some system tools, and they’re held to limits too. Purrchiver runs /usr/bin/hdiutil with Process, in a shipping App Store app:

CommandFrom inside the sandbox
hdiutil convert, makehybrid, isencrypted, chpassUsed by Purrchiver; a smoke test covers the create and verify path in a sandboxed host
hdiutil attachFails with “Device not configured”, so no mounting
hdiutil create -srcfolderBlocked the same way

So check the command you need under the sandbox, not in Terminal, where it isn’t sandboxed. The way around it may be a different pipeline: Reading a DMG in the App Sandbox is how Purrchiver gets by without mounting.

Programs in other places are harder. Apple says the user-selected entitlements don’t let you run programs outside your bundle, container or app group containers. It names one more entitlement, com.apple.security.files.user-selected.executable, for writing programs into places the person picked. Purrchiver has neither need, so I haven’t tried that.

Two other rules can bite:

  • Don’t pass secrets as arguments. Any process of the same user can read a command’s arguments. Purrchiver’s password used to go to hdiutil that way, and the post above covers the fix.
  • Don’t leave processes running. App Store guideline 2.4.5 doesn’t allow spawned processes that keep running after the app quits without consent.

Consider not launching anything, too. Purrchiver’s age encryption is a static library inside the app, so there’s no tool to launch.

Your own helper tool

Purrchiver doesn’t embed one, and I haven’t followed this myself. These are the steps that matter most from Apple’s Embedding a command-line tool in a sandboxed app; follow that page for all of them, including the signing settings:

  • Copy the tool into Contents/MacOS, with a Copy Files phase whose destination is Executables.
  • Give it exactly two entitlements, com.apple.security.app-sandbox and com.apple.security.inherit, both true.
  • Turn off Code Signing Inject Base Entitlements, because com.apple.security.get-task-allow can’t be combined with inherit.
  • Turn on Skip Install for the tool’s target. If Xcode offers Distribute Content rather than Distribute App, that’s the setting to check.

If the tool crashes straight away with a code signing error, check its entitlements first. That’s the failure Apple describes. Without get-task-allow, you also can’t debug the tool.

Shortcuts actions

Purrchiver’s App Intents run in the app, under its sandbox and entitlements. That shaped them in three ways:

  • Hand back data, not a path into your container. App Review rejected Purrchiver 1.0 under guideline 2.4.5(i). My notes on it point to six actions that returned a file in sandbox temp, which stayed in the container. They now read the output into memory, delete the working copy, and let a Save File step put it wherever the person wants. A test runs each action’s real perform() and checks nothing is left in temp.
  • Set a limit, since it’s all in memory. Purrchiver refuses results over 2 GB and tells the person to use the app for that one.
  • Don’t write beside the input. An action that hands back data isn’t bound by the related-item name, so Generate Checksums can use the conventional photos.zip.sha256, and the Save File step decides where it goes.

While fixing that, I also removed a Settings row that showed the container’s path with a Reveal in Finder button.

Quick Look extensions

Purrchiver’s preview extension runs in its own sandbox. It has one entitlement, com.apple.security.app-sandbox. The host grants it read access to the one file being previewed, for that request, so it never needs user-selected access, and an earlier read-only entitlement was removed as unused.

From Purrchiver’s code and commits:

  • It only gets that one file. Treat the preview as reading what you’re handed, and not writing anywhere beyond its own temp.
  • Quick Look can kill it mid-preview. Temp files were left in a container nothing else can reach. The first preview in each extension process now sweeps them, but only those older than ten minutes, since more than one extension process can run at once.
  • Stay fast, with a limit of your own. Purrchiver stops at a 5-second deadline and 1,000 rows, and shows what it has. Those are my numbers, not Apple’s. The first version waited for the read to finish anyway; the read now has to stop when it’s cancelled.