Skip to content

Commit 2ab65d1

Browse files
committed
fix(cursor): route denied native tools through advertised Codex exec
Cursor-native read/shell/fetch stay denied by default, but the refusal used to name a top-level exec_command/shell_command bridge even on Desktop code-mode turns that only advertise exec. Teach the denial path and tool guidance to retry through nested await tools.exec_command(...) when that is the catalog this turn actually exposed.
1 parent 3d67ac2 commit 2ab65d1

10 files changed

Lines changed: 190 additions & 51 deletions

File tree

‎docs-site/src/content/docs/guides/providers.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ and `fast=true` `requested_model` parameters; flattened `cursor-grok-{version}-{
519519
are discovery and picker identities only. Cursor serves Kimi K3 only as effort-suffixed wire ids, so
520520
`cursor/kimi-k3` exposes a `low` / `high` / `max` ladder and defaults to `max`, matching the
521521
model's documented API default. Cursor server-driven native read/write/delete/ls/grep/shell/fetch execution
522-
is disabled by default because it bypasses Codex's approval and sandbox path; set
522+
is disabled by default because it bypasses Codex's approval and sandbox path. Denied native calls are rerouted to the Codex tools advertised on that turn — a flat `exec_command` / `shell_command` bridge, or nested `await tools.exec_command(...)` helpers inside Desktop code-mode `exec`. Set
523523
`unsafeAllowNativeLocalExec: true` on the `providers.cursor` object in `~/.opencodex/config.json`
524524
only for trusted local experiments (or via **Providers → Cursor → Edit JSON** in the dashboard).
525525
See the [Configuration reference](/reference/configuration/#cursor-provider-adapter-cursor)

‎docs-site/src/content/docs/reference/configuration/providers.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ Explicit variants send Cursor's `default` model with its `optimization` paramete
286286
selection on every request. They remain available when live discovery omits `default`.
287287

288288
Cursor server-driven local tools are disabled by default. Codex continues using its own tools such as
289-
`apply_patch` and `exec_command` with its own approval and sandbox policy:
289+
`apply_patch`, `exec_command`, and Desktop code-mode `exec` (nested `await tools.exec_command(...)` helpers) with its own approval and sandbox policy:
290290

291291
- `"off"` (default) rejects Cursor-native `read`, `write`, `delete`, `ls`, `grep`, `shell`, and
292292
`fetch` execution.
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import {
2+
CODEX_APPLY_PATCH_TOOL,
3+
CODEX_EXEC_COMMAND_TOOL,
4+
CODEX_SHELL_COMMAND_TOOL,
5+
CODEX_UNIFIED_EXEC_TOOL,
6+
isCodexShellBridgeToolName,
7+
OCX_RESPONSES_TOOL_PROVIDER,
8+
} from "./tool-definitions";
9+
10+
export interface CursorNativeExecBridgeCatalog {
11+
clientToolNames?: readonly string[];
12+
}
13+
14+
const CODE_MODE_SHELL_HINT =
15+
`the Codex code-mode tool \`${CODEX_UNIFIED_EXEC_TOOL}\` and call a nested helper INSIDE its JavaScript body, for example \`await tools.${CODEX_EXEC_COMMAND_TOOL}({cmd: "pwd"})\`. Do not invent a top-level \`${CODEX_SHELL_COMMAND_TOOL}\` / \`${CODEX_EXEC_COMMAND_TOOL}\` call`;
16+
17+
const FLAT_SHELL_HINT =
18+
`the Codex bridge shell tool from the current catalog (\`${CODEX_SHELL_COMMAND_TOOL}\` or \`${CODEX_EXEC_COMMAND_TOOL}\`, including the long \`mcp_${OCX_RESPONSES_TOOL_PROVIDER}_*\` display name if listed)`;
19+
20+
function advertisedNames(catalog?: CursorNativeExecBridgeCatalog): string[] {
21+
return (catalog?.clientToolNames ?? [])
22+
.map(name => name.trim())
23+
.filter(name => name.length > 0);
24+
}
25+
26+
function hasAdvertisedName(names: readonly string[], expected: string): boolean {
27+
return names.some(name => name === expected || name.endsWith(`_${expected}`) || name.endsWith(`__${expected}`));
28+
}
29+
30+
/** True when this turn advertised freeform/code-mode exec and no flat shell bridge. */
31+
export function cursorNativeExecUsesCodeModeBridge(catalog?: CursorNativeExecBridgeCatalog): boolean {
32+
const names = advertisedNames(catalog);
33+
if (names.length === 0) return false;
34+
const hasExec = hasAdvertisedName(names, CODEX_UNIFIED_EXEC_TOOL);
35+
const hasFlatShell = names.some(name =>
36+
isCodexShellBridgeToolName(name)
37+
|| name.endsWith(`_${CODEX_EXEC_COMMAND_TOOL}`)
38+
|| name.endsWith(`_${CODEX_SHELL_COMMAND_TOOL}`)
39+
|| name.endsWith(`__${CODEX_EXEC_COMMAND_TOOL}`)
40+
|| name.endsWith(`__${CODEX_SHELL_COMMAND_TOOL}`),
41+
);
42+
return hasExec && !hasFlatShell;
43+
}
44+
45+
function bridgeTarget(catalog?: CursorNativeExecBridgeCatalog): string {
46+
return cursorNativeExecUsesCodeModeBridge(catalog) ? CODE_MODE_SHELL_HINT : FLAT_SHELL_HINT;
47+
}
48+
49+
function silenceClause(kind: "shell" | "filesystem" | "network"): string {
50+
if (kind === "filesystem") {
51+
return "Do not tell the user shell or read access is blocked, disabled, or denied — silently call that bridge tool.";
52+
}
53+
if (kind === "network") {
54+
return "Do not tell the user network or shell access is blocked, disabled, or denied — silently call that bridge tool.";
55+
}
56+
return "Do not tell the user shell access is blocked, disabled, or denied — silently call that bridge tool.";
57+
}
58+
59+
export function nativeShellDisabledMessage(catalog?: CursorNativeExecBridgeCatalog): string {
60+
// Do not insist on "the same command" — that steers models into replaying bash/CMD
61+
// idioms through the Codex bridge on Windows PowerShell 5.1 and looping (#604).
62+
// Keep this host-shell-neutral: OpenCodex may run on a different OS than the Codex
63+
// client that executes the bridge (LAN/SSH remote-proxy).
64+
return (
65+
`Route this through ${bridgeTarget(catalog)}. `
66+
+ `${silenceClause("shell")} `
67+
+ "Adapt the command for the Codex client host shell before calling the bridge "
68+
+ "(Windows PowerShell 5.1: no CMD `cd /d`, no bash heredocs; `&&`/`||` are unsupported parser errors — prefer the bridge working-directory argument for directory changes, and use `if ($?) { ... }` for success-gated follow-up steps; do not treat `;` as a substitute for `&&`). "
69+
+ "Make at most one corrected bridge attempt after a failure, then report the error and stop — do not repeat equivalent failing commands."
70+
);
71+
}
72+
73+
export function nativeFilesystemDisabledMessage(catalog?: CursorNativeExecBridgeCatalog): string {
74+
const editHint = cursorNativeExecUsesCodeModeBridge(catalog)
75+
? `or a nested \`await tools.${CODEX_APPLY_PATCH_TOOL}(...)\` helper inside \`${CODEX_UNIFIED_EXEC_TOOL}\` for file edits`
76+
: "or `apply_patch` for file edits";
77+
return (
78+
`Route filesystem work through ${bridgeTarget(catalog)} with equivalent shell commands (cat, head, ls, rg, grep), ${editHint}. `
79+
+ silenceClause("filesystem")
80+
);
81+
}
82+
83+
export function nativeFetchDisabledMessage(catalog?: CursorNativeExecBridgeCatalog): string {
84+
if (cursorNativeExecUsesCodeModeBridge(catalog)) {
85+
return (
86+
`Route this through ${CODE_MODE_SHELL_HINT} with a nested \`await tools.${CODEX_EXEC_COMMAND_TOOL}({cmd: "curl ..."})\` helper. `
87+
+ silenceClause("network")
88+
);
89+
}
90+
return (
91+
"Route this through the Codex shell bridge tool `shell_command` (aliases: `exec_command`, `mcp_opencodex-responses_shell_command`, `mcp_opencodex-responses_exec_command`) with curl or wget. "
92+
+ silenceClause("network")
93+
);
94+
}

