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 let an agent's label reach another job through a slash

Țugui Dragoșc1bc252bbab8
diff --git a/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItem.swift b/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItem.swift
index 6940eeec..bcae2791 100644
--- a/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItem.swift
+++ b/Packages/PeelCore/Sources/PeelCore/BackgroundItems/BackgroundItem.swift
@@ -95,14 +95,24 @@ public struct BackgroundItem: Sendable, Hashable, Identifiable {
}
/// True when Peel may start, stop, enable and disable the job: never under a label of macOS or for a system
- /// extension, and a daemon only under a label the helper takes, which also leaves out the helper itself.
+ /// extension, an agent only under a label `launchctl` reads whole, and a daemon only under a label the helper
+ /// takes, which also leaves out the helper itself.
public var canBeControlled: Bool {
- !usesALabelOfMacOS && !isSystemExtension && (!requiresPrivileges || PrivilegedPathPolicy.allowsDaemon(label))
+ !usesALabelOfMacOS && !isSystemExtension
+ && (requiresPrivileges ? PrivilegedPathPolicy.allowsDaemon(label) : isReadWholeByLaunchctl)
}
- /// True for a name Peel never passes to `launchctl` (`PrivilegedPathPolicy.isValidLabel`). Apple's names have notes
- /// of their own.
- public var hasAnUnusableLabel: Bool { !label.hasPrefix("com.apple.") && !PrivilegedPathPolicy.isValidLabel(label) }
+ /// `launchctl` reads a service's name up to the next slash, so a label with one reaches another job.
+ private var isReadWholeByLaunchctl: Bool {
+ !label.utf8.contains { $0 == 0x2F || $0 < 0x20 || $0 == 0x7F }
+ }
+
+ /// True for a name Peel never passes to `launchctl`: one the helper refuses for a daemon, or one `launchctl` would
+ /// misread for an agent. Apple's names have notes of their own.
+ public var hasAnUnusableLabel: Bool {
+ !label.hasPrefix("com.apple.")
+ && (requiresPrivileges ? !PrivilegedPathPolicy.isValidLabel(label) : !isReadWholeByLaunchctl)
+ }
/// True when Peel may disable the job: not one ServiceManagement manages, and one whose row Enable stays on.
public var mayBeDisabled: Bool { canBeControlled && !isManagedByServiceManagement && staysListedWhenDisabled }
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/BackgroundItemActionsTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/BackgroundItemActionsTests.swift
index 47ef39c1..9d15a909 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/BackgroundItemActionsTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/BackgroundItemActionsTests.swift
@@ -171,6 +171,21 @@ struct BackgroundItemActionsTests {
#expect(!daemon("com.apple.example").hasAnUnusableLabel)
}
+ @Test func anAgentWhoseNameReachesAnotherJobIsLeftAlone() {
+ func agent(_ label: String) -> BackgroundItem {
+ BackgroundItem(
+ label: label, kind: .agent, source: .userLibrary, plistURL: nil, program: "/usr/local/bin/agent",
+ runsAtLoad: true, keepsAlive: true, ownerBundleIdentifier: nil, ownerName: nil, ownerURL: nil,
+ isOrphan: false, state: .loaded, isDisabled: false
+ )
+ }
+
+ #expect(!agent("com.openssh.ssh-agent/x").canBeControlled)
+ #expect(agent("com.openssh.ssh-agent/x").hasAnUnusableLabel)
+ #expect(BackgroundItemActions.refusal(for: agent("com.openssh.ssh-agent/x")) != nil)
+ #expect(agent("org.example.agent").canBeControlled)
+ }
+
@Test func aJobInTheUserDomainIsAskedAboutThere() throws {
let directory = try TemporaryDirectory()
let job = try PropertyListSerialization.data(