Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
---

Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
48 changes: 39 additions & 9 deletions native2/Sources/DesktopHost/Admission.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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[..<slash]))
}
var targets: [pid_t?] = []
for id in snapshots {
if let pid = await Snapshots.shared.get(id)?.pid { targets.append(pid) }
}
if let named = arguments["pid"]?.int ?? arguments["target"]?["pid"]?.int {
let pid = pid_t(truncatingIfNeeded: named)
if let id = arguments["snapshot"]?.string, let observed = await Snapshots.shared.get(id)?.pid, observed != pid {
return .failure(TargetMismatch(message:
"invalidRequest: The pid names a different process than the snapshot, so nothing was sent. Pass the snapshot_id alone, from a fresh desktop_inspect of the window to act on."))
}
targets.append(pid)
}
return .success(targets.isEmpty ? [nil] : targets)
}

/// What an agent gets for a refusal: actions report a refused action, everything else an error.
Expand Down
81 changes: 81 additions & 0 deletions native2/Tests/RuntimeTests/AdmissionTargetTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import CoreGraphics
@testable import DesktopHost
import DesktopProtocol
import Foundation
import Testing

/// Admission checks every process a call's input can reach, whichever argument carries it: the snapshot, a
/// notification id's snapshot, a drag's to_snapshot, and an explicit pid, which must agree with the snapshot's.
@Suite(.serialized)
struct AdmissionTargetTests {
/// A process that is neither this host, a protected app nor system UI.
static let other: pid_t = 1

static func snapshot(_ pid: pid_t) async -> 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)
}
}
Loading