Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
32 changes: 32 additions & 0 deletions libs/cua-driver-rs/crates/cua-driver/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,38 @@ pub fn run_call(
process::exit(64);
}

// `check_permissions` in-process is ONLY correct when the process is
// running from CuaDriver.app (the daemon). Any one-shot CLI spawned by
// an IDE terminal (VS Code, Cursor, Claude Code, etc.) inherits the
// IDE's TCC responsibility chain, so AXIsProcessTrusted() reads against
// the IDE's bundle — not com.trycua.driver — and may report stale or
// wrong results after `tccutil reset Accessibility com.trycua.driver`.
//
// If the daemon were up we would already have forwarded above; reaching
// this branch means no daemon is listening. Warn the user and force
// `prompt: false` — the tool's default would otherwise raise a TCC
// dialog attributed to the calling shell/IDE bundle, not CuaDriver.app,
// so the user would grant the wrong identity.
//
// Mirrors Swift CallCommand.swift lines 148-171.
let json_args = if tool == "check_permissions" {
eprintln!(
"⚠️ Not running inside the cua-driver daemon process. \
Results may be inaccurate — TCC checks the calling process, not CuaDriver.app, \
so permissions granted to CuaDriver.app may read as \"NOT granted\" here.\n\
For authoritative results, start the daemon first: `cua-driver serve`, \
then re-run this check."
);
let mut coerced = json_args
.unwrap_or_else(|| serde_json::Value::Object(serde_json::Map::new()));
if let serde_json::Value::Object(ref mut map) = coerced {
map.insert("prompt".into(), serde_json::Value::Bool(false));
}
Some(coerced)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Verify the current coercion only mutates object-shaped args and that tool default is prompt=true.
rg -n -C3 'if let serde_json::Value::Object\(ref mut map\)' libs/cua-driver-rs/crates/cua-driver/src/cli.rs
rg -n -C3 'unwrap_or\(true\)' libs/cua-driver-rs/crates/platform-macos/src/tools/check_permissions.rs

Repository: trycua/cua

Length of output: 1526


🏁 Script executed:

#!/bin/bash
# Check where json_args comes from and if non-object values are realistic
rg -n -B15 'let mut coerced = json_args' libs/cua-driver-rs/crates/cua-driver/src/cli.rs | head -40

Repository: trycua/cua

Length of output: 973


🏁 Script executed:

#!/bin/bash
# See initial json_args assignment and full context of this block
sed -n '630,690p' libs/cua-driver-rs/crates/cua-driver/src/cli.rs

Repository: trycua/cua

Length of output: 2648


🏁 Script executed:

#!/bin/bash
# Find function signature to see json_args parameter type/source
rg -n -B20 'let json_args = if tool == "check_permissions"' libs/cua-driver-rs/crates/cua-driver/src/cli.rs | grep -E '(fn|json_args)'

Repository: trycua/cua

Length of output: 111


🏁 Script executed:

#!/bin/bash
# Find the function signature containing this code
sed -n '600,680p' libs/cua-driver-rs/crates/cua-driver/src/cli.rs | head -40

Repository: trycua/cua

Length of output: 2237


🏁 Script executed:

#!/bin/bash
# Find the function containing this code block
rg -n 'fn.*json_args|fn main|pub fn' libs/cua-driver-rs/crates/cua-driver/src/cli.rs | head -10

Repository: trycua/cua

Length of output: 691


🏁 Script executed:

#!/bin/bash
# Get the run_call function signature and initial lines
sed -n '576,620p' libs/cua-driver-rs/crates/cua-driver/src/cli.rs

Repository: trycua/cua

Length of output: 2476


🏁 Script executed:

#!/bin/bash
# Find where run_call is invoked to see what json_args values are passed
rg -n 'run_call\(' libs/cua-driver-rs/crates/cua-driver/src/cli.rs

Repository: trycua/cua

Length of output: 75


🏁 Script executed:

#!/bin/bash
# Search main and command handlers for where run_call is invoked
rg -n 'run_call' libs/cua-driver-rs/crates/cua-driver/src/cli.rs

Repository: trycua/cua

Length of output: 75


🏁 Script executed:

#!/bin/bash
# Search entire crate for invocations of run_call
rg -n 'run_call' libs/cua-driver-rs/crates/cua-driver/src/

Repository: trycua/cua

Length of output: 510


🏁 Script executed:

#!/bin/bash
# Check main.rs to see where json_args comes from
sed -n '100,130p' libs/cua-driver-rs/crates/cua-driver/src/main.rs
sed -n '340,360p' libs/cua-driver-rs/crates/cua-driver/src/main.rs

Repository: trycua/cua

Length of output: 2810


🏁 Script executed:

#!/bin/bash
# Find the Command enum definition to see json_args type
rg -n 'enum Command|struct Call' libs/cua-driver-rs/crates/cua-driver/src/cli.rs | head -20

Repository: trycua/cua

Length of output: 76


🏁 Script executed:

#!/bin/bash
# Show the Command enum definition
sed -n '19,100p' libs/cua-driver-rs/crates/cua-driver/src/cli.rs

Repository: trycua/cua

Length of output: 4037


🏁 Script executed:

#!/bin/bash
# Find where json_args is parsed from command line arguments
rg -n 'json_args' libs/cua-driver-rs/crates/cua-driver/src/cli.rs | head -20

Repository: trycua/cua

Length of output: 695


🏁 Script executed:

#!/bin/bash
# See how json_args is parsed from command line input
sed -n '200,220p' libs/cua-driver-rs/crates/cua-driver/src/cli.rs
sed -n '245,265p' libs/cua-driver-rs/crates/cua-driver/src/cli.rs

Repository: trycua/cua

Length of output: 1773


🏁 Script executed:

#!/bin/bash
# Verify the suggested fix logic is sound
cat <<'EOF' > /tmp/test_fix.rs
// Current buggy code:
let json_args_buggy = Some(serde_json::json!(true)); // or [], "str", etc.
let mut coerced = json_args_buggy
    .unwrap_or_else(|| serde_json::Value::Object(serde_json::Map::new()));
if let serde_json::Value::Object(ref mut map) = coerced {
    map.insert("prompt".into(), serde_json::Value::Bool(false));
}
// Result: coerced is still true, no "prompt" key inserted

// Suggested fixed code:
let json_args_fixed = Some(serde_json::json!(true)); // or [], "str", etc.
let mut map = match json_args_fixed {
    Some(serde_json::Value::Object(map)) => map,
    _ => serde_json::Map::new(),
};
map.insert("prompt".into(), serde_json::Value::Bool(false));
let result = Some(serde_json::Value::Object(map));
// Result: always a new Object with "prompt": false
EOF
cat /tmp/test_fix.rs

Repository: trycua/cua

Length of output: 833


Force prompt: false for all arg shapes, not only object args.

When json_args is a non-object JSON value (e.g., true, [], or "string"), this branch leaves it unchanged, and check_permissions falls back to prompt=true. That re-enables the misattributed TCC prompt in exactly the path this PR is trying to prevent. Users can pass non-object JSON via the command line, so this is not an edge case.

Suggested fix
-        let mut coerced = json_args
-            .unwrap_or_else(|| serde_json::Value::Object(serde_json::Map::new()));
-        if let serde_json::Value::Object(ref mut map) = coerced {
-            map.insert("prompt".into(), serde_json::Value::Bool(false));
-        }
-        Some(coerced)
+        let mut map = match json_args {
+            Some(serde_json::Value::Object(map)) => map,
+            _ => serde_json::Map::new(),
+        };
+        map.insert("prompt".into(), serde_json::Value::Bool(false));
+        Some(serde_json::Value::Object(map))
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
let mut coerced = json_args
.unwrap_or_else(|| serde_json::Value::Object(serde_json::Map::new()));
if let serde_json::Value::Object(ref mut map) = coerced {
map.insert("prompt".into(), serde_json::Value::Bool(false));
}
Some(coerced)
let mut map = match json_args {
Some(serde_json::Value::Object(map)) => map,
_ => serde_json::Map::new(),
};
map.insert("prompt".into(), serde_json::Value::Bool(false));
Some(serde_json::Value::Object(map))
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@libs/cua-driver-rs/crates/cua-driver/src/cli.rs` around lines 674 - 679, The
current code only sets prompt:false when json_args is an Object, leaving
non-object json_args (e.g., true, [], "str") unchanged and causing
check_permissions to treat them as prompt:true; change the coercion so that
regardless of the shape of json_args the returned value is an Object with
prompt:false: capture the original json_args, create a serde_json::Map, insert
("prompt", serde_json::Value::Bool(false)), then if the original was an Object
merge its entries into the map else insert the original under a new key (e.g.,
"value") before returning Some(serde_json::Value::Object(map)); update the code
around the variables json_args and coerced to implement this behavior.

} else {
json_args
};

let rt = tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()
Expand Down
15 changes: 15 additions & 0 deletions libs/cua-driver-rs/crates/platform-macos/src/permissions/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,21 @@ pub fn current_status() -> PermissionsStatus {
}

/// Live AX trust state — `AXIsProcessTrusted()`.
///
/// ⚠️ Attribution caveat: when called from a CLI process spawned by a
/// terminal application (Terminal.app, VS Code, Cursor, etc.), macOS
/// attributes the calling process to the *responsible* parent application
/// rather than to the codesigned CuaDriver.app bundle. This means
/// `AXIsProcessTrusted()` reflects the parent's Accessibility grant, not
/// CuaDriver.app's — so results can be stale or misleading after
/// `tccutil reset Accessibility com.trycua.driver`.
///
/// This is only a concern for in-process CLI calls without a running daemon.
/// When the daemon is up, `run_call` forwards through the Unix socket and
/// the check runs inside the daemon process (CuaDriver.app), where the
/// attribution is correct.
///
/// See: https://github.com/trycua/cua/issues/1561
pub fn accessibility_granted() -> bool {
unsafe { crate::ax::bindings::AXIsProcessTrusted() }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,15 @@ fn def() -> &'static ToolDef {
By default also raises the system permission dialogs for any missing grants — \
Apple's request APIs are no-ops when the grant is already active, so this is \
safe to call repeatedly. Pass {\"prompt\": false} for a purely read-only \
status check.".into(),
status check.\n\n\
⚠️ CLI caveat: when called from a shell or IDE terminal without a running \
cua-driver daemon, macOS attributes the calling process to the parent \
application (Terminal.app, VS Code, Cursor, etc.) rather than to \
CuaDriver.app. AXIsProcessTrusted() therefore reflects the parent's \
Accessibility grant, not CuaDriver.app's — so the result can be stale or \
misleading after `tccutil reset Accessibility com.trycua.driver`. \
For authoritative results, start the daemon first \
(`cua-driver serve` or open CuaDriver.app), then re-run this check.".into(),
input_schema: serde_json::json!({
"type": "object",
"properties": {
Expand Down