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.

A wallet's keys stay protected on the disk its folder was moved to

Țugui Dragoșceea4a805442
diff --git a/ARCHITECTURE.md b/ARCHITECTURE.md
index 456d7ae1..5b89b073 100644
--- a/ARCHITECTURE.md
+++ b/ARCHITECTURE.md
@@ -111,9 +111,11 @@ A handful of ideas carry most of the weight. Each is enforced in one place and t
- **One guard.** `RemovalGuard` decides, for the app and the `peel` tool alike, whether an item may move. Its
list of what nothing can bring back lives in `ProtectedData`, in PeelPrivileged, so the helper applies the
same list without asking the app. The guard judges the item and not the spelling of its path: each rule is
- asked of the path as written, as found on disk, and as the kernel names the item, then of its device and
- inode. A path is read name by name (`PathComponents`), never as text, since a slash and a combining mark after
- it are one Swift `Character`.
+ asked of the path as written, as found on disk, and as the kernel names the item, and the places it protects
+ by path (in the home, the keychains, and the folders that stay themselves) are known by device and inode as
+ well, wherever a link leads them. It finds those places without opening them, since opening a folder inside
+ another app's container makes macOS ask for that app's data. A path is read name by name (`PathComponents`),
+ never as text, since a slash and a combining mark after it are one Swift `Character`.
- **Exclusions everywhere.** The user's exclusions reach every scanner, so an excluded item never appears, and
a folder with something excluded inside is never moved.
- **Unknown stays unknown.** A size that couldn't be measured, a list a tool didn't give, an answer that didn't
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index d26ad815..074de8bb 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -119,8 +119,11 @@ Tests enforce these. If your change makes one of them fail, the rule is right an
preferences files.
It judges the item, not how its path is spelled: each rule is asked of the path as written, as found on disk,
- and as the kernel names the item, then of its device and inode, so another case, a link, or a `/.vol` name
- reaches the same answer. [SAFETY.md](SAFETY.md) lists every protected place.
+ and as the kernel names the item, so another case, a link, or a `/.vol` name reaches the same answer. The
+ places it protects by path (in the home, the keychains, and the folders that stay themselves) are known by
+ device and inode as well, wherever a link leads them, and are found without opening them
+ (`PathPattern.locatedWithoutOpening`): opening a folder inside another app's container makes macOS ask the
+ person for that app's data. [SAFETY.md](SAFETY.md) lists every protected place.
- **Paths are compared name by name, never as text.** A Swift `String` reads a slash and a combining mark after
it as one `Character`, so `hasPrefix(folder + "/")` and `split(separator: "/")` miss a folder that such a name
sits in. Every "is this inside that folder" question goes through `PathComponents`.
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/RemovalGuard.swift b/Packages/PeelCore/Sources/PeelCore/Removal/RemovalGuard.swift
index cbbe0d1f..cc5a4b56 100644
--- a/Packages/PeelCore/Sources/PeelCore/Removal/RemovalGuard.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Removal/RemovalGuard.swift
@@ -30,7 +30,7 @@ struct RemovalGuard: Sendable {
paths += homeDirectories.map { $0.path(percentEncoded: false) }
paths += environment.locations.map { $0.url.path(percentEncoded: false) }
paths += ProtectedData.sharedHomeItems.map { home.appending(path: $0).path(percentEncoded: false) }
- let trees = (ProtectedData.homeFolders + ProtectedData.homeKeys).map { home.appending(path: $0).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)
protectedObjects = ProtectedObjects(trees: trees + ProtectedData.systemFolders, folders: paths)
@@ -40,7 +40,7 @@ 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 PathPattern.spellings(of: path).union(PathPattern.located(path).map(PathPattern.spellings) ?? [])
+ return PathPattern.spellings(of: path).union(PathPattern.locatedWithoutOpening(path).map(PathPattern.spellings) ?? [])
}
func allowsRemoval(of url: URL) -> Bool {
diff --git a/Packages/PeelCore/Sources/PeelCore/Support/PathPattern.swift b/Packages/PeelCore/Sources/PeelCore/Support/PathPattern.swift
index 4dea8b38..83f944ea 100644
--- a/Packages/PeelCore/Sources/PeelCore/Support/PathPattern.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Support/PathPattern.swift
@@ -47,7 +47,11 @@ public enum PathPattern {
/// The kernel's name when `path` can be opened, or `realpath`'s when it cannot: a folder macOS keeps from
/// Peel (Mail, without Full Disk Access) does not open, but `realpath` still resolves its links.
private static func name(of path: String) -> String? {
- if let name = kernelName(of: path) { return name }
+ kernelName(of: path) ?? realName(of: path)
+ }
+
+ /// `realpath`'s name for `path`: links resolved and each name spelled as on disk, found without opening anything.
+ private static func realName(of path: String) -> String? {
guard let real = realpath(path, nil) else { return nil }
defer { free(real) }
return String(cString: real)
@@ -58,11 +62,23 @@ public enum PathPattern {
/// never followed, since a move takes a link and not what it points to. Nil when the part appended holds
/// `.` or `..`, which cannot be resolved without the folders.
static func located(_ path: String) -> String? {
+ located(path, naming: name(of:))
+ }
+
+ /// `located`, with `realpath` naming the folders. Opening a folder inside another app's container makes macOS ask
+ /// the person for that app's data, and looking a path up there does not, so this is what the guard asks of the
+ /// places it protects. `realpath` gives the kernel's answer for any path but one that begins with `/.vol/`,
+ /// `/.nofollow/` or `/.resolve/`, and a place named from a home folder never does.
+ static func locatedWithoutOpening(_ path: String) -> String? {
+ located(path, naming: realName(of:))
+ }
+
+ private static func located(_ path: String, naming name: (String) -> String?) -> String? {
guard path != "/" else { return path }
var ancestor = (path as NSString).deletingLastPathComponent
var rest = [(path as NSString).lastPathComponent]
while true {
- if let folder = name(of: ancestor) {
+ if let folder = name(ancestor) {
guard !rest.contains(where: { $0 == "." || $0 == ".." }) else { return nil }
return (folder == "/" ? "" : folder) + "/" + rest.reversed().joined(separator: "/")
}
diff --git a/Packages/PeelCore/Sources/PeelPrivileged/ProtectedData.swift b/Packages/PeelCore/Sources/PeelPrivileged/ProtectedData.swift
index d0fac8d4..71b43756 100644
--- a/Packages/PeelCore/Sources/PeelPrivileged/ProtectedData.swift
+++ b/Packages/PeelCore/Sources/PeelPrivileged/ProtectedData.swift
@@ -134,6 +134,10 @@ public enum ProtectedData: Sendable {
".kaspa",
]
+ /// Every protected place named from a home folder: `homeFolders`, `homeKeys` and `walletKeys`. `refuses`,
+ /// `holds` and `RemovalGuard` all read it, so a place added to one of those lists is protected by all three.
+ public static let homeTrees = homeFolders + homeKeys + walletKeys
+
/// Hidden items in the home folder that belong to no single app: shells, git, and the folders many tools
/// share. Never removed on any app's behalf, but what sits inside `.config`, `.cache`, and `.local` still
/// can be, so this is an exact match and not a tree.
@@ -292,7 +296,7 @@ public enum ProtectedData: Sendable {
private static func trees(under home: String) -> Trees {
treesByHome.withLock { cache in
if let trees = cache[home] { return trees }
- let trees = Trees((homeFolders + homeKeys + walletKeys).map { PathComponents.of((home as NSString).appendingPathComponent($0).lowercased()) })
+ let trees = Trees(homeTrees.map { PathComponents.of((home as NSString).appendingPathComponent($0).lowercased()) })
if cache.count >= 32 { cache.removeAll() }
cache[home] = trees
return trees
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/AttackRemovalGuardTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/AttackRemovalGuardTests.swift
index 3585ba7d..5014c082 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/AttackRemovalGuardTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/AttackRemovalGuardTests.swift
@@ -319,6 +319,27 @@ struct AttackRemovalGuardTests {
#expect(guardian.allowsRemoval(of: cache.deletingLastPathComponent()))
}
+ /// A blockchain can outgrow the startup disk, so a wallet's folder is often kept on another one, with a link to it
+ /// where the wallet looks. Its keys have to be known under the name of the disk they are on too.
+ @Test func aWalletKeptOnAnotherDiskThroughALink() throws {
+ let directory = try TemporaryDirectory()
+ let home = directory.url.appending(path: "home", directoryHint: .isDirectory)
+ let disk = directory.url.appending(path: "disk", directoryHint: .isDirectory)
+ let key = try directory.file("disk/Bitcoin/wallet.dat")
+ let wallet = try directory.file("disk/Bitcoin/wallets/default/wallet.dat")
+ let blocks = try directory.file("disk/Bitcoin/blocks/blk00000.dat", bytes: 4_096)
+ try directory.directory("home/Library/Application Support")
+ let bitcoin = disk.appending(path: "Bitcoin", directoryHint: .isDirectory)
+ try link(bitcoin.path(percentEncoded: false), at: home.appending(path: "Library/Application Support/Bitcoin").path(percentEncoded: false))
+ let guardian = RemovalGuard(environment: SearchEnvironment(homeDirectory: home, rootDirectory: directory.url.appending(path: "root")))
+
+ for item in [key, wallet, disk.appending(path: "Bitcoin/wallets"), bitcoin, disk] {
+ #expect(!guardian.allowsRemoval(of: item), "ATTACK SUCCEEDED: \(item.path(percentEncoded: false)) may be removed under the name of its disk")
+ }
+ // The blockchain beside the keys comes back on its own, wherever it is kept.
+ #expect(guardian.allowsRemoval(of: blocks.deletingLastPathComponent()))
+ }
+
/// `trashItem` moves a link, not what it points at, so an item is judged where it sits, not where it leads.
@Test func anItemUnderALinkedParentThatIsItselfALink() throws {
let directory = try TemporaryDirectory()
diff --git a/SAFETY.md b/SAFETY.md
index 76243df7..fd8576c7 100644
--- a/SAFETY.md
+++ b/SAFETY.md
@@ -82,9 +82,12 @@ folded together before the check, and there are tests that try to get past it by
can follow `/.nofollow/` or `/.resolve/0/`, and `/.vol/<device>/<inode>` reaches a file by its numbers
alone. The usual way of resolving a path folds none of them, and the Trash takes a file under each. So Peel
asks the system what it calls the item, from an open descriptor rather than from the text, and then asks the
-disk what the item *is*: every protected folder is known by its device and inode as well as by name, and
-an item is refused when it, or any folder it really sits in, is one of them. That also stops a file name
-that begins with a combining accent, which text comparison cannot see the start of.
+disk what the item *is*. Every place the table names in your home, `/Library/Keychains`, and each folder above
+that stays itself are known by their device and inode as well as by name, wherever a link leads them, and an
+item is refused when it, or any folder it really sits in, is one of them. That also stops a file name that
+begins with a combining accent, which text comparison cannot see the start of. Everything else on this page is
+judged by the item's names: as written, where it really sits, and as the system names it, spelled as the disk
+stores it.
**And the thing judged is the thing moved.** A check made by name and a move made by name are two looks at
the disk, and between them a folder can be swapped for a link into Messages by anything running as you. So
@@ -200,8 +203,9 @@ A wallet's keys can be the only way to what they hold, so Peel keeps them in fou
- **Where wallets keep their keys is never removed.** The 58 places in the table at the top of this page, each
read from the wallet's own documentation or source, are refused outright by the app, the `peel` tool, and the
- helper, and so is any folder that holds one. What comes back on its own beside a key, such as a downloaded
- blockchain, can still go.
+ helper, and so is any folder that holds one. When a wallet's folder was moved to another disk and linked back,
+ as is common once a blockchain outgrows the startup disk, its keys are refused there as well. What comes back on
+ its own beside a key, such as a downloaded blockchain, can still go.
- **A browser profile with a wallet stays.** A wallet extension such as MetaMask or Phantom keeps its vault in the
browser's profile, beside `Local Storage`, which every extension shares. So a profile that holds one of the
wallet extensions Peel knows (those of more than 60 wallets, for Chrome, Brave, Edge, Arc, Opera, Vivaldi, and