‎src/adapters/cursor/native-exec-fs.ts‎

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import {
3434
type GrepArgs,
3535
} from "./gen/agent_pb";
3636
import { errorText, execBytes, lineCount, textDecoder, textEncoder } from "./native-exec-common";
37+
import { nativeFilesystemDisabledMessage, type CursorNativeExecBridgeCatalog } from "./native-exec-bridge";
3738

3839
const MAX_GREP_FILES = 500;
3940
const MAX_GREP_RESULTS = 200;
@@ -46,14 +47,15 @@ function codexNativeMutationRefusal(operation: "write" | "delete", structuredEdi
4647
return `Cursor-native ${operation} is disabled for this Codex request because apply_patch is available.${structuredHint} No file was changed.`;
4748
}
4849

49-
const NATIVE_LOCAL_EXEC_DISABLED =
50-
"Route filesystem work through the Codex Responses bridge shell tool (`shell_command` / `exec_command`, or the listed `mcp_opencodex-responses_*` display alias) with equivalent shell commands (cat, head, ls, rg, grep), or `apply_patch` for file edits. Do not tell the user shell or read access is blocked, disabled, or denied — silently call that bridge tool.";
50+
function nativeLocalExecDisabled(catalog?: CursorNativeExecBridgeCatalog): string {
51+
return nativeFilesystemDisabledMessage(catalog);
52+
}
5153

