Skip to content

Commit 3d7ad1c

Browse files
committed
fix(security): centralize owner-only tool gating and scope maps
1 parent 9130fd2 commit 3d7ad1c

16 files changed

Lines changed: 369 additions & 248 deletions

‎CHANGELOG.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ Docs: https://docs.openclaw.ai
8080
- Security/Browser: route browser URL navigation through one SSRF-guarded validation path for tab-open/CDP-target/Playwright navigation flows and block private/metadata destinations by default (configurable via `browser.ssrfPolicy`). This ships in the next npm release. Thanks @dorjoos for reporting.
8181
- Security/Exec: for the next npm release, harden safe-bin stdin-only enforcement by blocking output/recursive flags (`sort -o/--output`, grep recursion) and tightening default safe bins to remove `sort`/`grep`, preventing safe-bin allowlist bypass for file writes/recursive reads. Thanks @nedlir for reporting.
8282
- Cron/Webhooks: protect cron webhook POST delivery with SSRF-guarded outbound fetch (`fetchWithSsrFGuard`) to block private/metadata destinations before request dispatch. Thanks @Adam55A-code.
83-
- Security/Gateway/Agents: remove implicit admin scopes from agent tool gateway calls by classifying methods to least-privilege operator scopes, and restrict `cron`/`gateway` tools to owner senders (with explicit runtime owner checks) to prevent non-owner DM privilege escalation. Ships in the next npm release. Thanks @Adam55A-code for reporting.
84-
- Security/Gateway: centralize gateway method-scope authorization and default non-CLI gateway callers to least-privilege method scopes, with explicit CLI scope handling and regression coverage to prevent scope drift.
83+
- Security/Gateway/Agents: remove implicit admin scopes from agent tool gateway calls by classifying methods to least-privilege operator scopes, and enforce owner-only tooling (`cron`, `gateway`, `whatsapp_login`) through centralized tool-policy wrappers plus tool metadata to prevent non-owner DM privilege escalation. Ships in the next npm release. Thanks @Adam55A-code for reporting.
84+
- Security/Gateway: centralize gateway method-scope authorization and default non-CLI gateway callers to least-privilege method scopes, with explicit CLI scope handling, full core-handler scope classification coverage, and regression guards to prevent scope drift.
8585
- Security/Net: block SSRF bypass via NAT64 (`64:ff9b::/96`, `64:ff9b:1::/48`), 6to4 (`2002::/16`), and Teredo (`2001:0000::/32`) IPv6 transition addresses, and fail closed on IPv6 parse errors. Thanks @jackhax.
8686

8787
## 2026.2.17

