fix: scope log file to bundle ID on unsandboxed macOS#202
Open
czottmann wants to merge 3 commits intoAvdLee:mainfrom
Open
fix: scope log file to bundle ID on unsandboxed macOS#202czottmann wants to merge 3 commits intoAvdLee:mainfrom
czottmann wants to merge 3 commits intoAvdLee:mainfrom
Conversation
The log path resolved via `FileManager.applicationSupportDirectory` lands in the shared `~/Library/Application Support/` directory on unsandboxed macOS apps, so `diagnostics_log.txt` collides between every app that embeds the package. Sandboxed processes (iOS, tvOS, watchOS, sandboxed macOS) are unaffected because the system already scopes that directory per container. When the current process lacks the `com.apple.security.app-sandbox` entitlement (detected via `SecTaskCopyValueForEntitlement`), append `Bundle.main.bundleIdentifier` as a subdirectory so each app gets its own log file. `createDirectory(... withIntermediateDirectories: true)` already handles creating the new intermediate path.
Flip the private `FileManager` extension in `DiagnosticsLogger.swift` to internal so the test target can see `applicationSupportDirectory` and `isSandboxed`. Add a macOS-gated regression test that asserts the resolved Application Support directory is scoped by `Bundle.main.bundleIdentifier` when the current process is unsandboxed (`swift test` always is), so any future change that reintroduces the shared-path regression fails loudly. Only the unsandboxed branch is exercised — the sandboxed branch isn't reachable from a test process without a real sandboxed app host.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
On unsandboxed macOS apps,
diagnostics_log.txtis written to the shared~/Library/Application Support/directory, so every app embedding this package writes to — and overwrites — the same file. Sandboxed processes (iOS, tvOS, watchOS, sandboxed macOS) aren't affected because the system already scopes that directory per container.This PR scopes the log file per app on unsandboxed macOS by appending
Bundle.main.bundleIdentifieras a subdirectory.Behavior
~/Library/Application Support/<bundleID>/diagnostics_log.txt.Implementation notes
SecTaskCreateFromSelf+SecTaskCopyValueForEntitlement("com.apple.security.app-sandbox"), so the check reflects the actual code signature rather than environment variables (which can be inherited or spoofed).#if os(macOS)— non-macOS platforms compile identically to before.createDirectory(..., withIntermediateDirectories: true)call at the setup site handles the new intermediate path — no call-site changes needed.FileManagerextension is now internal so the test target can reachapplicationSupportDirectoryandisSandboxed. It is not part of the public API.Test plan
swift buildgreenAppSystemMetadataReporterTests/testMetadataexists onmainand is not affected by this change)DiagnosticsLoggerTests.testApplicationSupportDirectoryIsScopedByBundleIDWhenUnsandboxed(macOS-gated) asserts the resolved Application Support directory ends with/<Bundle.main.bundleIdentifier>when running unsandboxed (which theswift testrunner always is)