52-
export function rejectReadExecForPolicy(execMsg: ExecServerMessage): Uint8Array {
54+
export function rejectReadExecForPolicy(execMsg: ExecServerMessage, catalog?: CursorNativeExecBridgeCatalog): Uint8Array {
5355
if (execMsg.message.case !== "readArgs") throw new Error("invalid read exec");
5456
const path = resolve(execMsg.message.value.path);
5557
return execBytes(execMsg, "readResult", create(ReadResultSchema, {
56-
result: { case: "error", value: create(ReadErrorSchema, { path, error: NATIVE_LOCAL_EXEC_DISABLED }) },
58+
result: { case: "error", value: create(ReadErrorSchema, { path, error: nativeLocalExecDisabled(catalog) }) },
5759
}));
5860
}
5961

@@ -98,13 +100,13 @@ export function rejectWriteExecForApplyPatch(execMsg: ExecServerMessage, structu
98100
}));
99101
}
100102

101-
export function rejectWriteExecForPolicy(execMsg: ExecServerMessage): Uint8Array {
103+
export function rejectWriteExecForPolicy(execMsg: ExecServerMessage, catalog?: CursorNativeExecBridgeCatalog): Uint8Array {
102104
if (execMsg.message.case !== "writeArgs") throw new Error("invalid write exec");
103105
const path = resolve(execMsg.message.value.path);
104106
return execBytes(execMsg, "writeResult", create(WriteResultSchema, {
105107
result: {
106108
case: "rejected",
107-
value: create(WriteRejectedSchema, { path, reason: `${NATIVE_LOCAL_EXEC_DISABLED} No file was changed.` }),
109+
value: create(WriteRejectedSchema, { path, reason: `${nativeLocalExecDisabled(catalog)} No file was changed.` }),
108110
},
109111
}));
110112
}
@@ -147,13 +149,13 @@ export function rejectDeleteExecForApplyPatch(execMsg: ExecServerMessage, struct
147149
}));
148150
}
149151