‎extensions/feishu/src/bot.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ import {
77
DEFAULT_GROUP_HISTORY_LIMIT,
88
type HistoryEntry,
99
} from "openclaw/plugin-sdk";
10-
import type { FeishuMessageContext, FeishuMediaInfo, ResolvedFeishuAccount } from "./types.js";
11-
import type { DynamicAgentCreationConfig } from "./types.js";
1210
import { resolveFeishuAccount } from "./accounts.js";
1311
import { createFeishuClient } from "./client.js";
1412
import { tryRecordMessage } from "./dedup.js";
@@ -30,6 +28,8 @@ import {
3028
import { createFeishuReplyDispatcher } from "./reply-dispatcher.js";
3129
import { getFeishuRuntime } from "./runtime.js";
3230
import { getMessageFeishu, sendMessageFeishu } from "./send.js";
31+
import type { FeishuMessageContext, FeishuMediaInfo, ResolvedFeishuAccount } from "./types.js";
32+
import type { DynamicAgentCreationConfig } from "./types.js";
3333

3434
// --- Permission error extraction ---
3535
// Extract permission grant URL from Feishu API error response.

‎src/agents/openclaw-gateway-tool.e2e.test.ts‎

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,15 @@ vi.mock("./tools/gateway.js", () => ({
1717
}));
1818

1919
describe("gateway tool", () => {
20-
it("rejects non-owner callers explicitly", async () => {
21-
const { callGatewayTool } = await import("./tools/gateway.js");
20+
it("marks gateway as owner-only", async () => {
2221
const tool = createOpenClawTools({
23-
senderIsOwner: false,
2422
config: { commands: { restart: true } },
2523
}).find((candidate) => candidate.name === "gateway");
2624
expect(tool).toBeDefined();
2725
if (!tool) {
2826
throw new Error("missing gateway tool");
2927
}
30-
31-
await expect(
32-
tool.execute("call-owner-check", {
33-
action: "config.get",
34-
}),
35-
).rejects.toThrow("Tool restricted to owner senders.");
36-
expect(callGatewayTool).not.toHaveBeenCalled();
28+
expect(tool.ownerOnly).toBe(true);
3729
});
3830

3931
it("schedules SIGUSR1 restart", async () => {

‎src/agents/openclaw-tools.ts‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ export function createOpenClawTools(options?: {
114114
}),
115115
createCronTool({
116116
agentSessionKey: options?.agentSessionKey,
117-
senderIsOwner: options?.senderIsOwner,
118117
}),
119118
...(messageTool ? [messageTool] : []),
120119
createTtsTool({
@@ -124,7 +123,6 @@ export function createOpenClawTools(options?: {
124123
createGatewayTool({
125124
agentSessionKey: options?.agentSessionKey,
126125
config: options?.config,
127-
senderIsOwner: options?.senderIsOwner,
128126
}),
129127
createAgentsListTool({
130128
agentSessionKey: options?.agentSessionKey,

‎src/agents/tool-policy.e2e.test.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@ function createOwnerPolicyTools() {
2222
},
2323
{
2424
name: "cron",
25+
ownerOnly: true,
2526
// oxlint-disable-next-line typescript/no-explicit-any
2627
execute: async () => ({ content: [], details: {} }) as any,
2728
},
2829
{
2930
name: "gateway",
31+
ownerOnly: true,
3032
// oxlint-disable-next-line typescript/no-explicit-any
3133
execute: async () => ({ content: [], details: {} }) as any,
3234
},
@@ -89,6 +91,19 @@ describe("tool-policy", () => {
8991
const filtered = applyOwnerOnlyToolPolicy(tools, true);
9092
expect(filtered.map((t) => t.name)).toEqual(["read", "cron", "gateway", "whatsapp_login"]);
9193
});
94+
95+
it("honors ownerOnly metadata for custom tool names", async () => {
96+
const tools = [
97+
{
98+
name: "custom_admin_tool",
99+
ownerOnly: true,
100+
// oxlint-disable-next-line typescript/no-explicit-any
101+
execute: async () => ({ content: [], details: {} }) as any,
102+
},
103+
] as unknown as AnyAgentTool[];
104+
expect(applyOwnerOnlyToolPolicy(tools, false)).toEqual([]);
105+
expect(applyOwnerOnlyToolPolicy(tools, true)).toHaveLength(1);
106+
});
92107
});
93108

94109
describe("TOOL_POLICY_CONFORMANCE", () => {

‎src/agents/tool-policy.ts‎

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { OWNER_ONLY_TOOL_ERROR, type AnyAgentTool } from "./tools/common.js";
1+
import { type AnyAgentTool, wrapOwnerOnlyToolExecution } from "./tools/common.js";
22

33
export type ToolProfileId = "minimal" | "coding" | "messaging" | "full";
44

@@ -60,7 +60,7 @@ export const TOOL_GROUPS: Record<string, string[]> = {
6060
],
6161
};
6262

63-
const OWNER_ONLY_TOOL_NAMES = new Set<string>(["whatsapp_login", "cron", "gateway"]);
63+
const OWNER_ONLY_TOOL_NAME_FALLBACKS = new Set<string>(["whatsapp_login", "cron", "gateway"]);
6464

6565
const TOOL_PROFILES: Record<ToolProfileId, ToolProfilePolicy> = {
6666
minimal: {
@@ -87,28 +87,24 @@ export function normalizeToolName(name: string) {
8787
}
8888

8989
export function isOwnerOnlyToolName(name: string) {
90-
return OWNER_ONLY_TOOL_NAMES.has(normalizeToolName(name));
90+
return OWNER_ONLY_TOOL_NAME_FALLBACKS.has(normalizeToolName(name));
91+
}
92+
93+
function isOwnerOnlyTool(tool: AnyAgentTool) {
94+
return tool.ownerOnly === true || isOwnerOnlyToolName(tool.name);
9195
}
9296

9397
export function applyOwnerOnlyToolPolicy(tools: AnyAgentTool[], senderIsOwner: boolean) {
9498
const withGuard = tools.map((tool) => {
95-
if (!isOwnerOnlyToolName(tool.name)) {
96-
return tool;
97-
}
98-
if (senderIsOwner || !tool.execute) {
99+
if (!isOwnerOnlyTool(tool)) {
99100
return tool;
100101
}
101-
return {
102-
...tool,
103-
execute: async () => {
104-
throw new Error(OWNER_ONLY_TOOL_ERROR);
105-
},
106-
};
102+
return wrapOwnerOnlyToolExecution(tool, senderIsOwner);
107103
});
108104
if (senderIsOwner) {
109105
return withGuard;
110106
}
111-
return withGuard.filter((tool) => !isOwnerOnlyToolName(tool.name));
107+
return withGuard.filter((tool) => !isOwnerOnlyTool(tool));
112108
}
113109

114110
export function normalizeToolList(list?: string[]) {

‎src/agents/tools/common.ts‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import type { ImageSanitizationLimits } from "../image-sanitization.js";
55
import { sanitizeToolResultImages } from "../tool-images.js";
66

77
// oxlint-disable-next-line typescript/no-explicit-any
8-
export type AnyAgentTool = AgentTool<any, unknown>;
8+
export type AnyAgentTool = AgentTool<any, unknown> & {
9+
ownerOnly?: boolean;
10+
};
911

1012
export type StringParamOptions = {
1113
required?: boolean;
@@ -210,10 +212,19 @@ export function jsonResult(payload: unknown): AgentToolResult<unknown> {
210212
};
211213
}
212214

213-
export function assertOwnerSender(senderIsOwner?: boolean): void {
214-
if (senderIsOwner === false) {
215-
throw new Error(OWNER_ONLY_TOOL_ERROR);
215+
export function wrapOwnerOnlyToolExecution(
216+
tool: AnyAgentTool,
217+
senderIsOwner: boolean,
218+
): AnyAgentTool {
219+
if (tool.ownerOnly !== true || senderIsOwner || !tool.execute) {
220+
return tool;
216221
}
222+
return {
223+
...tool,
224+
execute: async () => {
225+
throw new Error(OWNER_ONLY_TOOL_ERROR);
226+
},
227+
};
217228
}
218229

219230
export async function imageResult(params: {

‎src/agents/tools/cron-tool.e2e.test.ts‎

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,9 @@ describe("cron tool", () => {
3030
callGatewayMock.mockResolvedValue({ ok: true });
3131
});
3232

33-
it("rejects non-owner callers explicitly", async () => {
34-
const tool = createCronTool({ senderIsOwner: false });
35-
await expect(
36-
tool.execute("call-owner-check", {
37-
action: "status",
38-
}),
39-
).rejects.toThrow("Tool restricted to owner senders.");
40-
expect(callGatewayMock).not.toHaveBeenCalled();
33+
it("marks cron as owner-only", async () => {
34+
const tool = createCronTool();
35+
expect(tool.ownerOnly).toBe(true);
4136
});
4237

4338
it.each([

‎src/agents/tools/cron-tool.ts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { extractTextFromChatContent } from "../../shared/chat-content.js";
88
import { isRecord, truncateUtf16Safe } from "../../utils.js";
99
import { resolveSessionAgentId } from "../agent-scope.js";
1010
import { optionalStringEnum, stringEnum } from "../schema/typebox.js";
11-
import { assertOwnerSender, type AnyAgentTool, jsonResult, readStringParam } from "./common.js";
11+
import { type AnyAgentTool, jsonResult, readStringParam } from "./common.js";
1212
import { callGatewayTool, readGatewayCallOptions, type GatewayCallOptions } from "./gateway.js";
1313
import { resolveInternalSessionKey, resolveMainSessionAlias } from "./sessions-helpers.js";
1414

@@ -48,7 +48,6 @@ const CronToolSchema = Type.Object({
4848

4949
type CronToolOptions = {
5050
agentSessionKey?: string;
51-
senderIsOwner?: boolean;
5251
};
5352

5453
type ChatMessage = {
@@ -202,6 +201,7 @@ export function createCronTool(opts?: CronToolOptions): AnyAgentTool {
202201
return {
203202
label: "Cron",
204203
name: "cron",
204+
ownerOnly: true,
205205
description: `Manage Gateway cron jobs (status/list/add/update/remove/run/runs) and send wake events.
206206
207207
ACTIONS:
@@ -260,7 +260,6 @@ WAKE MODES (for wake action):
260260
Use jobId as the canonical identifier; id is accepted for compatibility. Use contextMessages (0-10) to add previous messages as context to the job text.`,
261261
parameters: CronToolSchema,
262262
execute: async (_toolCallId, args) => {
263-
assertOwnerSender(opts?.senderIsOwner);
264263
const params = args as Record<string, unknown>;
265264
const action = readStringParam(params, "action", { required: true });
266265
const gatewayOpts: GatewayCallOptions = {

‎src/agents/tools/gateway-tool.ts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
} from "../../infra/restart-sentinel.js";
1111
import { scheduleGatewaySigusr1Restart } from "../../infra/restart.js";
1212
import { stringEnum } from "../schema/typebox.js";
13-
import { assertOwnerSender, type AnyAgentTool, jsonResult, readStringParam } from "./common.js";
13+
import { type AnyAgentTool, jsonResult, readStringParam } from "./common.js";
1414
import { callGatewayTool, readGatewayCallOptions } from "./gateway.js";
1515

1616
const DEFAULT_UPDATE_TIMEOUT_MS = 20 * 60_000;
@@ -65,16 +65,15 @@ const GatewayToolSchema = Type.Object({
6565
export function createGatewayTool(opts?: {
6666
agentSessionKey?: string;
6767
config?: OpenClawConfig;
68-
senderIsOwner?: boolean;
6968
}): AnyAgentTool {
7069
return {
7170
label: "Gateway",
7271
name: "gateway",
72+
ownerOnly: true,
7373
description:
7474
"Restart, apply config, or update the gateway in-place (SIGUSR1). Use config.patch for safe partial config updates (merges with existing). Use config.apply only when replacing entire config. Both trigger restart after writing. Always pass a human-readable completion message via the `note` parameter so the system can deliver it to the user after restart.",
7575
parameters: GatewayToolSchema,
7676
execute: async (_toolCallId, args) => {
77-
assertOwnerSender(opts?.senderIsOwner);
7877
const params = args as Record<string, unknown>;
7978
const action = readStringParam(params, "action", { required: true });
8079
if (action === "restart") {

0 commit comments

Comments
 (0)