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.

Keep every folder on the way to a place Peel looks inside

Țugui Dragoș11ed1f489231
diff --git a/Packages/PeelCore/Sources/PeelCore/Leftovers/SearchLocation.swift b/Packages/PeelCore/Sources/PeelCore/Leftovers/SearchLocation.swift
index 27912671..160d75f5 100644
--- a/Packages/PeelCore/Sources/PeelCore/Leftovers/SearchLocation.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Leftovers/SearchLocation.swift
@@ -60,6 +60,12 @@ public struct SearchLocation: Sendable, Hashable {
public var isForLinks: Bool {
self == .commandLineTools || self == .shellCompletions
}
+
+ /// True for a folder inside a browser's own data, which goes with the browser, so the folders above it are
+ /// not kept in place for it.
+ var sitsInABrowsersOwnData: Bool {
+ self == .nativeMessagingHosts
+ }
}
public let kind: Kind
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/ProtectedObjects.swift b/Packages/PeelCore/Sources/PeelCore/Removal/ProtectedObjects.swift
index a04026bb..a365141b 100644
--- a/Packages/PeelCore/Sources/PeelCore/Removal/ProtectedObjects.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Removal/ProtectedObjects.swift
@@ -57,7 +57,7 @@ struct ProtectedObjects: Sendable {
/// Returns every folder above `path`, splitting on the slash alone as `NSString` does. A `String` treats a
/// slash followed by a combining mark as one character, so a prefix test would miss where a folder ends.
- private static func ancestors(of path: String) -> [String] {
+ static func ancestors(of path: String) -> [String] {
var ancestors: [String] = []
var folder = (path as NSString).deletingLastPathComponent
while folder != "/", !folder.isEmpty {
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/RemovalGuard.swift b/Packages/PeelCore/Sources/PeelCore/Removal/RemovalGuard.swift
index b1257749..6c9dc6f4 100644
--- a/Packages/PeelCore/Sources/PeelCore/Removal/RemovalGuard.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Removal/RemovalGuard.swift
@@ -84,9 +84,13 @@ struct RemovalGuard: Sendable {
// so they are never removed themselves.
let homeDirectories: [URL] = [home] + ProtectedData.accountFolders.map { home.appending(path: $0) }
paths += homeDirectories.map { $0.path(percentEncoded: false) }
- paths += environment.locations.map { $0.url.path(percentEncoded: false) }
+ let locations = environment.locations
+ paths += locations.filter { !$0.kind.sitsInABrowsersOwnData }.map { $0.url.path(percentEncoded: false) }
paths += SpaceInventory.emptiedFolders(in: environment).map { $0.path(percentEncoded: false) }
paths += ProtectedData.sharedHomeItems.map { home.appending(path: $0).path(percentEncoded: false) }
+ // Every folder on the way to one of these stays as well, since moving it would take the place along.
+ paths += Set(paths).flatMap(ProtectedObjects.ancestors)
+ paths += locations.filter { $0.kind.sitsInABrowsersOwnData }.map { $0.url.path(percentEncoded: false) }
let trees = ProtectedData.homeTrees.map { home.appending(path: $0).path(percentEncoded: false) }
protectedPaths = Set(paths.flatMap(Self.names))
protectedTrees = Set((trees + ProtectedData.systemFolders).flatMap(Self.names)).map(PathComponents.of)
@@ -97,9 +101,11 @@ struct RemovalGuard: Sendable {
/// reached through a link, and Spotlight names files by the disk they are on, so both forms must match.
private static func names(of path: String) -> Set<String> {
let path = normalized(path)
- return ProtectedData.spellings(of: path).union(
- PathPattern.locatedWithoutOpening(path).map(ProtectedData.spellings) ?? []
- )
+ var names = ProtectedData.spellings(of: path)
+ if let located = PathPattern.locatedWithoutOpening(path), located != path {
+ names.formUnion(ProtectedData.spellings(of: located))
+ }
+ return names
}
func allowsRemoval(of url: URL) -> Bool {
diff --git a/Packages/PeelCore/Sources/PeelPrivileged/PrivilegedPathPolicy.swift b/Packages/PeelCore/Sources/PeelPrivileged/PrivilegedPathPolicy.swift
index 3bdb3a7b..654824f5 100644
--- a/Packages/PeelCore/Sources/PeelPrivileged/PrivilegedPathPolicy.swift
+++ b/Packages/PeelCore/Sources/PeelPrivileged/PrivilegedPathPolicy.swift
@@ -82,6 +82,8 @@ public struct PrivilegedPathPolicy: Sendable {
private static let protectedFlags = UInt32(SF_RESTRICTED) | UInt32(SF_IMMUTABLE) | UInt32(SF_NOUNLINK)
private let locations: [String]
+ /// The served folders alone, without those the helper takes only a link or a manifest from.
+ private let served: [String]
private let restorable: [String]
private let applicationLocations: [String]
private let links: [String]
@@ -112,7 +114,8 @@ public struct PrivilegedPathPolicy: Sendable {
guard stat(path, &info) == 0 else { return nil }
return (info.st_dev, info.st_ino)
}
- locations = (systemLocations + [homeDirectory + "/Library"]).compactMap(Self.realPath) + links + manifests
+ served = (systemLocations + [homeDirectory + "/Library"]).compactMap(Self.realPath)
+ locations = served + links + manifests
restorable = (restoreLocations + [homeDirectory + "/Library"]).compactMap(Self.realPath)
self.applicationLocations = applicationLocations.compactMap(Self.realPath)
let library = Self.realPath(homeDirectory + "/Library") ?? homeDirectory + "/Library"
@@ -180,6 +183,12 @@ public struct PrivilegedPathPolicy: Sendable {
guard let location = locations.first(where: { PathComponents.isPath(resolved, inside: $0) }) else {
return .failure(.outsideAllowedLocations)
}
+ // A folder on the way to one Peel looks inside stays too, since moving it would take that one along.
+ let lowered = resolved.lowercased()
+ let names = PathComponents.of(lowered)
+ guard !searchedFolders.contains(where: { $0.count > names.count && $0.starts(with: names) }),
+ !served.contains(where: { PathComponents.isPath($0.lowercased(), inside: lowered) })
+ else { return .failure(.irreplaceable) }
// A tool's link sits directly in its folder, never deeper, and so does a browser's manifest.
if links.contains(location), resolvedParent != location {
return .failure(.outsideAllowedLocations)
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/AttackHelperPolicyTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/AttackHelperPolicyTests.swift
index 5eb8c7d7..bbff9084 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/AttackHelperPolicyTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/AttackHelperPolicyTests.swift
@@ -281,6 +281,39 @@ extension AttackHelperPolicyTests {
#expect(!policy(directory).open(inside.path(percentEncoded: false)).isFailure)
}
+ @Test func theHelperRefusesAFolderAroundOnePeelSearches() throws {
+ let directory = try TemporaryDirectory()
+ try directory.directory("home/Library/Application Support/Avid/Audio/Plug-Ins")
+ try directory.directory("home/Library/Application Support/Avid/Pro Tools")
+ try directory.directory("home/Library/Audio/Plug-Ins/Components")
+ try directory.directory("root/Library/Application Support/Avid/Audio/Plug-Ins")
+ try directory.directory("root/Library/Application Support/Avid/Pro Tools")
+ try directory.directory("root/Library/Application Support/Chromium/NativeMessagingHosts")
+ try directory.directory("home/Library/Application Support/Google/Chrome/NativeMessagingHosts")
+ let root = directory.url.path(percentEncoded: false) + "/"
+ let policy = PrivilegedPathPolicy(
+ homeDirectory: root + "home",
+ systemLocations: [root + "root/Library/Application Support", root + "root/Library/Application Support/Avid/Audio/Plug-Ins"],
+ restoreLocations: [],
+ manifestLocations: [root + "root/Library/Application Support/Chromium/NativeMessagingHosts"]
+ )
+ func opens(_ path: String) -> Bool {
+ !policy.open(directory.url.appending(path: path).path(percentEncoded: false)).isFailure
+ }
+
+ for holder in [
+ "home/Library/Application Support/Avid", "home/Library/Application Support/Avid/Audio", "home/Library/Audio",
+ "home/Library/Audio/Plug-Ins", "root/Library/Application Support/Avid", "root/Library/Application Support/AVID",
+ ] {
+ #expect(!opens(holder), "ATTACK SUCCEEDED: root would move \(holder) whole, and a folder Peel searches with it")
+ }
+ #expect(opens("home/Library/Application Support/Avid/Pro Tools"))
+ #expect(opens("root/Library/Application Support/Avid/Pro Tools"))
+ // A browser's own data goes with the browser, the folder it reads manifests from included.
+ #expect(opens("home/Library/Application Support/Google/Chrome"))
+ #expect(opens("root/Library/Application Support/Chromium"))
+ }
+
@Test func theHelperRefusesACachesFolderWithAnIDEsLocalHistoryThreeLevelsDown() throws {
let directory = try TemporaryDirectory()
let caches = "home/Library/Containers/com.example.ide/Data/Library/Caches"
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/AttackRemovalGuardTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/AttackRemovalGuardTests.swift
index 42601b7e..1f62dd4a 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/AttackRemovalGuardTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/AttackRemovalGuardTests.swift
@@ -334,6 +334,44 @@ struct AttackRemovalGuardTests {
/// The hidden items tools share in the home folder, such as `.config` and `.zshrc`, are refused whatever an
/// app calls itself. Anything else inside a shared folder can still go.
+ /// Peel looks inside these folders and never moves them whole, so neither may a folder on the way to one go.
+ @Test func theFolderAroundAPlacePeelLooksInside() throws {
+ let directory = try TemporaryDirectory()
+ let home = directory.url.appending(path: "home", directoryHint: .isDirectory)
+ let root = directory.url.appending(path: "root", directoryHint: .isDirectory)
+ try directory.file("home/Library/Application Support/Avid/Audio/Plug-Ins/Example.aaxplugin/Contents/Info.plist")
+ try directory.file("home/Library/Application Support/Avid/Pro Tools/settings.txt")
+ try directory.file("home/Library/Audio/Plug-Ins/Components/Example.component/Contents/Info.plist")
+ try directory.file("root/Library/Application Support/Avid/Audio/Plug-Ins/Example.aaxplugin/Contents/Info.plist")
+ try directory.file("root/opt/homebrew/share/zsh/site-functions/_example")
+ try directory.file("home/Library/Application Support/Google/Chrome/NativeMessagingHosts/com.example.app.json")
+ let trashedFolder = try directory.directory("home/.Trash/Avid")
+ let trashedFile = try directory.file("home/.Trash/Avid.txt")
+ let guardian = RemovalGuard(environment: SearchEnvironment(homeDirectory: home, rootDirectory: root))
+
+ let holders = [
+ home.appending(path: "Library/Application Support/Avid"),
+ home.appending(path: "Library/Application Support/Avid/Audio"),
+ home.appending(path: "Library/Audio"),
+ home.appending(path: "Library/Audio/Plug-Ins"),
+ root.appending(path: "Library/Application Support/Avid"),
+ root.appending(path: "opt/homebrew"),
+ ]
+ for holder in holders {
+ #expect(guardian.refusal(of: holder) == .staysItself, "ATTACK SUCCEEDED: \(holder.path(percentEncoded: false)) goes, and a folder Peel looks inside with it")
+ }
+ #expect(guardian.allowsRemoval(of: home.appending(path: "Library/Application Support/Avid/Pro Tools")))
+ #expect(guardian.allowsRemoval(of: home.appending(path: "Library/Application Support/Avid/Audio/Plug-Ins/Example.aaxplugin")))
+ #expect(guardian.allowsRemoval(of: home.appending(path: "Library/Audio/Plug-Ins/Components/Example.component")))
+ // A browser's own data goes with the browser, the folder it reads manifests from included.
+ #expect(guardian.allowsRemoval(of: home.appending(path: "Library/Application Support/Google/Chrome")))
+ let avid = home.appending(path: "Library/Application Support/Avid2")
+ #expect(guardian.allowsPuttingBack(trashedFolder, at: avid))
+ try FileManager.default.moveItem(at: home.appending(path: "Library/Application Support/Avid"), to: avid)
+ #expect(guardian.allowsPuttingBack(trashedFolder, at: home.appending(path: "Library/Application Support/Avid")))
+ #expect(!guardian.allowsPuttingBack(trashedFile, at: home.appending(path: "Library/Application Support/Avid")))
+ }
+
@Test func theFoldersTheWholeMacShares() throws {
let directory = try TemporaryDirectory()
let home = directory.url.appending(path: "home", directoryHint: .isDirectory)
diff --git a/SAFETY.md b/SAFETY.md
index a1af2618..71ab0ce8 100644
--- a/SAFETY.md
+++ b/SAFETY.md
@@ -61,8 +61,10 @@ the helper (see [The helper that runs as root](#the-helper-that-runs-as-root));
`/usr/local/Caskroom`, the record Homebrew keeps of an app it installed on an Intel Mac, which goes with the app. And
these folders themselves, though not what is inside them: `/`, `/Applications`, `/Library`, `/Users`, `/Users/Shared`,
`/Volumes`, `/opt`, `/private` and its `var`, `tmp`, and `etc`, `/cores`, your home folder, every folder Peel searches,
-such as `~/Library/Caches`, and every folder Space empties, such as `/opt/homebrew/var/log`: each is looked inside and
-never moved whole. Anything System Integrity Protection guards, which macOS marks as restricted, is refused as well, and
+such as `~/Library/Caches`, every folder on the way to one, such as `~/Library/Audio`, and every folder Space empties,
+such as `/opt/homebrew/var/log`: each is looked inside and never moved whole. A browser's own data folder is not kept
+for the folder inside it where the browser reads its extensions' manifests: it goes with the browser. Anything System
+Integrity Protection guards, which macOS marks as restricted, is refused as well, and
so is anything macOS marks so that nothing can remove or rename it, such as the private folders it makes in your
temporary folder for the processes it runs in a sandbox; Peel doesn't list those.