Skip to main content
coreydaley.dev

Checksum files in the App Sandbox: why Photos.sha256, not Photos.zip.sha256

New in Purrchiver 1.1. If you don't see it, update Purrchiver from the App Store.

Purrchiver 1.1 can write a checksum file when you create an archive: a list of every file inside with its SHA-256, so whoever you send it to can confirm nothing changed on the way. The format is the standard one shasum writes and reads. The name isn’t. The convention is to add .sha256 to the archive’s full name, Photos.zip.sha256. Purrchiver writes Photos.sha256.

That wasn’t a style choice. It’s the only name I found the App Sandbox would let it create.

The sandbox gives you the file, not the folder

A sandboxed Mac app can only touch files you’ve given it. When you pick a file in an Open or Save panel, the app gets access to that one file, not to the folder it’s in. That’s the point: an app you asked to open Photos.zip shouldn’t be able to read everything else in Downloads.

But a checksum file has to go beside the archive, in a folder Purrchiver was never given. For that, macOS has related items: files an app may touch because they belong with a file you did pick. Apple’s example is a movie and its subtitle file. To use them, an app needs three things:

  1. Declare the type as related in its Info.plist: a document type for .sha256 with NSIsRelatedItemType set to true, and the role Editor.
  2. Ask through file coordination, with a file presenter whose primary item is the file you picked and whose presented item is the sidecar, registered for just that access. The write goes through NSFileCoordinator, and macOS decides whether to allow it.
  3. Hold the security-scoped access you already have to the primary file while it does.

The Info.plist entry looks like this:

<dict>
    <key>CFBundleTypeName</key>      <string>Checksum File</string>
    <key>CFBundleTypeRole</key>      <string>Editor</string>
    <key>LSHandlerRank</key>         <string>None</string>
    <key>CFBundleTypeExtensions</key>
    <array>
        <string>sha256</string><string>sha512</string>
        <string>sha1</string><string>md5</string><string>sfv</string>
    </array>
    <key>NSIsRelatedItemType</key>   <true/>
</dict>

One detail there came from testing, not documentation: with the role set to None, the sandbox refused the write. Editor worked, and it’s what Apple’s example uses. The rank of None is my own choice, so Purrchiver never offers itself in Finder’s Open With menu for checksum files. It needs permission to write them, not to be their app.

The rule: same name, different extension

Apple’s example of a related item is a subtitle file that, by convention, has its movie’s name and a different extension. I read that loosely, as “named after it”. The sandbox looks stricter than that. On a sandboxed build, with an archive picked through a real Open panel, on macOS 27:

  • Writing photos.sha256 next to photos.zip: allowed.
  • Writing photos.zip.sha256 next to photos.zip: refused.

My reading is that to macOS, photos.zip.sha256 is a file named photos.zip with the extension sha256, and photos.zip isn’t photos. The refusal shows up in the system log, too. A UI test covers the real path as well: it creates an archive through the real Save panel in a folder outside the app’s own, and expects the short name to appear beside it.

So Purrchiver writes photos.sha256. The code that picks the name replaces the archive’s last extension instead of adding one:

// photos.zip → photos.sha256
archiveURL.deletingPathExtension().appendingPathExtension("sha256")

Nothing is lost for the person receiving it

shasum -c doesn’t care what the checksum file is called. It reads the paths inside it. Each line is a hash, two spaces, and the file’s path inside the archive:

3f2a…c91e  Photos/beach.jpg
9b07…44d0  Photos/sunset.jpg

So after extracting, anyone can check the files with the tool that’s already on their Mac, run from the folder that holds what was extracted. Here the archive’s top folder is Photos, extracted into Downloads:

cd ~/Downloads
shasum -a 256 -c Photos.sha256

Purrchiver’s own tests run exactly that: they write a checksum file for a test archive, extract it, run the real /usr/bin/shasum -c, and expect every line to come back OK.

Paths with a backslash or a newline in them are escaped the way coreutils does it, and a separate test round-trips those odd names through Purrchiver’s own reader.

What the shorter name gives up

The full-name convention exists for a reason, and I gave it up.

Purrchiver already had a function for the conventional name, and its comment argued for keeping the full name: so a.tar.gz and a.tar.xz don’t collide on one checksum file. The sandbox forced the opposite choice. Now both would want a.tar.sha256, and so would photos.zip and photos.7z in the same folder.

At first, if a Photos.sha256 was already there, from another archive or from you, writing a new one replaced it without a word, and extracting the other archive would then have been checked against the wrong list. So each list now names its archive on its first line, as a comment:

# Checksums for Photos.zip

shasum -c skips comment lines, so the file still works with it. Purrchiver won’t replace a list that names another archive, and it replaces an older list with no name only when that list covers the same files. When you extract, a list that names a different archive isn’t checked against this one; Purrchiver says whose list it is instead.

Reading had an open question too, and it mattered more. Download sites publish checksums under the conventional name, something.zip.sha256. When extracting, Purrchiver looks for that name first, then the short one. Reading goes through the same related-item rule as writing, so I expected the sandbox to refuse the long name beside a real download. It doesn’t. A UI test now opens an archive from outside the app’s container through the real Open panel, with nested.zip.sha256 beside it, and the check reads the list and verifies.

Checking on extract

When you extract an archive and its checksum file is beside it, Purrchiver checks every file and tells you which ones don’t match. A few decisions shaped that:

  • It checks the archive, not what landed on disk. If you chose to rename or skip files that already existed, the files on disk no longer line up with the list. The question the list answers is “is this the archive it describes?”, so that’s what’s checked.
  • A partial match is never a pass. A file in the archive that isn’t in the list counts as a discrepancy, not as fine.
  • A broken checksum file is reported, not ignored. If the file exists but has nothing usable in it, you’re told, rather than shown silence that looks like nothing to check.
  • Only full extracts are checked. If you extract three files, the list describes the whole archive, so the comparison wouldn’t mean anything.

Checking is free. Writing a checksum file is part of the one-time unlock, because it makes something new.

The Shortcuts actions don’t have this problem

Purrchiver’s Generate Checksums Shortcuts action names its output photos.zip.sha256, the conventional way. It can, because it never writes beside the archive. It hands the file back to Shortcuts as data, and a Save File step puts it wherever you choose. The related-item rule only applies when the app itself writes next to a file you picked.

What to take away

  • A sandboxed app writes beside your file only as a “related item”: declare the type with NSIsRelatedItemType, use the Editor role, which is what worked here, and go through NSFileCoordinator with your file as the primary item.
  • To write a related item, keep the primary file’s name and change only the extension. That’s what I measured on macOS 27: photos.sha256 could be written beside photos.zip, and photos.zip.sha256 was refused for writing, though it can be read.
  • Test outside your own container. Inside its own container an app already has access to everything, so the related-item rule never comes into play, and a test there proves nothing about a user’s Downloads folder.

Would you rather have the conventional name and a second Save panel, or the shorter name and no extra prompt?

Related posts