150-
export function rejectDeleteExecForPolicy(execMsg: ExecServerMessage): Uint8Array {
152+
export function rejectDeleteExecForPolicy(execMsg: ExecServerMessage, catalog?: CursorNativeExecBridgeCatalog): Uint8Array {
151153
if (execMsg.message.case !== "deleteArgs") throw new Error("invalid delete exec");
152154
const path = resolve(execMsg.message.value.path);
153155
return execBytes(execMsg, "deleteResult", create(DeleteResultSchema, {
154156
result: {
155157
case: "rejected",
156-
value: create(DeleteRejectedSchema, { path, reason: `${NATIVE_LOCAL_EXEC_DISABLED} No file was changed.` }),
158+
value: create(DeleteRejectedSchema, { path, reason: `${nativeLocalExecDisabled(catalog)} No file was changed.` }),
157159
},
158160
}));
159161
}
@@ -188,11 +190,11 @@ export function deleteExec(execMsg: ExecServerMessage): Uint8Array {
188190
}
189191
}
190192

191-
export function rejectLsExecForPolicy(execMsg: ExecServerMessage): Uint8Array {
193+
export function rejectLsExecForPolicy(execMsg: ExecServerMessage, catalog?: CursorNativeExecBridgeCatalog): Uint8Array {
192194
if (execMsg.message.case !== "lsArgs") throw new Error("invalid ls exec");
193195
const path = resolve(execMsg.message.value.path);
194196
return execBytes(execMsg, "lsResult", create(LsResultSchema, {
195-
result: { case: "error", value: create(LsErrorSchema, { path, error: NATIVE_LOCAL_EXEC_DISABLED }) },
197+
result: { case: "error", value: create(LsErrorSchema, { path, error: nativeLocalExecDisabled(catalog) }) },
196198
}));
197199
}
198200

@@ -256,8 +258,8 @@ function grepError(execMsg: ExecServerMessage, error: string): Uint8Array {
256258
}));
257259
}
258260

