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.

Say what the helper would of a place it doesn't serve

Țugui Dragoș3ca24e139233
diff --git a/Packages/PeelCore/Sources/PeelCore/Plugins/Plugins.swift b/Packages/PeelCore/Sources/PeelCore/Plugins/Plugins.swift
index 3967716c..8937a46a 100644
--- a/Packages/PeelCore/Sources/PeelCore/Plugins/Plugins.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Plugins/Plugins.swift
@@ -40,7 +40,7 @@ public struct Plugin: Sendable, Hashable, Identifiable {
/// protected. Such a plug-in is listed and never offered.
public let refusal: GuardRefusal?
/// True when, moved through Peel's helper, it could not come back from History: code is loaded from its folder,
- /// and it isn't root's alone (`PrivilegedPathPolicy.keepsOut`).
+ /// and it isn't root's alone (the helper's `.loadsCode`).
public var staysInTheTrash = false
public var id: URL { url }
@@ -152,7 +152,8 @@ public enum Plugins {
requiresPrivileges: requiresPrivileges,
refusal: removalGuard.refusal(of: item.url)
)
- plugin.staysInTheTrash = requiresPrivileges && reach.keepsOut(item.url, at: item.url)
+ plugin.staysInTheTrash = requiresPrivileges
+ && reach.refusal(ofPuttingBack: item.url, at: item.url) == .loadsCode
return plugin
}
let sorted = plugins.compactMap(\.self).sorted {
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/HelperReach.swift b/Packages/PeelCore/Sources/PeelCore/Removal/HelperReach.swift
index 89a6f407..8b5d01aa 100644
--- a/Packages/PeelCore/Sources/PeelCore/Removal/HelperReach.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Removal/HelperReach.swift
@@ -25,14 +25,13 @@ struct HelperReach: Sendable {
policy.takesOnlyALink(at: url.path(percentEncoded: false))
}
- /// True when the helper would keep `trashed` out of the place `original` names, a folder code is loaded from.
- func keepsOut(_ trashed: URL, at original: URL) -> Bool {
+ /// What the helper would answer to putting `trashed` back at the place `original` names, before it opens anything.
+ func refusal(ofPuttingBack trashed: URL, at original: URL) -> PrivilegedPathPolicy.Rejection? {
var info = stat()
guard lstat(trashed.path(percentEncoded: false), &info) == 0,
let located = PathPattern.located(original.path(percentEncoded: false))
- else { return false }
- let parent = (located as NSString).deletingLastPathComponent
- return policy.keepsOut(owner: info.st_uid, mode: info.st_mode, at: located, in: parent)
+ else { return nil }
+ return policy.refusal(ofPuttingBack: located, owner: info.st_uid, mode: info.st_mode)
}
/// True when the helper would refuse `url` for where it is: outside the folders it serves, in `/Applications`
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift b/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift
index 0911496d..dd8d130f 100644
--- a/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Removal/TrashService.swift
@@ -759,8 +759,9 @@ public struct TrashService: Sendable {
guard item.isInTheTrash else { return .missingFromTrash }
guard !item.originalURL.isThere else { return .alreadyThere }
if Self.needsTheHelper(toPutBack: item),
- HelperReach(environment: environment).keepsOut(item.trashedURL, at: item.originalURL) {
- return .failed(PrivilegedPathPolicy.Rejection.loadsCode.explanation)
+ let rejection = HelperReach(environment: environment)
+ .refusal(ofPuttingBack: item.trashedURL, at: item.originalURL) {
+ return .failed(rejection.explanation)
}
return nil
}
diff --git a/Packages/PeelCore/Sources/PeelPrivileged/PrivilegedPathPolicy.swift b/Packages/PeelCore/Sources/PeelPrivileged/PrivilegedPathPolicy.swift
index 33d1bf36..db9ed40e 100644
--- a/Packages/PeelCore/Sources/PeelPrivileged/PrivilegedPathPolicy.swift
+++ b/Packages/PeelCore/Sources/PeelPrivileged/PrivilegedPathPolicy.swift
@@ -274,12 +274,25 @@ public struct PrivilegedPathPolicy: Sendable {
/// `parent`: such a folder takes back only what root owns and nobody else can write to, which is how anything the
/// helper took from there looks. Anything else could have been rewritten while it sat in the user's Trash. A
/// browser starts the program a manifest names, so a manifests folder is one that code is loaded from.
- public func keepsOut(owner: uid_t?, mode: mode_t?, at path: String, in parent: String) -> Bool {
+ private func keepsOut(owner: uid_t?, mode: mode_t?, at path: String, in parent: String) -> Bool {
let takesAnything = restorable.contains { PathComponents.isPath(path, inside: $0) }
guard !takesAnything || manifests.contains(parent) else { return false }
return owner != trustedOwner || mode.map { $0 & 0o022 != 0 } ?? true
}
+ /// What `openDestination` would answer before it opens anything, for an item `owner` owns with `mode`, nil when it
+ /// would go on to open the folder.
+ public func refusal(ofPuttingBack path: String, owner: uid_t?, mode: mode_t?) -> Rejection? {
+ let aFolder = mode.map { $0 & S_IFMT == S_IFDIR } ?? false
+ switch resolve(path, in: locations, puttingBackAFolder: aFolder) {
+ case .failure(let rejection):
+ return rejection
+ case .success(let item):
+ if links.contains(item.parent), mode.map({ $0 & S_IFMT == S_IFLNK }) != true { return .notALink }
+ return keepsOut(owner: owner, mode: mode, at: item.path, in: item.parent) ? .loadsCode : nil
+ }
+ }
+
/// Like `open`, for a path where nothing exists yet. A folder that code is loaded from keeps out what `keepsOut`
/// says.
public func openDestination(_ path: String, for trashed: OpenItem) -> Result<OpenItem, Rejection> {
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/TrashServiceTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/TrashServiceTests.swift
index 8faabe92..2690d96f 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/TrashServiceTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/TrashServiceTests.swift
@@ -597,6 +597,24 @@ struct TrashServiceTests {
#expect(throughTheHelper == .failed(PrivilegedPathPolicy.Rejection.loadsCode.explanation))
}
+ @Test(.permissionsHold) func saysWhatTheHelperWouldOfAPlaceItDoesNotServe() throws {
+ let directory = try TemporaryDirectory()
+ let locked = try directory.directory("home/Locked")
+ let trashed = try directory.file("home/.Trash/notes.txt", bytes: 1)
+ let service = TrashService(
+ environment: SearchEnvironment(
+ homeDirectory: directory.url.appending(path: "home"),
+ rootDirectory: directory.url.appending(path: "root")
+ )
+ ) { $0 }
+ let item = TrashedItem(originalURL: locked.appending(path: "notes.txt"), trashedURL: trashed, date: .now)
+ try directory.setPermissions(0o555, of: locked)
+ defer { try? directory.setPermissions(0o755, of: locked) }
+
+ let outside = PrivilegedPathPolicy.Rejection.outsideAllowedLocations.explanation
+ #expect(service.refusal(ofPuttingBack: item) == .failed(outside))
+ }
+
@Test func aMoveRefusedForAnItemLockedInFinderSaysItIsLocked() async throws {
let directory = try TemporaryDirectory()
let locked = try directory.file("home/Library/Preferences/org.example.locked.plist")