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.

The helper keeps the app's three missing rules on its own

Țugui Dragoșf65f49138911
diff --git a/Packages/PeelCore/Sources/PeelCore/Leftovers/SearchLocation.swift b/Packages/PeelCore/Sources/PeelCore/Leftovers/SearchLocation.swift
index b848b62f..f3c9098c 100644
--- a/Packages/PeelCore/Sources/PeelCore/Leftovers/SearchLocation.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Leftovers/SearchLocation.swift
@@ -51,6 +51,30 @@ public struct SearchLocation: Sendable, Hashable {
}
extension SearchLocation.Kind {
+ init(_ folder: LibraryFolder) {
+ switch folder {
+ case .applicationSupport: self = .applicationSupport
+ case .applicationScripts: self = .applicationScripts
+ case .caches: self = .caches
+ case .containers: self = .containers
+ case .groupContainers: self = .groupContainers
+ case .preferences: self = .preferences
+ case .preferencesByHost: self = .preferencesByHost
+ case .savedApplicationState: self = .savedApplicationState
+ case .recentDocuments: self = .recentDocuments
+ case .logs: self = .logs
+ case .httpStorages: self = .httpStorages
+ case .webKit: self = .webKit
+ case .cookies: self = .cookies
+ case .launchAgents: self = .launchAgents
+ case .launchDaemons: self = .launchDaemons
+ case .privilegedHelperTools: self = .privilegedHelperTools
+ case .audioUnits, .audioDrivers, .vst, .vst3, .clap, .midiDrivers, .internetPlugIns, .preferencePanes, .quickLook,
+ .screenSavers, .spotlight, .services, .inputMethods, .colorPickers, .contextualMenuItems, .mailBundles:
+ self = .plugIns
+ }
+ }
+
/// True when this kind of location looks at an entry with this name. The home folder is split in two: hidden
/// entries, and the rest except the folders every account starts with. At the top of a Library, a folder
/// scanned as a location of its own is never offered whole.
@@ -91,32 +115,8 @@ public struct SearchEnvironment: Sendable {
)
}
- static let userEntries: [(SearchLocation.Kind, String)] = [
- (.applicationSupport, "Application Support"),
- (.applicationScripts, "Application Scripts"),
- (.caches, "Caches"),
- (.containers, "Containers"),
- (.groupContainers, "Group Containers"),
- (.preferences, "Preferences"),
- (.preferencesByHost, "Preferences/ByHost"),
- (.savedApplicationState, "Saved Application State"),
- (.recentDocuments, "Application Support/com.apple.sharedfilelist/com.apple.LSSharedFileList.ApplicationRecentDocuments"),
- (.logs, "Logs"),
- (.httpStorages, "HTTPStorages"),
- (.webKit, "WebKit"),
- (.cookies, "Cookies"),
- (.launchAgents, "LaunchAgents"),
- ]
-
- static let localEntries: [(SearchLocation.Kind, String)] = [
- (.applicationSupport, "Application Support"),
- (.caches, "Caches"),
- (.preferences, "Preferences"),
- (.logs, "Logs"),
- (.launchAgents, "LaunchAgents"),
- (.launchDaemons, "LaunchDaemons"),
- (.privilegedHelperTools, "PrivilegedHelperTools"),
- ]
+ static let userEntries = LibraryFolder.user.map { (SearchLocation.Kind($0), $0.rawValue) }
+ static let localEntries = LibraryFolder.local.map { (SearchLocation.Kind($0), $0.rawValue) }
/// Lowercased, because disks ignore case by default, so `desktop` is the Desktop folder.
static let accountFolderNames = Set(ProtectedData.accountFolders.map { $0.lowercased() })
diff --git a/Packages/PeelCore/Sources/PeelCore/Plugins/Plugins.swift b/Packages/PeelCore/Sources/PeelCore/Plugins/Plugins.swift
index ffb539ce..46e177b1 100644
--- a/Packages/PeelCore/Sources/PeelCore/Plugins/Plugins.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Plugins/Plugins.swift
@@ -1,4 +1,5 @@
public import Foundation
+internal import PeelPrivileged
public struct Plugin: Sendable, Hashable, Identifiable {
public enum Category: String, Sendable, Hashable, CaseIterable {
@@ -36,30 +37,41 @@ public struct Plugin: Sendable, Hashable, Identifiable {
public var id: URL { url }
}
+extension Plugin.Category {
+ init?(_ folder: LibraryFolder) {
+ switch folder {
+ case .audioUnits: self = .audioUnits
+ case .audioDrivers: self = .audioDrivers
+ case .vst: self = .vst
+ case .vst3: self = .vst3
+ case .clap: self = .clap
+ case .midiDrivers: self = .midiDrivers
+ case .internetPlugIns: self = .internetPlugIns
+ case .preferencePanes: self = .preferencePanes
+ case .quickLook: self = .quickLook
+ case .screenSavers: self = .screenSavers
+ case .spotlight: self = .spotlight
+ case .services: self = .services
+ case .inputMethods: self = .inputMethods
+ case .colorPickers: self = .colorPickers
+ case .contextualMenuItems: self = .contextualMenuItems
+ case .mailBundles: self = .mailBundles
+ case .applicationSupport, .applicationScripts, .caches, .containers, .groupContainers, .preferences,
+ .preferencesByHost, .savedApplicationState, .recentDocuments, .logs, .httpStorages, .webKit, .cookies,
+ .launchAgents, .launchDaemons, .privilegedHelperTools:
+ return nil
+ }
+ }
+}
+
/// Finds the plug-ins that apps have installed in the folders macOS loads plug-ins from. Apple's own
/// components sit in the same folders and are left out, because macOS relies on them.
public enum Plugins {
- /// The folders in each Library that macOS loads plug-ins from. `SearchEnvironment` reuses this table, so the
- /// uninstall and Orphaned Files search these folders, and `RemovalGuard` refuses to remove them. The helper's
- /// `PrivilegedPathPolicy.systemLocations` must also list each of these folders inside `/Library`.
- static let folders: [(String, Plugin.Category)] = [
- ("Audio/Plug-Ins/Components", .audioUnits),
- ("Audio/Plug-Ins/HAL", .audioDrivers),
- ("Audio/Plug-Ins/VST", .vst),
- ("Audio/Plug-Ins/VST3", .vst3),
- ("Audio/Plug-Ins/CLAP", .clap),
- ("Audio/MIDI Drivers", .midiDrivers),
- ("Internet Plug-Ins", .internetPlugIns),
- ("PreferencePanes", .preferencePanes),
- ("QuickLook", .quickLook),
- ("Screen Savers", .screenSavers),
- ("Spotlight", .spotlight),
- ("Services", .services),
- ("Input Methods", .inputMethods),
- ("ColorPickers", .colorPickers),
- ("Contextual Menu Items", .contextualMenuItems),
- ("Mail/Bundles", .mailBundles),
- ]
+ /// The folders in each Library that macOS loads plug-ins from, with what each holds. `SearchEnvironment` reuses
+ /// this table, so the uninstall and Orphaned Files search these folders.
+ static let folders = LibraryFolder.plugIns.compactMap { folder in
+ Plugin.Category(folder).map { (folder.rawValue, $0) }
+ }
/// Returns the `CFBundleIdentifier` in the plug-in's `Info.plist`. A plug-in is named for what it does, not
/// for who made it, so this is its only identifier. It is only a claim, so callers match it like a file name.
diff --git a/Packages/PeelCore/Sources/PeelPrivileged/LibraryFolder.swift b/Packages/PeelCore/Sources/PeelPrivileged/LibraryFolder.swift
new file mode 100644
index 00000000..a976371f
--- /dev/null
+++ b/Packages/PeelCore/Sources/PeelPrivileged/LibraryFolder.swift
@@ -0,0 +1,56 @@
+/// The folders of a Library that Peel looks inside, one by one, and never moves whole: the uninstall and Orphaned
+/// Files search them, the Plug-ins tool lists what they hold, and the app's guard and the helper both refuse them.
+public enum LibraryFolder: String, CaseIterable, Sendable {
+ case applicationSupport = "Application Support"
+ case applicationScripts = "Application Scripts"
+ case caches = "Caches"
+ case containers = "Containers"
+ case groupContainers = "Group Containers"
+ case preferences = "Preferences"
+ case preferencesByHost = "Preferences/ByHost"
+ case savedApplicationState = "Saved Application State"
+ case recentDocuments = "Application Support/com.apple.sharedfilelist/com.apple.LSSharedFileList.ApplicationRecentDocuments"
+ case logs = "Logs"
+ case httpStorages = "HTTPStorages"
+ case webKit = "WebKit"
+ case cookies = "Cookies"
+ case launchAgents = "LaunchAgents"
+ case launchDaemons = "LaunchDaemons"
+ case privilegedHelperTools = "PrivilegedHelperTools"
+ case audioUnits = "Audio/Plug-Ins/Components"
+ case audioDrivers = "Audio/Plug-Ins/HAL"
+ case vst = "Audio/Plug-Ins/VST"
+ case vst3 = "Audio/Plug-Ins/VST3"
+ case clap = "Audio/Plug-Ins/CLAP"
+ case midiDrivers = "Audio/MIDI Drivers"
+ case internetPlugIns = "Internet Plug-Ins"
+ case preferencePanes = "PreferencePanes"
+ case quickLook = "QuickLook"
+ case screenSavers = "Screen Savers"
+ case spotlight = "Spotlight"
+ case services = "Services"
+ case inputMethods = "Input Methods"
+ case colorPickers = "ColorPickers"
+ case contextualMenuItems = "Contextual Menu Items"
+ case mailBundles = "Mail/Bundles"
+
+ /// The folders macOS loads plug-ins from, in each Library.
+ public static let plugIns: [LibraryFolder] = [
+ .audioUnits, .audioDrivers, .vst, .vst3, .clap, .midiDrivers, .internetPlugIns, .preferencePanes, .quickLook,
+ .screenSavers, .spotlight, .services, .inputMethods, .colorPickers, .contextualMenuItems, .mailBundles,
+ ]
+
+ /// The folders searched in the home's Library, besides the plug-in folders.
+ public static let user: [LibraryFolder] = [
+ .applicationSupport, .applicationScripts, .caches, .containers, .groupContainers, .preferences,
+ .preferencesByHost, .savedApplicationState, .recentDocuments, .logs, .httpStorages, .webKit, .cookies,
+ .launchAgents,
+ ]
+
+ /// The folders searched in the Mac's own `/Library`, besides the plug-in folders.
+ public static let local: [LibraryFolder] = [
+ .applicationSupport, .caches, .preferences, .logs, .launchAgents, .launchDaemons, .privilegedHelperTools,
+ ]
+
+ public static let inTheUsersLibrary = user + plugIns
+}
diff --git a/Packages/PeelCore/Sources/PeelPrivileged/PrivilegedPathPolicy.swift b/Packages/PeelCore/Sources/PeelPrivileged/PrivilegedPathPolicy.swift
index 59aff66a..44fb01e5 100644
--- a/Packages/PeelCore/Sources/PeelPrivileged/PrivilegedPathPolicy.swift
+++ b/Packages/PeelCore/Sources/PeelPrivileged/PrivilegedPathPolicy.swift
@@ -37,33 +37,9 @@ public struct PrivilegedPathPolicy: Sendable {
}
}
- public static let systemLocations = [
- "/Applications",
- "/Library/Application Support",
- "/Library/Audio/MIDI Drivers",
- "/Library/Audio/Plug-Ins/CLAP",
- "/Library/Audio/Plug-Ins/Components",
- "/Library/Audio/Plug-Ins/HAL",
- "/Library/Audio/Plug-Ins/VST",
- "/Library/Audio/Plug-Ins/VST3",
- "/Library/Caches",
- "/Library/ColorPickers",
- "/Library/Contextual Menu Items",
- "/Library/Input Methods",
- "/Library/Internet Plug-Ins",
- "/Library/LaunchAgents",
- "/Library/LaunchDaemons",
- "/Library/Logs",
- "/Library/Mail/Bundles",
- "/Library/PreferencePanes",
- "/Library/Preferences",
- "/Library/PrivilegedHelperTools",
- "/Library/QuickLook",
- "/Library/Screen Savers",
- "/Library/Services",
- "/Library/Spotlight",
- "/private/var/db/receipts",
- ]
+ public static let systemLocations = ["/Applications"]
+ + (LibraryFolder.local + LibraryFolder.plugIns).map { "/Library/" + $0.rawValue }
+ + ["/private/var/db/receipts"]
/// The served folders where anything the helper moved may be put back (`init` adds the user's Library).
/// The others are folders that code is loaded from, and take back only what belongs to root alone (see
@@ -87,6 +63,8 @@ public struct PrivilegedPathPolicy: Sendable {
private let restorable: [String]
private let applicationLocations: [String]
private let links: [String]
+ /// The folders of the home's Library that Peel searches, lowercased, as names from the root.
+ private let searchedFolders: Set<[String]>
private let homeDirectory: String
private let trustedOwner: uid_t
@@ -104,6 +82,10 @@ public struct PrivilegedPathPolicy: Sendable {
locations = (systemLocations + [homeDirectory + "/Library"]).compactMap(Self.realPath) + links
restorable = (restoreLocations + [homeDirectory + "/Library"]).compactMap(Self.realPath)
self.applicationLocations = applicationLocations.compactMap(Self.realPath)
+ let library = Self.realPath(homeDirectory + "/Library") ?? homeDirectory + "/Library"
+ searchedFolders = Set(LibraryFolder.inTheUsersLibrary.map { folder in
+ PathComponents.of((library + "/" + folder.rawValue).lowercased())
+ })
}
/// True for a path directly in one of the folders the helper takes only a link from.
@@ -132,7 +114,11 @@ public struct PrivilegedPathPolicy: Sendable {
let parent = "/" + components.dropLast().joined(separator: "/")
guard let resolvedParent = Self.realPath(parent) else { return .failure(.unresolvableParent) }
let resolved = (resolvedParent == "/" ? "" : resolvedParent) + "/" + name
- guard !ProtectedData.refuses(resolved, home: homeDirectory) else { return .failure(.irreplaceable) }
+ guard
+ !ProtectedData.refuses(resolved, home: homeDirectory),
+ !searchedFolders.contains(PathComponents.of(resolved.lowercased())),
+ !ProtectedData.spellings(of: resolved).contains(where: ProtectedData.isInAContainersDocuments)
+ else { return .failure(.irreplaceable) }
// Refused as well when moving it would take something protected along with it. The helper runs as
// root, so it asks this itself rather than trusting the app to.
guard
diff --git a/Packages/PeelCore/Sources/PeelPrivileged/ProtectedData.swift b/Packages/PeelCore/Sources/PeelPrivileged/ProtectedData.swift
index 71b43756..e3414ee1 100644
--- a/Packages/PeelCore/Sources/PeelPrivileged/ProtectedData.swift
+++ b/Packages/PeelCore/Sources/PeelPrivileged/ProtectedData.swift
@@ -213,18 +213,23 @@ public enum ProtectedData: Sendable {
static let workKeptInCaches: Set<String> = ["localhistory", "location_data"]
/// True for a path under a `Caches` folder that is at or inside one of those folders, or holds one a level
- /// or two down, which is how deep they sit (`Caches/JetBrains/<product>/LocalHistory`). It reads the disk,
- /// so it needs the path as spelled on disk.
+ /// or two down, and for a Library's `Caches` folder holding one three levels down, which is how deep they sit
+ /// (`Caches/JetBrains/<product>/LocalHistory`). It reads the disk, so it needs the path as spelled on disk.
public static func holdsWorkKeptInACache(_ path: String) -> Bool {
let components = PathComponents.of(path).map { $0.lowercased() }
- guard let caches = components.lastIndex(of: "caches"), caches + 1 < components.count else { return false }
+ guard let caches = components.lastIndex(of: "caches") else { return false }
+ if caches + 1 == components.count {
+ return caches > 0 && components[caches - 1] == "library" && holdsWorkKeptInACache(path, levels: 3)
+ }
if components[(caches + 1)...].contains(where: workKeptInCaches.contains) { return true }
+ return holdsWorkKeptInACache(path, levels: 2)
+ }
- let names = (try? FileManager.default.contentsOfDirectory(atPath: path)) ?? []
+ private static func holdsWorkKeptInACache(_ folder: String, levels: Int) -> Bool {
+ guard levels > 0 else { return false }
+ let names = (try? FileManager.default.contentsOfDirectory(atPath: folder)) ?? []
return names.contains { name in
- if workKeptInCaches.contains(name.lowercased()) { return true }
- let deeper = (try? FileManager.default.contentsOfDirectory(atPath: path + "/" + name)) ?? []
- return deeper.contains { workKeptInCaches.contains($0.lowercased()) }
+ workKeptInCaches.contains(name.lowercased()) || holdsWorkKeptInACache(folder + "/" + name, levels: levels - 1)
}
}
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/AttackHelperPolicyTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/AttackHelperPolicyTests.swift
index 534b77a4..c6c80669 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/AttackHelperPolicyTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/AttackHelperPolicyTests.swift
@@ -215,6 +215,37 @@ extension AttackHelperPolicyTests {
}
/// The helper builds its whole policy around this string: which home is the caller's, which Trash is opened.
+ @Test func theHelperRefusesWhatAContainersDocumentsHoldOnItsOwn() throws {
+ let directory = try TemporaryDirectory()
+ let path = "home/Library/Containers/com.example.notes/Data/Documents/novel.txt"
+ try directory.file(path)
+
+ let result = policy(directory).open(directory.url.appending(path: path).path(percentEncoded: false))
+ #expect(result.isFailure, "ATTACK SUCCEEDED: root would move a document out of an app's container")
+ }
+
+ /// Peel looks inside each of these folders and offers what an app left there, never the folder itself.
+ @Test func theHelperRefusesEveryFolderPeelSearchesInTheUsersLibrary() throws {
+ let directory = try TemporaryDirectory()
+ for folder in LibraryFolder.inTheUsersLibrary {
+ let path = "home/Library/" + folder.rawValue
+ try directory.directory(path)
+ let result = policy(directory).open(directory.url.appending(path: path).path(percentEncoded: false))
+ #expect(result.isFailure, "ATTACK SUCCEEDED: root would move \(path) whole")
+ }
+ let inside = try directory.directory("home/Library/Caches/com.example.app")
+ #expect(!policy(directory).open(inside.path(percentEncoded: false)).isFailure)
+ }
+
+ @Test func theHelperRefusesACachesFolderWithAnIDEsLocalHistoryThreeLevelsDown() throws {
+ let directory = try TemporaryDirectory()
+ let caches = "home/Library/Containers/com.example.ide/Data/Library/Caches"
+ try directory.file(caches + "/JetBrains/IntelliJIdea2026.2/LocalHistory/changes.storageData")
+
+ let result = policy(directory).open(directory.url.appending(path: caches).path(percentEncoded: false))
+ #expect(result.isFailure, "ATTACK SUCCEEDED: root would move a Caches folder and the local history inside it")
+ }
+
@Test func readsTheCallersHomeFolder() throws {
let home = try #require(UserAuthorization.homeDirectory(of: getuid()))
#expect(home == String(cString: getpwuid(getuid()).pointee.pw_dir))