259-
export function rejectGrepExecForPolicy(execMsg: ExecServerMessage): Uint8Array {
260-
return grepError(execMsg, NATIVE_LOCAL_EXEC_DISABLED);
261+
export function rejectGrepExecForPolicy(execMsg: ExecServerMessage, catalog?: CursorNativeExecBridgeCatalog): Uint8Array {
262+
return grepError(execMsg, nativeLocalExecDisabled(catalog));
261263
}
262264

263265
export function grepExec(execMsg: ExecServerMessage): Uint8Array {

‎src/adapters/cursor/native-exec-network.ts‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
import { create } from "@bufbuild/protobuf";
22
import { FetchErrorSchema, FetchResultSchema, FetchSuccessSchema, type ExecServerMessage } from "./gen/agent_pb";
33
import { errorText, execBytes } from "./native-exec-common";
4+
import { nativeFetchDisabledMessage, type CursorNativeExecBridgeCatalog } from "./native-exec-bridge";
45

56
export interface CursorNativeNetworkDeps {
67
fetch?: typeof fetch;
78
}
89

9-
const NATIVE_FETCH_DISABLED =
10-
"Route this through the Codex shell bridge tool `shell_command` (aliases: `exec_command`, `mcp_opencodex-responses_shell_command`, `mcp_opencodex-responses_exec_command`) with curl or wget. Do not tell the user network or shell access is blocked, disabled, or denied — silently call that bridge tool.";
10+
function nativeFetchDisabled(catalog?: CursorNativeExecBridgeCatalog): string {
11+
return nativeFetchDisabledMessage(catalog);
12+
}
1113

12-
export function rejectFetchExecForPolicy(execMsg: ExecServerMessage): Uint8Array {
14+
export function rejectFetchExecForPolicy(execMsg: ExecServerMessage, catalog?: CursorNativeExecBridgeCatalog): Uint8Array {
1315
if (execMsg.message.case !== "fetchArgs") throw new Error("invalid fetch exec");
1416
const args = execMsg.message.value;
1517
return execBytes(execMsg, "fetchResult", create(FetchResultSchema, {
16-
result: { case: "error", value: create(FetchErrorSchema, { url: args.url, error: NATIVE_FETCH_DISABLED }) },
18+
result: { case: "error", value: create(FetchErrorSchema, { url: args.url, error: nativeFetchDisabled(catalog) }) },
1719
}));
1820
}
1921

‎src/adapters/cursor/native-exec-shell.ts‎

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
type ExecServerMessage,
2020
} from "./gen/agent_pb";
2121
import { errorText, execBytes, execStreamCloseBytes } from "./native-exec-common";
22+
import { nativeShellDisabledMessage as catalogNativeShellDisabledMessage, type CursorNativeExecBridgeCatalog } from "./native-exec-bridge";
2223
import {
2324
createAdmissionGate,
2425
type AdmissionLease,
@@ -82,21 +83,11 @@ let unresolvedKills = 0;
8283
let killFailures = 0;
8384

8485
/** Rejection text when Cursor-native shell is denied by policy (issue #604). */
85-
export function nativeShellDisabledMessage(): string {
86-
// Do not insist on "the same command" — that steers models into replaying bash/CMD
87-
// idioms through the Codex bridge on Windows PowerShell 5.1 and looping (#604).
88-
// Keep this host-shell-neutral: OpenCodex may run on a different OS than the Codex
89-
// client that executes the bridge (LAN/SSH remote-proxy).
90-
return (
91-
"Route this through the Codex bridge shell tool from the current catalog (`shell_command` or `exec_command`, including the long `mcp_opencodex-responses_*` display name if listed). "
92-
+ "Do not tell the user shell access is blocked, disabled, or denied — silently call that bridge tool. "
93-
+ "Adapt the command for the Codex client host shell before calling the bridge "
94-
+ "(Windows PowerShell 5.1: no CMD `cd /d`, no bash heredocs; `&&`/`||` are unsupported parser errors — prefer the bridge working-directory argument for directory changes, and use `if ($?) { ... }` for success-gated follow-up steps; do not treat `;` as a substitute for `&&`). "
95-
+ "Make at most one corrected bridge attempt after a failure, then report the error and stop — do not repeat equivalent failing commands."
96-
);
86+
export function nativeShellDisabledMessage(catalog?: CursorNativeExecBridgeCatalog): string {
87+
return catalogNativeShellDisabledMessage(catalog);
9788
}
9889

99-
function rejectedShellResult(command: string, cwd: string, started: number) {
90+
function rejectedShellResult(command: string, cwd: string, started: number, catalog?: CursorNativeExecBridgeCatalog) {
10091
return create(ShellResultSchema, {
10192
result: {
10293
case: "failure",
@@ -106,18 +97,18 @@ function rejectedShellResult(command: string, cwd: string, started: number) {
10697
exitCode: 1,
10798
signal: "",
10899
stdout: "",
109-
stderr: nativeShellDisabledMessage(),
100+
stderr: nativeShellDisabledMessage(catalog),
110101
executionTime: Date.now() - started,
111102
aborted: true,
112103
}),
113104
},
114105
});
115106
}
116107

117-
export function rejectShellExecForPolicy(execMsg: ExecServerMessage): Uint8Array {
108+
export function rejectShellExecForPolicy(execMsg: ExecServerMessage, catalog?: CursorNativeExecBridgeCatalog): Uint8Array {
118109
if (execMsg.message.case !== "shellArgs") throw new Error("invalid shell exec");
119110
const args = execMsg.message.value;
120-
return execBytes(execMsg, "shellResult", rejectedShellResult(args.command, resolve(args.workingDirectory || process.cwd()), Date.now()));
111+
return execBytes(execMsg, "shellResult", rejectedShellResult(args.command, resolve(args.workingDirectory || process.cwd()), Date.now(), catalog));
121112
}
122113

123114
export function shellExec(execMsg: ExecServerMessage): Uint8Array {
@@ -155,7 +146,7 @@ export function shellExec(execMsg: ExecServerMessage): Uint8Array {
155146
}));
156147
}
157148

158-
export function rejectShellStreamExecForPolicy(execMsg: ExecServerMessage): Uint8Array[] {
149+
export function rejectShellStreamExecForPolicy(execMsg: ExecServerMessage, catalog?: CursorNativeExecBridgeCatalog): Uint8Array[] {
159150
if (execMsg.message.case !== "shellStreamArgs") throw new Error("invalid shell stream exec");
160151
const args = execMsg.message.value;
161152
const cwd = resolve(args.workingDirectory || process.cwd());
@@ -165,12 +156,12 @@ export function rejectShellStreamExecForPolicy(execMsg: ExecServerMessage): Uint
165156
event: { case: "start", value: create(ShellStreamStartSchema, { sandboxPolicy: args.requestedSandboxPolicy }) },
166157
})),
167158
execBytes(execMsg, "shellStream", create(ShellStreamSchema, {
168-
event: { case: "stderr", value: create(ShellStreamStderrSchema, { data: nativeShellDisabledMessage() }) },
159+
event: { case: "stderr", value: create(ShellStreamStderrSchema, { data: nativeShellDisabledMessage(catalog) }) },
169160
})),
170161
execBytes(execMsg, "shellStream", create(ShellStreamSchema, {
171162
event: { case: "exit", value: create(ShellStreamExitSchema, { code: 1, cwd, aborted: true }) },
172163
})),
173-
execBytes(execMsg, "shellResult", rejectedShellResult(args.command, cwd, started)),
164+
execBytes(execMsg, "shellResult", rejectedShellResult(args.command, cwd, started, catalog)),
174165
execStreamCloseBytes(execMsg),
175166
];
176167
}
@@ -261,12 +252,12 @@ export async function shellStreamExec(execMsg: ExecServerMessage): Promise<Uint8
261252
return replies;
262253
}
263254

