Skip to content

Commit 3bf2f88

Browse files
committed
fix(codex): keep permission grants human-gated
1 parent 4281c0b commit 3bf2f88

2 files changed

Lines changed: 71 additions & 17 deletions

File tree

‎extensions/codex/src/app-server/approval-bridge.test.ts‎

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,51 @@ describe("Codex app-server approval bridge", () => {
232232
},
233233
);
234234

235+
it.each([
236+
["promoted tool policy", { autoApproveOpenClawToolPolicy: true }],
237+
["full-auto runtime policy", { autoApprove: true }],
238+
] as const)("keeps permission grants on the human path under %s", async (_label, policy) => {
239+
const params = createParams();
240+
mockCallGatewayTool
241+
.mockResolvedValueOnce({ id: "plugin:permission-policy-allow", status: "accepted" })
242+
.mockResolvedValueOnce({
243+
id: "plugin:permission-policy-allow",
244+
decision: "allow-once",
245+
});
246+
247+
const result = await handleCodexAppServerApprovalRequest({
248+
method: "item/permissions/requestApproval",
249+
requestParams: {
250+
threadId: "thread-1",
251+
turnId: "turn-1",
252+
itemId: "permission-policy-allow",
253+
permissions: {
254+
network: { enabled: true },
255+
fileSystem: { write: ["/workspace"] },
256+
},
257+
},
258+
paramsForRun: params,
259+
threadId: "thread-1",
260+
turnId: "turn-1",
261+
...policy,
262+
});
263+
264+
expect(result).toEqual({
265+
permissions: {
266+
network: { enabled: true },
267+
fileSystem: { write: ["/workspace"] },
268+
},
269+
scope: "turn",
270+
});
271+
expect(mockRunBeforeToolCallHook).toHaveBeenCalledWith(
272+
expect.objectContaining({ toolName: "codex_permission_approval" }),
273+
);
274+
expect(mockCallGatewayTool.mock.calls.map(([method]) => method)).toEqual([
275+
"plugin.approval.request",
276+
"plugin.approval.waitDecision",
277+
]);
278+
});
279+
235280
it("routes command approvals through plugin approvals and accepts allowed commands", async () => {
236281
const params = createParams();
237282
mockCallGatewayTool
@@ -2465,8 +2510,11 @@ describe("Codex app-server approval bridge", () => {
24652510
});
24662511
});
24672512

2468-
it("fails closed for unsupported native approval methods without requesting plugin approval", async () => {
2513+
it("routes unknown approval methods to the human path and still fails closed", async () => {
24692514
const params = createParams();
2515+
mockCallGatewayTool
2516+
.mockResolvedValueOnce({ id: "plugin:future-approval", status: "accepted" })
2517+
.mockResolvedValueOnce({ id: "plugin:future-approval", decision: "deny" });
24702518

24712519
const result = await handleCodexAppServerApprovalRequest({
24722520
method: "future/requestApproval",
@@ -2484,8 +2532,15 @@ describe("Codex app-server approval bridge", () => {
24842532
decision: "decline",
24852533
reason: "OpenClaw codex app-server bridge does not grant native approvals yet.",
24862534
});
2487-
expect(mockCallGatewayTool).not.toHaveBeenCalled();
2488-
expect(params.onAgentEvent).not.toHaveBeenCalled();
2535+
expect(mockRunBeforeToolCallHook).not.toHaveBeenCalled();
2536+
expect(mockCallGatewayTool.mock.calls.map(([method]) => method)).toEqual([
2537+
"plugin.approval.request",
2538+
"plugin.approval.waitDecision",
2539+
]);
2540+
findApprovalEvent(params, {
2541+
status: "denied",
2542+
approvalId: "plugin:future-approval",
2543+
});
24892544
});
24902545
it("labels permission approvals explicitly with permission detail", async () => {
24912546
const params = createParams();

‎extensions/codex/src/app-server/approval-bridge.ts‎

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ const PERMISSION_VALUE_MAX_LENGTH = 48;
3434
const COMMAND_PREVIEW_WITH_DETAILS_MAX_LENGTH = 80;
3535
const APPROVAL_PREVIEW_SCAN_MAX_LENGTH = 4096;
3636
const APPROVAL_PREVIEW_OMITTED = "[preview truncated or unsafe content omitted]";
37+
// Automatic approval is limited to concrete calls. A before_tool_call allow
38+
// covers the evaluated call, not future scope; new or grant-shaped methods stay human-gated.
39+
const CONCRETE_TOOL_AUTO_APPROVAL_METHODS = new Set([
40+
"item/commandExecution/requestApproval",
41+
"item/fileChange/requestApproval",
42+
]);
3743
const ANSI_OSC_SEQUENCE_RE = new RegExp(
3844
String.raw`(?:\u001b]|\u009d)[^\u001b\u009c\u0007]*(?:\u0007|\u001b\\|\u009c)`,
3945
"g",
@@ -87,10 +93,6 @@ export async function handleCodexAppServerApprovalRequest(params: {
8793
if (!matchesCurrentTurn(requestParams, params.threadId, params.turnId)) {
8894
return undefined;
8995
}
90-
if (!isSupportedAppServerApprovalMethod(params.method)) {
91-
return unsupportedApprovalResponse();
92-
}
93-
9496
const context = buildApprovalContext({
9597
method: params.method,
9698
requestParams,
@@ -135,7 +137,12 @@ export async function handleCodexAppServerApprovalRequest(params: {
135137
});
136138
return buildApprovalResponse(params.method, context.requestParams, policyOutcome.outcome);
137139
}
138-
if (params.autoApproveOpenClawToolPolicy === true && policyOutcome?.outcome === "allowed") {
140+
const canAutoApproveConcreteToolCall = CONCRETE_TOOL_AUTO_APPROVAL_METHODS.has(params.method);
141+
if (
142+
canAutoApproveConcreteToolCall &&
143+
params.autoApproveOpenClawToolPolicy === true &&
144+
policyOutcome?.outcome === "allowed"
145+
) {
139146
emitApprovalEvent(params.paramsForRun, {
140147
phase: "resolved",
141148
kind: context.kind,
@@ -147,7 +154,7 @@ export async function handleCodexAppServerApprovalRequest(params: {
147154
});
148155
return buildApprovalResponse(params.method, context.requestParams, "approved-once");
149156
}
150-
if (params.autoApprove === true) {
157+
if (canAutoApproveConcreteToolCall && params.autoApprove === true) {
151158
emitApprovalEvent(params.paramsForRun, {
152159
phase: "resolved",
153160
kind: context.kind,
@@ -1256,14 +1263,6 @@ function approvalKindForMethod(method: string): AgentApprovalEventData["kind"] {
12561263
return "unknown";
12571264
}
12581265

1259-
function isSupportedAppServerApprovalMethod(method: string): boolean {
1260-
return (
1261-
method === "item/commandExecution/requestApproval" ||
1262-
method === "item/fileChange/requestApproval" ||
1263-
method === "item/permissions/requestApproval"
1264-
);
1265-
}
1266-
12671266
function emitApprovalEvent(params: EmbeddedRunAttemptParams, data: AgentApprovalEventData): void {
12681267
void params.onAgentEvent?.({
12691268
stream: "approval",

0 commit comments

Comments
 (0)