Skip to main content
coreydaley.dev

Test the signed binary, not the source: CI gates for entitlements and debug code

Purrchiver makes no network requests, and it doesn’t have the entitlement that would let it. For a while my tests checked that by reading the app’s .entitlements file. That file isn’t what ships. The entitlements in the signed binary are the file plus whatever the target’s Capabilities build settings add. Turn on “Outgoing Connections” in the Capabilities tab and the signed app gets com.apple.security.network.client while the .entitlements file, and every test that reads it, stays clean.

That’s the general problem: a check that reads the source proves what the source says. Here are three checks I moved from the source to the built product, and what one of them found on the first run.

1. Read the entitlements from the signed app

The test target is hosted in the app, so Bundle.main is the built and signed app the tests run inside. The Security framework will read its effective entitlements without shelling out to codesign, which also works inside the sandbox:

func effectiveEntitlements(of url: URL) throws -> [String: Any] {
    var code: SecStaticCode?
    guard SecStaticCodeCreateWithPath(url as CFURL, [], &code) == errSecSuccess,
          let code else { throw SigningError() }
    var info: CFDictionary?
    let flags = SecCSFlags(rawValue: kSecCSSigningInformation)
    guard SecCodeCopySigningInformation(code, flags, &info) == errSecSuccess,
          let dict = info as? [String: Any] else { throw SigningError() }
    return dict[kSecCodeInfoEntitlementsDict as String] as? [String: Any] ?? [:]
}

The test then fails on anything in a deny list:

  • com.apple.security.network.client and .network.server
  • any com.apple.security.cs.* key (Hardened Runtime exceptions)
  • any com.apple.security.application-groups key

Those are the kinds of entitlement a build setting, a template, or a dependency can add without anyone editing the .entitlements file. The same test runs against the embedded Quick Look extension at Contents/PlugIns/…appex, which has no Capabilities toggle of its own to inspect, so only the effective check covers it.

A separate test asserts the built Info.plist has no NSAppTransportSecurity key. With no network entitlement it would do nothing, but its presence would mean someone is planning to use the network.

Make sure the test can fail

A deny-list test passes on an empty dictionary. If the signing read silently returns nothing, every check passes and proves nothing. So the test first asserts that com.apple.security.app-sandbox is present. A sandboxed Mac App Store app always has it, so seeing it proves the read returned the real entitlements.

Before this, these checks were a line on my submission checklist: run codesign -d by hand and look. Now a Capabilities change fails the test run.

2. Prove debug-only code isn’t in the Release binary

Like most apps with UI tests, Purrchiver has test hooks: launch arguments and helper methods that let a UI test put the app into a known state. Some of them would be harmful in a shipping build. They’re all inside #if DEBUG, and for a while the only evidence of that was reading the source.

There are two layers now.

A source test reads the file that handles launch arguments and fails if any of the test-only argument strings appears outside an #if DEBUG region. It’s cheap and catches the obvious mistake.

A binary check builds what actually ships and looks inside it. A CI job runs xcodebuild archive with the Release configuration, unsigned (signing doesn’t change which symbols are present), then scans every Mach-O file in the archived app:

find "$APP" -type f | while read -r f; do
  file -b "$f" | grep -q 'Mach-O' || continue
  strings -a "$f"; nm "$f" 2>/dev/null
done > dump.txt

for token in "${FORBIDDEN[@]}"; do
  grep -qF -- "$token" dump.txt && { echo "leaked: $token"; fail=1; }
done

strings finds launch-argument literals. nm finds test-helper methods by their mangled Swift symbols, which still contain the method name. Scanning every Mach-O in the bundle, not just the main executable, means the check still holds if code moves into an embedded framework.

I checked that the guard discriminates: every forbidden token is present in a Debug build and absent from the Release archive. A check that never fails doesn’t tell you anything.

What it found

Wiring the guard up turned up a release blocker. A SwiftUI #Preview in one settings view called debug-only test helpers, and unlike the app’s other previews, it wasn’t wrapped in #if DEBUG. So the preview body was compiled into the Release build, where those helpers don’t exist, and xcodebuild archive, the build that goes to the App Store, failed.

Wrapping the preview in #if DEBUG fixed it. The guard now covers that case too: an unwrapped preview that touches debug code breaks the Release archive the guard depends on.

3. Pin a local binary dependency yourself

Purrchiver links a prebuilt static library for age encryption, vendored as an .xcframework and referenced from a Swift package with binaryTarget(path:). A remote binaryTarget(url:checksum:) makes SwiftPM check a checksum. A path-based one doesn’t. My repo had a .sha256 file next to the framework, and nothing ever read it, so a swapped or tampered library slice would have built and shipped.

The fix is a few lines of shell in a fast CI job:

cd Vendor && shasum -a 256 -c MyLibrary.xcframework.sha256

shasum -c recomputes every file listed in the pin and fails on a mismatch or a missing file. I checked that it fails when a pinned hash is changed.

What to take away

  • Entitlements: test the effective set from the signed binary, and include a check that proves the read worked.
  • #if DEBUG: prove it against a Release archive with strings and nm, and confirm the same check finds the tokens in a Debug build.
  • binaryTarget(path:): SwiftPM won’t verify it, so pin it and check the pin in CI.

Each of these replaced a manual step or a reading of the source. Reading the source tells you what the code says. These checks look at what Xcode actually built.

Related posts