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.

Every way a path can be written is worked out in one place

Țugui Dragoș8b60ea1df8ab
diff --git a/Packages/PeelCore/Sources/PeelCore/Developer/ProjectArtifacts.swift b/Packages/PeelCore/Sources/PeelCore/Developer/ProjectArtifacts.swift
index f1f21874..69345eca 100644
--- a/Packages/PeelCore/Sources/PeelCore/Developer/ProjectArtifacts.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Developer/ProjectArtifacts.swift
@@ -469,9 +469,9 @@ public enum ProjectArtifacts {
/// `/System/Volumes/Data/Users/me` are the same home folder, and a link into a cloud folder is that cloud folder.
public static func refusal(for root: URL, home: URL = .homeDirectory) -> Refusal? {
let spellings = ([PathPattern.comparablePath(of: root), PathPattern.canonical(root).path(percentEncoded: false)]
- + Array(PathPattern.spellings(of: PathPattern.comparablePath(of: root)))).map { $0.lowercased() }
+ + Array(ProtectedData.spellings(of: PathPattern.comparablePath(of: root)))).map { $0.lowercased() }
let homes = ([PathPattern.comparablePath(of: home), PathPattern.canonical(home).path(percentEncoded: false)]
- + Array(PathPattern.spellings(of: PathPattern.comparablePath(of: home)))).map { spelling in
+ + Array(ProtectedData.spellings(of: PathPattern.comparablePath(of: home)))).map { spelling in
var path = spelling.lowercased()
while path.count > 1, path.hasSuffix("/") { path.removeLast() }
return path
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/RemovalGuard.swift b/Packages/PeelCore/Sources/PeelCore/Removal/RemovalGuard.swift
index ea125933..a7434355 100644
--- a/Packages/PeelCore/Sources/PeelCore/Removal/RemovalGuard.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Removal/RemovalGuard.swift
@@ -82,7 +82,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.locatedWithoutOpening(path).map(PathPattern.spellings) ?? [])
+ return ProtectedData.spellings(of: path).union(PathPattern.locatedWithoutOpening(path).map(ProtectedData.spellings) ?? [])
}
func allowsRemoval(of url: URL) -> Bool {
@@ -134,7 +134,7 @@ struct RemovalGuard: Sendable {
guard !ProtectedData.holds(name, home: home) else { return .holdsProtectedData }
}
- for spelling in names.reduce(into: Set<String>(), { $0.formUnion(PathPattern.spellings(of: $1)) }) {
+ for spelling in names.reduce(into: Set<String>(), { $0.formUnion(ProtectedData.spellings(of: $1)) }) {
let isUnderAPrefix = Self.protectedPrefixes.contains { PathComponents.isPath(spelling, inside: $0) }
guard !protectedPaths.contains(spelling) else { return .staysItself }
guard !isUnderAPrefix || Self.isAToolsLink(spelling, isALink: isALink) else { return .protectedLocation }
diff --git a/Packages/PeelCore/Sources/PeelCore/Support/Exclusions.swift b/Packages/PeelCore/Sources/PeelCore/Support/Exclusions.swift
index d349deca..8f50bdb6 100644
--- a/Packages/PeelCore/Sources/PeelCore/Support/Exclusions.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Support/Exclusions.swift
@@ -132,10 +132,10 @@ public struct Exclusions: Sendable, Codable, Hashable {
return inside.filter { place in !inside.contains { $0 != place && Exclusions(paths: [$0]).excludes(place) } }
}
- /// Every way `url` can be written (see `PathPattern.spellings(of:)`). An exclusion has to match all of
+ /// Every way `url` can be written (see `ProtectedData.spellings(of:)`). An exclusion has to match all of
/// them, or an item under `/var`, `/tmp` or `/etc` would slip past it when named through `/private`.
private static func spellings(of url: URL) -> Set<String> {
- PathPattern.spellings(of: PathPattern.comparablePath(of: url))
+ ProtectedData.spellings(of: PathPattern.comparablePath(of: url))
}
/// Every spelling of every excluded path as its names. The root, which has none, is left out: it would
diff --git a/Packages/PeelCore/Sources/PeelCore/Support/PathPattern.swift b/Packages/PeelCore/Sources/PeelCore/Support/PathPattern.swift
index 83f944ea..c1f94bf7 100644
--- a/Packages/PeelCore/Sources/PeelCore/Support/PathPattern.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Support/PathPattern.swift
@@ -14,11 +14,6 @@ public enum PathPattern {
return path
}
- /// macOS keeps `/var`, `/tmp` and `/etc` as symbolic links into `/private`, so the same file has two
- /// names. Both are needed to compare paths, and resolving the link is no help for a file that is not
- /// there to resolve.
- private static let privateLinks = ["/var", "/tmp", "/etc"]
-
/// The name the kernel gives the file at `url`: one spelling, with links resolved, or `url` unchanged when
/// no name can be found. A path Peel is told about (by a cask, by an installer receipt) goes through this
/// before it can become something to remove.
@@ -96,31 +91,6 @@ public enum PathPattern {
|| ProtectedData.refuses(canonical(url).path(percentEncoded: false), home: home)
}
- /// Every name the same file answers to, in lowercase: with and without `/private`, and with links resolved.
- /// A Mac disk is case-insensitive unless formatted otherwise, so `~/Library/mail` is `~/Library/Mail`.
- /// Comparing one spelling only would let a protected path be reached by writing it differently.
- static func spellings(of path: String) -> Set<String> {
- var names = privateNames(of: path)
- let resolved = (path as NSString).resolvingSymlinksInPath
- if resolved != path {
- names.formUnion(privateNames(of: resolved))
- }
- return Set(names.map { $0.lowercased() })
- }
-
- private static func privateNames(of path: String) -> Set<String> {
- for link in privateLinks {
- if PathComponents.isPath(path, atOrInside: link) {
- return [path, "/private" + path]
- }
- let inPrivate = "/private" + link
- if PathComponents.isPath(path, atOrInside: inPrivate) {
- return [path, String(path.dropFirst("/private".count))]
- }
- }
- return [path]
- }
-
/// The existing files `pattern` names, at most `maximum`, leaving out irreplaceable ones (`isIrreplaceable`).
/// A pattern without a leading `/` or `~` is read relative to `home`.
static func expand(_ pattern: String, home: URL, maximum: Int = maximumMatches) -> [URL] {
diff --git a/Packages/PeelCore/Sources/PeelPrivileged/ProtectedData.swift b/Packages/PeelCore/Sources/PeelPrivileged/ProtectedData.swift
index e3414ee1..44b89a7a 100644
--- a/Packages/PeelCore/Sources/PeelPrivileged/ProtectedData.swift
+++ b/Packages/PeelCore/Sources/PeelPrivileged/ProtectedData.swift
@@ -330,11 +330,12 @@ public enum ProtectedData: Sendable {
}
}
- /// The lowercase names a check should compare for `path`. macOS keeps `/var`, `/tmp`, and `/etc` as
- /// symbolic links into `/private`, and disks are case-insensitive by default, so one file has several
- /// names. A check that knew only one of them could be bypassed by writing another.
+ /// The lowercase names a check should compare for `path`: as written, with its `.` and `..` read, and with
+ /// links resolved. macOS keeps `/var`, `/tmp`, and `/etc` as symbolic links into `/private`, and disks are
+ /// case-insensitive by default, so one file has several names. A check that knew only one of them could be
+ /// bypassed by writing another.
public static func spellings(of path: String) -> Set<String> {
- var names = privateNames(of: (path as NSString).standardizingPath)
+ var names = privateNames(of: path).union(privateNames(of: (path as NSString).standardizingPath))
let resolved = (path as NSString).resolvingSymlinksInPath
if resolved != path {
names.formUnion(privateNames(of: resolved))
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/AttackRemovalGuardTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/AttackRemovalGuardTests.swift
index 729240b3..7b6513a4 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/AttackRemovalGuardTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/AttackRemovalGuardTests.swift
@@ -65,6 +65,15 @@ struct AttackRemovalGuardTests {
/// Attack 1: write the protected folder in a different case. APFS is case-insensitive by default, so the
/// path still names the same file, and comparing strings must not miss it.
+ @Test func everySpellingOfAPathIsAsked() {
+ let parent = ProtectedData.spellings(of: "/Users/x/Library/Caches/../Mail/")
+ #expect(parent.contains("/users/x/library/caches/../mail"), "the path as written")
+ #expect(parent.contains("/users/x/library/mail"), "the path with its parent folder read")
+
+ let temporary = ProtectedData.spellings(of: "/private/tmp/org.example.app")
+ #expect(temporary.isSuperset(of: ["/private/tmp/org.example.app", "/tmp/org.example.app"]))
+ }
+
@Test func caseSpellingOfAProtectedTree() throws {
let directory = try TemporaryDirectory()
let home = directory.url.appending(path: "home", directoryHint: .isDirectory)