Skip to content

Commit c4f2ec3

Browse files
committed
fix(desktop): snapshot window lookup survives endpoint security agents
On Macs running endpoint security agents (e.g. SentinelOne, Kandji ESF), snapshot "Test capture" failed with "Finding the front window failed" even with Screen Recording and Accessibility granted: the agent SIGKILLs osascript when the ObjC-bridge JXA lookup script is passed inline via `-e`. Run osascript with `-l JavaScript -` and write the script to its stdin, which the same agents allow. The modifier-pair poller is unaffected and keeps using `-e`.
1 parent e57c093 commit c4f2ec3

2 files changed

Lines changed: 48 additions & 2 deletions

File tree

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { assert, it, vi } from "vite-plus/test";
2+
3+
const { execFileMock } = vi.hoisted(() => ({ execFileMock: vi.fn() }));
4+
5+
vi.mock("node:child_process", () => ({ execFile: execFileMock }));
6+
vi.mock("../electron/WindowsForeground.ts", () => ({ loadWindowsForegroundApi: vi.fn() }));
7+
8+
import { activeWindow } from "./ActiveWindow.ts";
9+
10+
it("feeds the macOS lookup script to osascript over stdin instead of inline", async () => {
11+
const lookup = {
12+
id: 42,
13+
title: "main.ts",
14+
bounds: { x: 10, y: 20, width: 800, height: 600 },
15+
owner: {
16+
name: "Editor",
17+
processId: 123,
18+
path: "/Applications/Editor.app",
19+
bundleId: "com.example.editor",
20+
},
21+
};
22+
const stdin = { on: vi.fn(), end: vi.fn() };
23+
execFileMock.mockImplementation(
24+
(
25+
file: string,
26+
args: ReadonlyArray<string>,
27+
_options: unknown,
28+
callback: (error: Error | null, stdout: string) => void,
29+
) => {
30+
assert.strictEqual(file, "/usr/bin/osascript");
31+
assert.deepEqual(args, ["-l", "JavaScript", "-"]);
32+
stdin.end.mockImplementation(() => callback(null, JSON.stringify(lookup) + "\n"));
33+
return { stdin };
34+
},
35+
);
36+
37+
assert.deepEqual(await activeWindow("darwin"), { platform: "macos", ...lookup });
38+
39+
assert.lengthOf(stdin.end.mock.calls, 1);
40+
assert.include(stdin.end.mock.calls[0]![0], "CGWindowListCopyWindowInfo");
41+
});

‎apps/desktop/src/snapShot/ActiveWindow.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,20 @@ function run() {
8080

8181
function runMacLookup(): Promise<string> {
8282
return new Promise((resolve, reject) => {
83-
NodeChildProcess.execFile(
83+
// Endpoint security agents can SIGKILL osascript when this ObjC-bridge script arrives
84+
// inline through `-e`; the same script read from stdin (`-`) is allowed to run.
85+
const child = NodeChildProcess.execFile(
8486
"/usr/bin/osascript",
85-
["-l", "JavaScript", "-e", MAC_LOOKUP_SCRIPT],
87+
["-l", "JavaScript", "-"],
8688
{ timeout: MAC_LOOKUP_TIMEOUT_MS, maxBuffer: 64 * 1024 },
8789
(error, stdout) => {
8890
if (error) reject(error);
8991
else resolve(stdout);
9092
},
9193
);
94+
// A write error means osascript already exited, which the callback above reports.
95+
child.stdin?.on("error", () => undefined);
96+
child.stdin?.end(MAC_LOOKUP_SCRIPT);
9297
});
9398
}
9499

0 commit comments

Comments
 (0)