ucu-mcp 0.6.3 → 0.6.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -5,6 +5,31 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
+ ## [0.6.4] - 2026-06-18
9
+
10
+ 4 个独立子 Agent 全面 code review 后的修复(skylight/element/tests/security)。
11
+
12
+ ### P0 Fixed(bugs/security)
13
+
14
+ - **focusWithoutRaise 返回值检查反转**(skylight/main.swift:170):`!= 0` 应为 `== 0`(OSStatus 0=success)。原代码导致 focus-without-raise 完全失效——per-pid 点击落到 AppKit 当前认为 active 的窗口,不一定是目标。
15
+ - **runInputChecked cgevent fallback ENOENT**(utils/input.ts):skylight-only 环境下 skylight 运行时错误会直接 throw ENOENT(cgevent-helper 不存在)。加 isCgeventAvailable 门控 + try/catch。
16
+ - **appNameMatches 过度匹配**(helpers.ts):双向 includes 导致 `"code"` 匹配 `"vscode"`。改为单向 process.includes(requested) + 最小 3 字符长度。
17
+ - **needCoordinateClick 静默返回 axpress 成功**(element.ts):JXA 请求坐标点击但 cx/cy 缺失时,原代码 fallthrough 返回 `{method:"axpress"}`(谎报成功)。改为抛 ElementNotFoundError。
18
+ - **normalizeShortcut 别名绕过**(guard.ts):`escape`→`esc`、`delete`→`del`、`return`→`enter` 不规范化,blocklist 可被长格式键名绕过(如 `cmd+option+escape` 不匹配 `command+esc+option`)。加 KEY_CANONICAL 表。
19
+ - **find_element 密码字段不脱敏**(element-tools.ts):AXSecureTextField/AXPasswordField 的 value 原样返回给模型。加 SENSITIVE_RE 脱敏为 `[REDACTED]`。
20
+ - **clipboard_read 重分类为 input**(guard.ts):从 OBSERVE_ACTIONS 移到 INPUT_ACTIONS。clipboard 可能含密码/TOTP,应受 user-activity pause 保护。
21
+
22
+ ### P1 Fixed(correctness/robustness)
23
+
24
+ - **isSkylightAvailable 不检查 skylight:true**(utils/input.ts):只查 `"ok"` 不查 `"skylight":true`,SPI 加载失败时仍认为可用。改为 regex 匹配 `/"skylight"\s*:\s*true/`。
25
+ - **keepTargetAxAlive execFileSync 阻塞事件循环 5s**(window.ts):fire-and-forget 但 execFileSync 同步阻塞。改为 setImmediate 让出 tick + timeout 5s→1s。
26
+ - **doScroll 误报 per-pid**(skylight/main.swift):scroll 事件无 field-40/window-loc 标记但返回 `method:"per-pid"`,调用方误以为成功。诚实报告 `hid-tap`。
27
+ - **SENSITIVE_PROCESS_HINTS 不含空格变体 + 缺失条目**(input.ts):`"keychainaccess"` 不匹配 `"keychain access"`(带空格)。改为 normalized 比对(strip 非字母数字)+ 补 keychain/keepassxc/coreauthui/passwords/systemsettings 等。
28
+
29
+ ### Tests
30
+
31
+ - 328 passed(修复 clipboard_read 分类测试 + jxaOnlyPlatform 加 skylight:null 避免 keepTargetAxAlive 消耗 mock)。
32
+
8
33
  ## [0.6.3] - 2026-06-17
9
34
 
10
35
  SKILL.md Confirmation Policy + 修 permissions.test.ts CI 在 Ubuntu 全挂的 bug。
