Skip to content

armgit / peel

Public Imported from GitHub
0

Uninstall Mac apps with every file they leave behind, and see why each belongs. Free up space, fine-tune macOS, and set up Terminal.

Never call a job orphaned for a program this account can't reach

Țugui Dragoș7ed1f92ec48d
diff --git a/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItemOwnership.swift b/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItemOwnership.swift
index 543ce0f9..e96ca8e6 100644
--- a/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItemOwnership.swift
+++ b/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItemOwnership.swift
@@ -129,7 +129,12 @@ public struct BackgroundItemOwnership: Sendable {
private static let standardPath = ["/usr/bin", "/bin", "/usr/sbin", "/sbin"]
private static func exists(_ program: String) -> Bool {
- guard !program.contains("/") else { return FileManager.default.fileExists(atPath: program) }
+ guard !program.contains("/") else {
+ // Gone only when nothing is there: launchd runs a daemon as root, which reaches a folder this account
+ // can't search.
+ var info = stat()
+ return stat(program, &info) == 0 || ![ENOENT, ENOTDIR, ELOOP].contains(errno)
+ }
return standardPath.contains { FileManager.default.isExecutableFile(atPath: $0 + "/" + program) }
}
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/BackgroundItemOwnershipTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/BackgroundItemOwnershipTests.swift
index bd43a88e..b9a61046 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/BackgroundItemOwnershipTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/BackgroundItemOwnershipTests.swift
@@ -42,6 +42,19 @@ struct BackgroundItemOwnershipTests {
#expect(!ownership.isOrphan(label: "org.example.cut", program: "/bin/sh\u{0}gone", owner: nil))
}
+ /// launchd runs a daemon as root, so a program in a folder this account can't search is not gone.
+ @Test(.permissionsHold) func neverCallsGoneAProgramInAFolderItCannotSearch() throws {
+ let directory = try TemporaryDirectory()
+ let program = try directory.file("Vendor/bin/agent").path(percentEncoded: false)
+ try directory.setPermissions(0, of: "Vendor")
+ defer { try? directory.setPermissions(0o755, of: "Vendor") }
+ let ownership = BackgroundItemOwnership(installedApps: []) { _ in nil }
+ let job = try #require(JobDefinition(["Label": "org.example.agent", "Program": program]))
+
+ #expect(!ownership.isOrphan(job))
+ #expect(!ownership.isOrphan(label: "org.example.agent", program: program, owner: nil))
+ }
+
/// What the helper refuses, the page must not offer: both read the same label.
@Test func knowsTheJobTheHelperWillNotActOn() {
func item(_ label: String, _ kind: BackgroundItem.Kind) -> BackgroundItem {