Skip to content

Commit bbb0b57

Browse files
committed
refactor: centralize gateway method policy helpers
1 parent d766465 commit bbb0b57

7 files changed

Lines changed: 80 additions & 37 deletions

File tree

‎src/gateway/method-scopes.test.ts‎

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@ import {
99
import { listGatewayMethods } from "./server-methods-list.js";
1010
import { coreGatewayHandlers } from "./server-methods.js";
1111

12+
const RESERVED_ADMIN_PLUGIN_METHOD = "config.plugin.inspect";
13+
14+
function setPluginGatewayMethodScope(method: string, scope: "operator.read" | "operator.write") {
15+
const registry = createEmptyPluginRegistry();
16+
registry.gatewayMethodScopes = {
17+
[method]: scope,
18+
};
19+
setActivePluginRegistry(registry);
20+
}
21+
1222
afterEach(() => {
1323
setActivePluginRegistry(createEmptyPluginRegistry());
1424
});
@@ -52,13 +62,9 @@ describe("method scope resolution", () => {
5262
});
5363

5464
it("keeps reserved admin namespaces admin-only even if a plugin scope is narrower", () => {
55-
const registry = createEmptyPluginRegistry();
56-
registry.gatewayMethodScopes = {
57-
"config.plugin.inspect": "operator.read",
58-
};
59-
setActivePluginRegistry(registry);
65+
setPluginGatewayMethodScope(RESERVED_ADMIN_PLUGIN_METHOD, "operator.read");
6066

61-
expect(resolveLeastPrivilegeOperatorScopesForMethod("config.plugin.inspect")).toEqual([
67+
expect(resolveLeastPrivilegeOperatorScopesForMethod(RESERVED_ADMIN_PLUGIN_METHOD)).toEqual([
6268
"operator.admin",
6369
]);
6470
});
@@ -119,13 +125,11 @@ describe("operator scope authorization", () => {
119125
});
120126

121127
it("requires admin for reserved admin namespaces even if a plugin registered a narrower scope", () => {
122-
const registry = createEmptyPluginRegistry();
123-
registry.gatewayMethodScopes = {
124-
"config.plugin.inspect": "operator.read",
125-
};
126-
setActivePluginRegistry(registry);
128+
setPluginGatewayMethodScope(RESERVED_ADMIN_PLUGIN_METHOD, "operator.read");
127129

128-
expect(authorizeOperatorScopesForMethod("config.plugin.inspect", ["operator.read"])).toEqual({
130+
expect(
131+
authorizeOperatorScopesForMethod(RESERVED_ADMIN_PLUGIN_METHOD, ["operator.read"]),
132+
).toEqual({
129133
allowed: false,
130134
missingScope: "operator.admin",
131135
});

‎src/gateway/method-scopes.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { getActivePluginRegistry } from "../plugins/runtime.js";
2-
import { isReservedAdminGatewayMethod } from "../shared/gateway-method-prefixes.js";
2+
import { resolveReservedGatewayMethodScope } from "../shared/gateway-method-policy.js";
33

44
export const ADMIN_SCOPE = "operator.admin" as const;
55
export const READ_SCOPE = "operator.read" as const;
@@ -161,8 +161,9 @@ function resolveScopedMethod(method: string): OperatorScope | undefined {
161161
if (explicitScope) {
162162
return explicitScope;
163163
}
164-
if (isReservedAdminGatewayMethod(method)) {
165-
return ADMIN_SCOPE;
164+
const reservedScope = resolveReservedGatewayMethodScope(method);
165+
if (reservedScope) {
166+
return reservedScope;
166167
}
167168
const pluginScope = getActivePluginRegistry()?.gatewayMethodScopes?.[method];
168169
if (pluginScope) {

‎src/plugins/loader.test.ts‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ function memoryPluginBody(id: string) {
7474
return `module.exports = { id: ${JSON.stringify(id)}, kind: "memory", register() {} };`;
7575
}
7676

77+
const RESERVED_ADMIN_PLUGIN_METHOD = "config.plugin.inspect";
78+
const RESERVED_ADMIN_SCOPE_WARNING =
79+
"gateway method scope coerced to operator.admin for reserved core namespace";
80+
7781
function writeBundledPlugin(params: {
7882
id: string;
7983
body?: string;
@@ -975,7 +979,7 @@ describe("loadOpenClawPlugins", () => {
975979
id: "reserved-gateway-scope",
976980
register(api) {
977981
api.registerGatewayMethod(
978-
"config.plugin.inspect",
982+
${JSON.stringify(RESERVED_ADMIN_PLUGIN_METHOD)},
979983
({ respond }) => respond(true, { ok: true }),
980984
{ scope: "operator.read" },
981985
);
@@ -994,12 +998,12 @@ describe("loadOpenClawPlugins", () => {
994998
},
995999
});
9961000

997-
expect(Object.keys(registry.gatewayHandlers)).toContain("config.plugin.inspect");
998-
expect(registry.gatewayMethodScopes?.["config.plugin.inspect"]).toBe("operator.admin");
1001+
expect(Object.keys(registry.gatewayHandlers)).toContain(RESERVED_ADMIN_PLUGIN_METHOD);
1002+
expect(registry.gatewayMethodScopes?.[RESERVED_ADMIN_PLUGIN_METHOD]).toBe("operator.admin");
9991003
expect(
10001004
registry.diagnostics.some((diag) =>
10011005
String(diag.message).includes(
1002-
"gateway method scope coerced to operator.admin for reserved core namespace: config.plugin.inspect",
1006+
`${RESERVED_ADMIN_SCOPE_WARNING}: ${RESERVED_ADMIN_PLUGIN_METHOD}`,
10031007
),
10041008
),
10051009
).toBe(true);

‎src/plugins/registry.ts‎

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import type {
99
} from "../gateway/server-methods/types.js";
1010
import { registerInternalHook } from "../hooks/internal-hooks.js";
1111
import type { HookEntry } from "../hooks/types.js";
12-
import { isReservedAdminGatewayMethod } from "../shared/gateway-method-prefixes.js";
12+
import { normalizePluginGatewayMethodScope } from "../shared/gateway-method-policy.js";
1313
import { resolveUserPath } from "../utils.js";
1414
import { buildPluginApi } from "./api-builder.js";
1515
import { registerPluginCommand, validatePluginCommandDefinition } from "./command-registration.js";
@@ -434,20 +434,16 @@ export function createPluginRegistry(registryParams: PluginRegistryParams) {
434434
return;
435435
}
436436
registry.gatewayHandlers[trimmed] = handler;
437-
let effectiveScope = opts?.scope;
438-
if (
439-
effectiveScope &&
440-
effectiveScope !== "operator.admin" &&
441-
isReservedAdminGatewayMethod(trimmed)
442-
) {
437+
const normalizedScope = normalizePluginGatewayMethodScope(trimmed, opts?.scope);
438+
if (normalizedScope.coercedToReservedAdmin) {
443439
pushDiagnostic({
444440
level: "warn",
445441
pluginId: record.id,
446442
source: record.source,
447443
message: `gateway method scope coerced to operator.admin for reserved core namespace: ${trimmed}`,
448444
});
449-
effectiveScope = "operator.admin";
450445
}
446+
const effectiveScope = normalizedScope.scope;
451447
if (effectiveScope) {
452448
registry.gatewayMethodScopes ??= {};
453449
registry.gatewayMethodScopes[trimmed] = effectiveScope;

‎src/plugins/types.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1992,6 +1992,13 @@ export type OpenClawPluginApi = {
19921992
registerHttpRoute: (params: OpenClawPluginHttpRouteParams) => void;
19931993
/** Register a native messaging channel plugin (channel capability). */
19941994
registerChannel: (registration: OpenClawPluginChannelRegistration | ChannelPlugin) => void;
1995+
/**
1996+
* Register a gateway RPC method for this plugin.
1997+
*
1998+
* Reserved core admin namespaces (`config.*`, `exec.approvals.*`,
1999+
* `wizard.*`, `update.*`) always normalize to `operator.admin` even if a
2000+
* narrower scope is requested.
2001+
*/
19952002
registerGatewayMethod: (
19962003
method: string,
19972004
handler: GatewayRequestHandler,
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
export const RESERVED_ADMIN_GATEWAY_METHOD_PREFIXES = [
2+
"exec.approvals.",
3+
"config.",
4+
"wizard.",
5+
"update.",
6+
] as const;
7+
8+
export const RESERVED_ADMIN_GATEWAY_METHOD_SCOPE = "operator.admin" as const;
9+
10+
export function isReservedAdminGatewayMethod(method: string): boolean {
11+
return RESERVED_ADMIN_GATEWAY_METHOD_PREFIXES.some((prefix) => method.startsWith(prefix));
12+
}
13+
14+
export function resolveReservedGatewayMethodScope(
15+
method: string,
16+
): typeof RESERVED_ADMIN_GATEWAY_METHOD_SCOPE | undefined {
17+
if (!isReservedAdminGatewayMethod(method)) {
18+
return undefined;
19+
}
20+
return RESERVED_ADMIN_GATEWAY_METHOD_SCOPE;
21+
}
22+
23+
export function normalizePluginGatewayMethodScope<TScope extends string>(
24+
method: string,
25+
scope: TScope | undefined,
26+
): {
27+
scope: TScope | typeof RESERVED_ADMIN_GATEWAY_METHOD_SCOPE | undefined;
28+
coercedToReservedAdmin: boolean;
29+
} {
30+
const reservedScope = resolveReservedGatewayMethodScope(method);
31+
if (!reservedScope || !scope || scope === reservedScope) {
32+
return {
33+
scope,
34+
coercedToReservedAdmin: false,
35+
};
36+
}
37+
return {
38+
scope: reservedScope,
39+
coercedToReservedAdmin: true,
40+
};
41+
}

‎src/shared/gateway-method-prefixes.ts‎

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)