diff --git a/.lane/memory/native2/Sources/DesktopProtocol/Contract.swift/01M797D98JQ8Y7BHAW2XN52BAA-the-contract-is-swift-code-n.md b/.lane/memory/native2/Sources/DesktopProtocol/Contract.swift/01M797D98JQ8Y7BHAW2XN52BAA-the-contract-is-swift-code-n.md index 0571472..12c0e24 100644 --- a/.lane/memory/native2/Sources/DesktopProtocol/Contract.swift/01M797D98JQ8Y7BHAW2XN52BAA-the-contract-is-swift-code-n.md +++ b/.lane/memory/native2/Sources/DesktopProtocol/Contract.swift/01M797D98JQ8Y7BHAW2XN52BAA-the-contract-is-swift-code-n.md @@ -3,6 +3,10 @@ id: 01M797D98JQ8Y7BHAW2XN52BAA anchor: '@file' created: 2026-11-13T01:12:33Z norm: '1' +sig: 55aac2c2aa641984 +body_hash: 723f0b9a2c9a5955 +raw_hash: 532816b183b930cb +lines: 1-47 --- The contract is Swift code, not a resource file: the embedded dylib and the bundled MCP binary run from app and Ace bundles where SwiftPM's Bundle.module lookup finds no resource bundle and crashes on first use diff --git a/.lane/memory/src/mcp.ts/01M797D9876WM2MRNZ9KCCPZB6-only-the-server-instructions.md b/.lane/memory/src/mcp.ts/01M797D9876WM2MRNZ9KCCPZB6-only-the-server-instructions.md index 348b7d4..fe9008f 100644 --- a/.lane/memory/src/mcp.ts/01M797D9876WM2MRNZ9KCCPZB6-only-the-server-instructions.md +++ b/.lane/memory/src/mcp.ts/01M797D9876WM2MRNZ9KCCPZB6-only-the-server-instructions.md @@ -3,6 +3,10 @@ id: 01M797D9876WM2MRNZ9KCCPZB6 anchor: '@file' created: 2026-11-13T01:12:33Z norm: '1' +sig: 4949218de757741f +body_hash: ed447564b998bd15 +raw_hash: e6707a9db9657024 +lines: 1-207 supersedes: 01M50Y6C8Z15HEPT1M6B0E0E6S --- diff --git a/.lane/memory/src/tools.ts/01M797D97G39XZAG6X6YB9W59X-generated-wrapper-the-tools.md b/.lane/memory/src/tools.ts/01M797D97G39XZAG6X6YB9W59X-generated-wrapper-the-tools.md index cae46e4..19de005 100644 --- a/.lane/memory/src/tools.ts/01M797D97G39XZAG6X6YB9W59X-generated-wrapper-the-tools.md +++ b/.lane/memory/src/tools.ts/01M797D97G39XZAG6X6YB9W59X-generated-wrapper-the-tools.md @@ -3,6 +3,10 @@ id: 01M797D97G39XZAG6X6YB9W59X anchor: '@file' created: 2026-11-13T01:12:33Z norm: '1' +sig: e3b0c44298fc1c14 +body_hash: cb3cb3de9c506bce +raw_hash: ed48dd5c78a08e28 +lines: 1-66 supersedes: 01M7975EN9Y94PY508R9YP15Y3 --- diff --git a/CHANGELOG.md b/CHANGELOG.md index 59fd0a3..a6aaf52 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,10 @@ Notable changes to Desktop Tools, newest first. Each release's GitHub release no - Tool descriptions, schemas and the rules they share are declared in Swift, in `native2/Sources/DesktopProtocol/ContractTools.swift`, which the app's MCP server serves. `@githubnext/desktop-tools/tools` and the key and wait-condition lists in `/protocol` are generated from those declarations (`npm run contract`) and export the same values as before. `desktopRequest` no longer checks the exclusive argument forms of `desktop_clipboard_write` and `desktop_dialog_answer` itself: the host refuses them with the same text. `DESKTOP_EXPECT_OPS` lists its ops in tool order. ([#237](https://github.com/githubnext/desktop-tools/issues/237)) +### Fixed + +- Input admission checks every window a call's input can reach, through any argument that names one, against the protected-app and System Settings rules, and refuses a call whose `pid` or `target.pid` names a different process than its `snapshot`. + ## [0.20.0] - 2026-11-02 ### Added diff --git a/native2/Sources/DesktopHost/Admission.swift b/native2/Sources/DesktopHost/Admission.swift index 803e9d0..cc92099 100644 --- a/native2/Sources/DesktopHost/Admission.swift +++ b/native2/Sources/DesktopHost/Admission.swift @@ -208,23 +208,53 @@ extension Tools { if let refusal = Callers.shared.refusal(client: client) { return refusal } guard Contract.actions.contains(name) else { return nil } // Clipboard writes can't reach a prompt or menu, so only the pause holds them. + let targets: [pid_t?] + switch await targetProcesses(name, arguments) { + case let .success(found): targets = found + case let .failure(refusal): return refusal.message + } if name != "desktop_clipboard_write", Callers.shared.enforcing || Protected.shared.active { - let target = await targetProcess(arguments) // A protecting app shows its own prompts and refuses input while they're up; its windows are always open. let ownUIOpen = Callers.shared.enforcing && (Callers.shared.asking || OwnUI.promptOrMenuOpen()) - if let refusal = OwnUIGuard.refusal(input: true, targetIsOwn: target.map(OwnUI.isOwn) ?? false, ownUIOpen: ownUIOpen) { - return refusal + for target in targets { + if let refusal = OwnUIGuard.refusal(input: true, targetIsOwn: target.map(OwnUI.isOwn) ?? false, ownUIOpen: ownUIOpen) { + return refusal + } } } - if let refusal = await SystemUI.refusal(name, arguments: arguments, target: await targetProcess(arguments)) { return refusal } + for target in targets { + if let refusal = await SystemUI.refusal(name, arguments: arguments, target: target) { return refusal } + } return Pause.shared.isPaused ? Pause.message : nil } - /// The process a call names, directly or through its snapshot. - private static func targetProcess(_ arguments: JSON) async -> pid_t? { - if let pid = arguments["pid"]?.int ?? arguments["target"]?["pid"]?.int { return pid_t(pid) } - if let id = arguments["snapshot"]?.string { return await Snapshots.shared.get(id)?.pid } - return nil + struct TargetMismatch: Error { let message: String } + + /// Every process a call's input can reach, one entry per place that names one: the snapshot, the snapshot in a + /// notification id, a drag's to_snapshot, and an explicit pid or target.pid. [nil] when nothing names one. + /// + /// Dispatch acts on the snapshots, so they are what admission must check; an explicit pid that names a different + /// process than the snapshot's is refused rather than trusted, since it could only steer the check elsewhere. + static func targetProcesses(_ name: String, _ arguments: JSON) async -> Result<[pid_t?], TargetMismatch> { + var snapshots: [String] = [] + if let id = arguments["snapshot"]?.string { snapshots.append(id) } + if let id = arguments["to_snapshot"]?.string { snapshots.append(id) } + if name == "desktop_notification_action", let id = arguments["id"]?.string, let slash = id.lastIndex(of: "/") { + snapshots.append(String(id[.. String { + let id = Snapshots.newID() + await Snapshots.shared.put(Snapshot( + id: id, pid: pid, generation: 0, windowID: 1, windowBounds: .zero, imageSize: .zero, + elements: [:], focusedID: nil, lastUsed: Date())) + return id + } + + static func admission(_ name: String, _ arguments: [(String, JSON)]) async -> String? { + Protected.shared.set([getpid()]) + defer { Protected.shared.set([]) } + return await Tools.admission(name, arguments: .object(arguments), client: nil) + } + + @Test + func `input through the snapshot to the host's own window is refused`() async { + let own = await Self.snapshot(getpid()) + let refusal = await Self.admission("desktop_click", [("snapshot", JSON(own)), ("element", "e1")]) + #expect(refusal?.hasPrefix("SELF_TARGET_REFUSED") == true) + } + + @Test + func `a decoy pid beside an own snapshot is refused`() async { + let own = await Self.snapshot(getpid()) + let refusal = await Self.admission("desktop_click", [("snapshot", JSON(own)), ("element", "e1"), ("pid", JSON(Int(Self.other)))]) + #expect(refusal != nil) + } + + @Test + func `a decoy target pid beside an own snapshot is refused`() async { + let own = await Self.snapshot(getpid()) + let refusal = await Self.admission( + "desktop_click", [("snapshot", JSON(own)), ("element", "e1"), ("target", .object([("pid", JSON(Int(Self.other)))]))]) + #expect(refusal != nil) + } + + @Test + func `a pid that disagrees with the snapshot's is refused even for another app`() async { + let other = await Self.snapshot(Self.other) + let refusal = await Self.admission("desktop_click", [("snapshot", JSON(other)), ("element", "e1"), ("pid", JSON(Int(getpid()) + 100_000))]) + #expect(refusal != nil) + } + + @Test + func `a pid that agrees with the snapshot's is admitted`() async { + let other = await Self.snapshot(Self.other) + #expect(await Self.admission("desktop_click", [("snapshot", JSON(other)), ("element", "e1"), ("pid", JSON(Int(Self.other)))]) == nil) + #expect(await Self.admission("desktop_click", [("snapshot", JSON(other)), ("element", "e1")]) == nil) + } + + @Test + func `a notification action whose id carries an own snapshot is refused`() async { + let own = await Self.snapshot(getpid()) + let refusal = await Self.admission("desktop_notification_action", [("id", JSON("\(own)/e1")), ("action", "open")]) + #expect(refusal?.hasPrefix("SELF_TARGET_REFUSED") == true) + } + + @Test + func `a drag whose to_snapshot is an own window is refused`() async { + let other = await Self.snapshot(Self.other) + let own = await Self.snapshot(getpid()) + let refusal = await Self.admission("desktop_drag", [ + ("snapshot", JSON(other)), ("to_snapshot", JSON(own)), + ("from", .object([("x", 0.1), ("y", 0.1)])), ("to", .object([("x", 0.5), ("y", 0.5)])), + ]) + #expect(refusal?.hasPrefix("SELF_TARGET_REFUSED") == true) + } +}