264-
export function rejectBackgroundShellSpawnExecForPolicy(execMsg: ExecServerMessage): Uint8Array {
255+
export function rejectBackgroundShellSpawnExecForPolicy(execMsg: ExecServerMessage, catalog?: CursorNativeExecBridgeCatalog): Uint8Array {
265256
if (execMsg.message.case !== "backgroundShellSpawnArgs") throw new Error("invalid background shell exec");
266257
const args = execMsg.message.value;
267258
const cwd = resolve(args.workingDirectory || process.cwd());
268259
return execBytes(execMsg, "backgroundShellSpawnResult", create(BackgroundShellSpawnResultSchema, {
269-
result: { case: "error", value: create(BackgroundShellSpawnErrorSchema, { command: args.command, workingDirectory: cwd, error: nativeShellDisabledMessage() }) },
260+
result: { case: "error", value: create(BackgroundShellSpawnErrorSchema, { command: args.command, workingDirectory: cwd, error: nativeShellDisabledMessage(catalog) }) },
270261
}));
271262
}
272263

@@ -518,10 +509,10 @@ export function backgroundShellSpawnExec(execMsg: ExecServerMessage, sessionId:
518509
}
519510
}
520511

521-
export function rejectWriteShellStdinExecForPolicy(execMsg: ExecServerMessage): Uint8Array {
512+
export function rejectWriteShellStdinExecForPolicy(execMsg: ExecServerMessage, catalog?: CursorNativeExecBridgeCatalog): Uint8Array {
522513
if (execMsg.message.case !== "writeShellStdinArgs") throw new Error("invalid shell stdin exec");
523514
return execBytes(execMsg, "writeShellStdinResult", create(WriteShellStdinResultSchema, {
524-
result: { case: "error", value: create(WriteShellStdinErrorSchema, { error: nativeShellDisabledMessage() }) },
515+
result: { case: "error", value: create(WriteShellStdinErrorSchema, { error: nativeShellDisabledMessage(catalog) }) },
525516
}));
526517
}
527518

0 commit comments

Comments
 (0)