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.

Peel holds the helper's replies to strings on its own side

Țugui Dragoș4777a7fc4481
diff --git a/Packages/PeelCore/Sources/PeelCore/Removal/PrivilegedHelper.swift b/Packages/PeelCore/Sources/PeelCore/Removal/PrivilegedHelper.swift
index 097a9395..18324480 100644
--- a/Packages/PeelCore/Sources/PeelCore/Removal/PrivilegedHelper.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Removal/PrivilegedHelper.swift
@@ -214,6 +214,19 @@ public enum PrivilegedHelper {
}
}
+ /// The helper's interface as Peel calls it. NSXPC checks the classes of a reply where it is decoded, which is
+ /// here, so the two dictionaries the trash method answers with are held to strings on this side: anything else
+ /// would stop Peel when Swift reads them.
+ static func remoteInterface() -> NSXPCInterface {
+ let interface = NSXPCInterface(with: (any PeelHelperProtocol).self)
+ let trash = #selector((any PeelHelperProtocol).moveItemsToTrash(version:atPaths:withReply:))
+ let strings = NSSet(array: [NSDictionary.self, NSString.self]) as? Set<AnyHashable> ?? []
+ for index in 0...1 {
+ interface.setClasses(strings, for: trash, argumentIndex: index, ofReply: true)
+ }
+ return interface
+ }
+
private static func withHelper<T: Sendable>(
fallback: T,
_ body: (any PeelHelperProtocol, @escaping @Sendable (T) -> Void) -> Void
@@ -221,7 +234,7 @@ public enum PrivilegedHelper {
guard let teamIdentifier = CodeSigning.currentTeamIdentifier() else { return fallback }
let connection = NSXPCConnection(machServiceName: HelperIdentity.machServiceName, options: .privileged)
- connection.remoteObjectInterface = NSXPCInterface(with: (any PeelHelperProtocol).self)
+ connection.remoteObjectInterface = remoteInterface()
connection.setCodeSigningRequirement(
CodeSigning.requirement(identifier: HelperIdentity.helperIdentifier, teamIdentifier: teamIdentifier)
)
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/HelperReplyTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/HelperReplyTests.swift
new file mode 100644
index 00000000..7ef2d7b5
--- /dev/null
+++ b/Packages/PeelCore/Tests/PeelCoreTests/HelperReplyTests.swift
@@ -0,0 +1,74 @@
+import Foundation
+@testable import PeelCore
+import PeelPrivileged
+import Synchronization
+import Testing
+
+/// The helper's trash method as a dishonest helper would answer it: the same selector, with replies of any kind.
+@objc private protocol LooseTrashReplies {
+ @objc(moveItemsToTrashWithVersion:atPaths:withReply:)
+ func moveItemsToTrash(version: Int, atPaths paths: [String], withReply reply: @escaping (NSDictionary, NSDictionary) -> Void)
+}
+
+private final class Answers: NSObject, LooseTrashReplies, NSXPCListenerDelegate {
+ let moved: NSDictionary
+
+ init(moved: NSDictionary) {
+ self.moved = moved
+ }
+
+ func moveItemsToTrash(version: Int, atPaths paths: [String], withReply reply: @escaping (NSDictionary, NSDictionary) -> Void) {
+ reply(moved, [:] as NSDictionary)
+ }
+
+ func listener(_ listener: NSXPCListener, shouldAcceptNewConnection connection: NSXPCConnection) -> Bool {
+ connection.exportedInterface = NSXPCInterface(with: LooseTrashReplies.self)
+ connection.exportedObject = self
+ connection.resume()
+ return true
+ }
+}
+
+struct HelperReplyTests {
+ private static let path = "/Users/x/Library/Caches/org.example.app"
+
+ /// NSXPC checks a reply's classes where the reply is decoded, which is Peel's side, so Peel accepts from the
+ /// helper only the strings its answer is made of.
+ @Test func refusesAReplyThatIsNotStrings() async throws {
+ #expect(await answer(to: [Self.path: NSNumber(value: 42)]) == "refused: \(NSXPCConnectionReplyInvalid)")
+ }
+
+ @Test func takesAReplyOfStrings() async throws {
+ #expect(await answer(to: [Self.path: "/Users/x/.Trash/org.example.app"]) == "accepted 1")
+ }
+
+ private func answer(to moved: NSDictionary) async -> String {
+ let helper = Answers(moved: moved)
+ let listener = NSXPCListener.anonymous()
+ listener.delegate = helper
+ listener.resume()
+ defer { listener.invalidate() }
+ let connection = NSXPCConnection(listenerEndpoint: listener.endpoint)
+ connection.remoteObjectInterface = PrivilegedHelper.remoteInterface()
+ connection.resume()
+ defer { connection.invalidate() }
+
+ let answer = await withCheckedContinuation { (continuation: CheckedContinuation<String, Never>) in
+ let answered = Mutex(false)
+ let finish: @Sendable (String) -> Void = { text in
+ let isFirst = answered.withLock { answered in
+ defer { answered = true }
+ return !answered
+ }
+ if isFirst { continuation.resume(returning: text) }
+ }
+ let proxy = connection.remoteObjectProxyWithErrorHandler { error in
+ finish("refused: \((error as NSError).code)")
+ } as? any PeelHelperProtocol
+ proxy?.moveItemsToTrash(version: HelperIdentity.protocolVersion, atPaths: [Self.path]) { moved, _ in
+ finish("accepted \(moved.count)")
+ }
+ }
+ return answer
+ }
+}