@@ -19,6 +19,14 @@ export function registerElementTools(registerTool) {
19
19
  const safetyCtx = await getSafetyContext(undefined);
20
20
  const response = await withSafety({ action: "find_element", params: { ...safetyCtx }, requiresAccessibility: true,
21
21
  execute: () => getPlatform().findElement({ text: params.text, role: params.role, app: effectiveApp, depth: params.depth, includeBounds: params.includeBounds, maxResults: params.maxResults, textMode: params.textMode, visibleOnly: params.visibleOnly, value: params.value, index: params.index, near: params.near }) });
22
+ // Security: mask password/secret field values before returning to the model.
23
+ const SENSITIVE_RE = /password|passwd|secret|pincode|pin\b|token|credential|api[_-]?key|access[_-]?key|private[_-]?key/i;
24
+ const SENSITIVE_ROLES = new Set(["AXSecureTextField", "AXPasswordField"]);
25
+ for (const r of response.results) {
26
+ if (SENSITIVE_ROLES.has(r.role) || SENSITIVE_RE.test(r.name || "")) {
27
+ r.value = "[REDACTED]";
28
+ }
29
+ }
22
30
  const payload = { results: response.results, metrics: response.metrics };
23
31
  if (response.results.length === 0 && effectiveApp && response.metrics.scannedCount === 0) {
24
32
  payload.hint =
@@ -148,7 +148,10 @@ export async function clickElement(elementId, app) {
148
148
  }
149
149
  // JXA requested a coordinate click → perform it via the input layer (per-process
150
150
  // when a pid is available, so the global cursor does not move).
151
- if (result.needCoordinateClick && typeof result.cx === "number" && typeof result.cy === "number") {
151
+ if (result.needCoordinateClick) {
152
+ if (typeof result.cx !== "number" || typeof result.cy !== "number") {
153
+ throw new ElementNotFoundError(`${elementId}: element requested coordinate click but reported no bounds (offscreen?)`);
154
+ }
152
155
  await this.click(Math.round(result.cx), Math.round(result.cy), "left", false);
153
156
  return { method: "coordinate", verified: false };
154
157
  }
@@ -564,7 +567,10 @@ export async function clickMenuBarExtra(app, selector = {}) {
564
567
  if (!result.success) {
565
568
  throw new Error(`click_menu_bar_extra failed in ${app}: ${result.error}`);
566
569
  }
567
- if (result.needCoordinateClick && typeof result.cx === "number" && typeof result.cy === "number") {
570
+ if (result.needCoordinateClick) {
571
+ if (typeof result.cx !== "number" || typeof result.cy !== "number") {
572
+ throw new ElementNotFoundError(`menu bar extra in ${app}: requested coordinate click but no bounds`);
573
+ }
568
574
  await this.click(Math.round(result.cx), Math.round(result.cy), "left", false);
569
575
  return { method: "coordinate", verified: false };
570
576
  }
@@ -47,10 +47,14 @@ export function appNameMatches(processName, requestedApp) {
47
47
  const requested = normalizeAppName(requestedApp);
48
48
  if (!process || !requested)
49
49
  return false;
50
- // normalizeAppName strips all non-alphanumerics, so equality is the only
51
- // meaningful comparison. (Prior startsWith/includes with spaces were dead code
52
- // since normalized strings contain no spaces/hyphens.)
53
- return process === requested || process.includes(requested) || requested.includes(process);
50
+ if (process === requested)
51
+ return true;
52
+ // Substring match only for requests >= 3 chars to avoid "code"→"vscode".
53
+ // Only allow process.includes(requested) (not bidirectional) to prevent
54
+ // short requests greedily absorbing longer unrelated process names.
55
+ if (requested.length >= 3 && process.includes(requested))
56
+ return true;
57
+ return false;
54
58
  }
55
59
  export function selectWindowForApp(windows, requestedApp) {
56
60
  const requested = normalizeAppName(requestedApp);
@@ -9,9 +9,12 @@ import { rethrowInputError, errorMessage } from "./helpers.js";
9
9
  * manager or auth dialog. These fall back to HID-tap (which at least requires
10
10
  * the app to be frontmost / visible to the user).
11
11
  */
12
+ // Normalized (non-alphanumeric-stripped) so "Keychain Access" → "keychainaccess" matches.
12
13
  const SENSITIVE_PROCESS_HINTS = [
13
- "keychainaccess", "1password", "lastpass", "bitwarden", "securityagent",
14
- "authd", "loginwindow", "applekeychain", "dashlane",
14
+ "keychainaccess", "keychain", "1password", "lastpass", "bitwarden", "keepassxc",
15
+ "securityagent", "coreauthui", "authd", "loginwindow", "applekeychain", "dashlane",
16
+ "enpass", "keeper", "passwords", // macOS Passwords app (Sequoia+)
17
+ "systempreferences", "systemsettings", // where ucu-mcp's own permissions live
15
18
  ];
16
19
  /** Resolve the per-process event target from the active focus_app target (pid + windowNumber). */
17
20
  function targetOf() {
@@ -19,7 +22,8 @@ function targetOf() {
19
22
  if (!t || !t.pid || t.pid <= 0)
20
23
  return undefined;
21
24
  // Security: never route per-process events into sensitive apps (password managers / auth).
22
- const name = (t.appName || "").toLowerCase();
25
+ // Normalize by stripping non-alphanumerics so "Keychain Access" → "keychainaccess".
26
+ const name = (t.appName || "").toLowerCase().replace(/[^a-z0-9]/g, "");
23
27
  if (SENSITIVE_PROCESS_HINTS.some((h) => name.includes(h))) {
24
28
  return undefined; // forces HID-tap fallback (requires frontmost)
25
29
  }
@@ -108,16 +108,25 @@ export async function keepTargetAxAlive(pid) {
108
108
  const helperPath = resolveNativeHelper.call(this, "skylight", "skylight-helper");
109
109
  if (!helperPath)
110
110
  return;
111
- try {
112
- execFileSync(helperPath, [], {
113
- input: JSON.stringify({ command: "keepAlive", pid }),
114
- encoding: "utf8",
115
- timeout: 5000,
111
+ // Defer to next tick so execFileSync doesn't block the current event loop tick.
112
+ // Lower timeout (1s) keepAlive should be near-instant; 5s was too long.
113
+ await new Promise((resolve) => {
114
+ setImmediate(() => {
115
+ try {
116
+ execFileSync(helperPath, [], {
117
+ input: JSON.stringify({ command: "keepAlive", pid }),
118
+ encoding: "utf8",
119
+ timeout: 1000,
120
+ });
121
+ }
122
+ catch {
123
+ // keepAlive is best-effort; ignore failures (helper missing / SPI unavailable).
124
+ }
125
+ finally {
126
+ resolve();
127
+ }
116
128
  });
117
- }
118
- catch {
119
- // keepAlive is best-effort; ignore failures (helper missing / SPI unavailable).
120
- }
129
+ });
121
130
  }
122
131
  export async function getActiveBrowserContext(app) {
123
132
  const appName = app || this.activeTarget?.appName;
@@ -24,7 +24,7 @@ export interface SafetyGuardConfig {
24
24
  }
25
25
  /** Actions that observe UI/system state without altering it. */
26
26
  export declare const OBSERVE_ACTIONS: ReadonlySet<string>;
27
- /** Actions that synthesize user input — need full user-activity protection. */
27
+ /** Actions that synthesize user input OR access sensitive data — need full user-activity protection. */
28
28
  export declare const INPUT_ACTIONS: ReadonlySet<string>;
29
29
  export declare function classifyAction(action: string): "observe" | "input" | "other";
30
30
  export declare class SafetyGuard {
@@ -84,13 +84,21 @@ const MODIFIER_CANONICAL = {
84
84
  };
85
85
  /** Normalize a shortcut string to lowercase, trimmed, sorted modifiers with
86
86
  * modifier aliases canonicalized (alt→option, ctrl→control, cmd→command). */
87
+ /** Canonical key name aliases — so blocklist entries match all spellings. */
88
+ const KEY_CANONICAL = {
89
+ escape: "esc",
90
+ delete: "del",
91
+ backspace: "del",
92
+ return: "enter",
93
+ option: "alt",
94
+ };
87
95
  function normalizeShortcut(raw) {
88
96
  return raw
89
97
  .toLowerCase()
90
98
  .split("+")
91
99
  .map((s) => {
92
100
  const t = s.trim();
93
- return MODIFIER_CANONICAL[t] ?? t;
101
+ return MODIFIER_CANONICAL[t] ?? KEY_CANONICAL[t] ?? t;
94
102
  })
95
103
  .sort()
96
104
  .join("+");
@@ -111,7 +119,6 @@ export const OBSERVE_ACTIONS = new Set([
111
119
  "wait",
112
120
  "wait_for_element",
113
121
  "doctor",
114
- "clipboard_read",
115
122
  // focus_app only sets the active target context via AppleScript activate
116
123
  // and an AX window lookup — it does not synthesize mouse or keyboard input,
117
124
  // so the user-activity pause must not block it. (OpenCode 0.3.7 follow-up)
@@ -119,7 +126,7 @@ export const OBSERVE_ACTIONS = new Set([
119
126
  // describe_screen reads screen state (OCR + AX), no input synthesis.
120
127
  "describe_screen",
121
128
  ]);
122
- /** Actions that synthesize user input — need full user-activity protection. */
129
+ /** Actions that synthesize user input OR access sensitive data — need full user-activity protection. */
123
130
  export const INPUT_ACTIONS = new Set([
124
131
  "click",
125
132
  "double_click",
@@ -132,6 +139,9 @@ export const INPUT_ACTIONS = new Set([
132
139
  "type_in_element",
133
140
  "set_value",
134
141
  "click_menu_bar_extra",
142
+ // clipboard_read accesses potentially sensitive data (passwords/TOTP copied
143
+ // by password managers) — treat as input so the user-activity pause applies.
144
+ "clipboard_read",
135
145
  "clipboard_write",
136
146
  ]);
137
147
  export function classifyAction(action) {
@@ -65,7 +65,7 @@ function isSkylightAvailable() {
65
65
  encoding: "utf8",
66
66
  timeout: 3000,
67
67
  });
68
- _skylightAvailable = stdout.includes('"ok"');
68
+ _skylightAvailable = /"skylight"\s*:\s*true/.test(stdout);
69
69
  }
70
70
  catch {
71
71
  _skylightAvailable = false;
@@ -103,17 +103,28 @@ function runInputChecked(payload, target) {
103
103
  // skylight crashed/timed out → fall through to cgevent.
104
104
  }
105
105
  }
106
- // cgevent path (HID-tap) — the reliable fallback.
107
- const raw = execFileSync(resolvedCgeventPath, [], {
108
- input: JSON.stringify(payload),
109
- encoding: "utf8",
110
- timeout: 10000,
111
- }).trim();
112
- const resp = JSON.parse(raw);
113
- if (resp.error) {
114
- throw new Error(`native helper error: ${resp.error}`);
115
- }
116
- return "hid-tap";
106
+ // cgevent path (HID-tap) — the reliable fallback. Guard against ENOENT
107
+ // (cgevent-helper missing) when only skylight was available.
108
+ if (!isCgeventAvailable()) {
109
+ throw new Error("input dispatch failed: no native helper available (both cgevent and skylight unavailable or errored)");
110
+ }
111
+ try {
112
+ const raw = execFileSync(resolvedCgeventPath, [], {
113
+ input: JSON.stringify(payload),
114
+ encoding: "utf8",
115
+ timeout: 10000,
116
+ }).trim();
117
+ const resp = JSON.parse(raw);
118
+ if (resp.error) {
119
+ throw new Error(`native helper error: ${resp.error}`);
120
+ }
121
+ return "hid-tap";
122
+ }
123
+ catch (e) {
124
+ if (e instanceof Error && e.message.startsWith("native helper error:"))
125
+ throw e;
126
+ throw new Error(`input dispatch failed: cgevent helper unavailable (${e.message})`);
127
+ }
117
128
  }
118
129
  /** @deprecated use runInputChecked — kept for external callers/tests. */
119
130
  function runNativeChecked(payload) {
@@ -167,7 +167,7 @@ func focusWithoutRaise(pid: Int32, windowID: Int) {
167
167
  windowID > 0 else { return }
168
168
  var prevPSN = [UInt32](repeating: 0, count: 2)
169
169
  var targetPSN = [UInt32](repeating: 0, count: 2)
170
- let prevOk = prevPSN.withUnsafeMutableBytes { raw in frontFn(raw.baseAddress!) != 0 }
170
+ let prevOk = prevPSN.withUnsafeMutableBytes { raw in frontFn(raw.baseAddress!) == 0 }
171
171
  if prevOk {
172
172
  let cid = connFn()
173
173
  var ownerCid: UInt32 = 0
@@ -342,16 +342,12 @@ func doDrag(_ p: Input) -> String {
342
342
  }
343
343
 
344
344
  func doScroll(_ p: Input) -> String {
345
- // Scroll events use CGEventCreateScrollWheelEvent; per-pid scroll posting is
346
- // unreliable across app types, so we route via HID-tap but still report method.
345
+ // Scroll events via SLEventPostToPid lack field-40/window-location stamping
346
+ // (postPerPid is mouse-click-specific), so per-pid scroll is unreliable.
347
+ // Honestly report hid-tap so the caller's warning path fires correctly.
347
348
  let dy = Int32(-(p.deltaY ?? 0)); let dx = Int32(p.deltaX ?? 0)
348
349
  guard let ev = CGEvent(scrollWheelEvent2Source: nil, units: .pixel, wheelCount: 2, wheel1: dy, wheel2: dx, wheel3: 0)
349
350
  else { return "{\"error\":\"fail\"}" }
350
- // Best-effort per-pid if we have a pid; scroll doesn't move the cursor noticeably anyway.
351
- if let pid = p.pid, pid > 0, slPostToPid != nil, !isFrontmost(pid) {
352
- slPostToPid?(pid, ev)
353
- return "{\"ok\":true,\"method\":\"per-pid\"}"
354
- }
355
351
  postHidTap(ev)
356
352
  return "{\"ok\":true,\"method\":\"hid-tap\"}"
357
353
  }
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ucu-mcp",
3
- "version": "0.6.3",
3
+ "version": "0.6.4",
4
4
  "description": "MCP server for Universal Computer Use — desktop automation for AI agents via Model Context Protocol",
5
5
  "type": "module",
6
6
  "bin": {