Skip to content

Commit 144e589

Browse files
committed
fix(policy): honor node command deny overrides
1 parent 997f74f commit 144e589

4 files changed

Lines changed: 142 additions & 18 deletions

File tree

‎extensions/policy/src/doctor/register.test.ts‎

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6697,7 +6697,7 @@ describe("registerPolicyDoctorChecks", () => {
66976697
requireUrlAllowlists: true,
66986698
},
66996699
nodes: {
6700-
denyCommands: ["system.run", "system.run.prepare", "system.which"],
6700+
denyCommands: ["system.run"],
67016701
},
67026702
},
67036703
}),
@@ -6760,14 +6760,78 @@ describe("registerPolicyDoctorChecks", () => {
67606760
expect.objectContaining({
67616761
checkId: "policy/gateway-node-command-denied",
67626762
severity: "error",
6763-
ocPath: "oc://openclaw.config/gateway/nodes/allowCommands/#2",
6763+
ocPath: "oc://openclaw.config/gateway/nodes/denyCommands",
67646764
requirement: "oc://policy.jsonc/gateway/nodes/denyCommands",
67656765
}),
67666766
]),
67676767
);
67686768
expect(result.findings).toHaveLength(13);
67696769
});
67706770

6771+
it("does not report gateway node commands denied by runtime config", async () => {
6772+
const configPath = join(workspaceDir, "openclaw.jsonc");
6773+
const cfg = {
6774+
...cfgWithPolicy(),
6775+
gateway: {
6776+
nodes: {
6777+
allowCommands: ["system.run"],
6778+
denyCommands: ["system.run"],
6779+
},
6780+
},
6781+
} as unknown as OpenClawConfig;
6782+
await fs.writeFile(configPath, "{}", "utf-8");
6783+
await fs.writeFile(
6784+
join(workspaceDir, "policy.jsonc"),
6785+
JSON.stringify({
6786+
gateway: {
6787+
nodes: {
6788+
denyCommands: ["system.run"],
6789+
},
6790+
},
6791+
}),
6792+
"utf-8",
6793+
);
6794+
6795+
registerPolicyDoctorChecks();
6796+
const result = await runDoctorLintChecks(ctx(configPath, cfg));
6797+
6798+
expect(result.findings).toEqual([]);
6799+
});
6800+
6801+
it("reports gateway node commands denied by policy without explicit extra allows", async () => {
6802+
const configPath = join(workspaceDir, "openclaw.jsonc");
6803+
const cfg = {
6804+
...cfgWithPolicy(),
6805+
gateway: {
6806+
nodes: {},
6807+
},
6808+
} as unknown as OpenClawConfig;
6809+
await fs.writeFile(configPath, "{}", "utf-8");
6810+
await fs.writeFile(
6811+
join(workspaceDir, "policy.jsonc"),
6812+
JSON.stringify({
6813+
gateway: {
6814+
nodes: {
6815+
denyCommands: ["system.run"],
6816+
},
6817+
},
6818+
}),
6819+
"utf-8",
6820+
);
6821+
6822+
registerPolicyDoctorChecks();
6823+
const result = await runDoctorLintChecks(ctx(configPath, cfg));
6824+
6825+
expect(result.findings).toEqual([
6826+
expect.objectContaining({
6827+
checkId: "policy/gateway-node-command-denied",
6828+
severity: "error",
6829+
ocPath: "oc://openclaw.config/gateway/nodes/denyCommands",
6830+
requirement: "oc://policy.jsonc/gateway/nodes/denyCommands",
6831+
}),
6832+
]);
6833+
});
6834+
67716835
it("reports omitted gateway bind when non-loopback exposure is denied", async () => {
67726836
const configPath = join(workspaceDir, "openclaw.jsonc");
67736837
const cfg = {

‎extensions/policy/src/doctor/scopes/gateway.ts‎

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -348,29 +348,52 @@ function gatewayNodeCommandFindings(
348348
policyDocName: string,
349349
evidence: PolicyEvidence,
350350
): readonly HealthFinding[] {
351-
const denied = new Set(
352-
readStringList(policy, ["gateway", "nodes", "denyCommands"], { lowercase: false }),
353-
);
354-
if (denied.size === 0) {
351+
if (!hasValidOptionalStringList(policy, ["gateway", "nodes", "denyCommands"])) {
355352
return [];
356353
}
357-
return (evidence.gatewayExposure ?? [])
358-
.filter(
359-
(entry) =>
360-
entry.kind === "nodeCommand" && entry.command !== undefined && denied.has(entry.command),
361-
)
362-
.map((entry): HealthFinding => {
354+
const policyDenied = readStringList(policy, ["gateway", "nodes", "denyCommands"], {
355+
lowercase: false,
356+
});
357+
if (policyDenied.length === 0) {
358+
return [];
359+
}
360+
const configDenied = new Set(
361+
(evidence.gatewayExposure ?? [])
362+
.filter((entry) => entry.kind === "nodeDenyCommand" && entry.command !== undefined)
363+
.map((entry) => entry.command),
364+
);
365+
return policyDenied
366+
.filter((command) => !configDenied.has(command))
367+
.map((command): HealthFinding => {
363368
return {
364369
checkId: CHECK_IDS.policyGatewayNodeCommandDenied,
365370
severity: "error",
366-
message: `Gateway node command '${entry.command ?? entry.id}' is denied by policy.`,
371+
message: `Gateway node command '${command}' is denied by policy but not denied by OpenClaw config.`,
367372
source: "policy",
368373
path: "openclaw config",
369-
ocPath: entry.source,
370-
target: entry.source,
374+
ocPath: "oc://openclaw.config/gateway/nodes/denyCommands",
375+
target: "oc://openclaw.config/gateway/nodes/denyCommands",
371376
requirement: `oc://${policyDocName}/gateway/nodes/denyCommands`,
372-
fixHint:
373-
"Remove the command from gateway.nodes.allowCommands or update policy after review.",
377+
fixHint: `Add '${command}' to gateway.nodes.denyCommands or update policy after review.`,
374378
};
375379
});
376380
}
381+
382+
function hasValidOptionalStringList(policy: unknown, path: readonly string[]): boolean {
383+
let current: unknown = policy;
384+
for (const part of path) {
385+
if (!isRecord(current)) {
386+
return true;
387+
}
388+
current = current[part];
389+
}
390+
return (
391+
current === undefined ||
392+
(Array.isArray(current) &&
393+
current.every((entry) => typeof entry === "string" && entry.trim() !== ""))
394+
);
395+
}
396+
397+
function isRecord(value: unknown): value is Record<string, unknown> {
398+
return typeof value === "object" && value !== null && !Array.isArray(value);
399+
}

‎extensions/policy/src/doctor/strictness.test.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ describe("policy doctor strictness", () => {
1111
const denyTools = POLICY_RULE_METADATA.find(
1212
(rule) => rule.policyPath.join(".") === "tools.denyTools",
1313
);
14+
const denyNodeCommands = POLICY_RULE_METADATA.find(
15+
(rule) => rule.policyPath.join(".") === "gateway.nodes.denyCommands",
16+
);
1417
const fsWorkspaceOnly = POLICY_RULE_METADATA.find(
1518
(rule) => rule.policyPath.join(".") === "tools.fs.requireWorkspaceOnly",
1619
);
@@ -23,6 +26,7 @@ describe("policy doctor strictness", () => {
2326

2427
expect(allowHosts).toBeDefined();
2528
expect(denyTools).toBeDefined();
29+
expect(denyNodeCommands).toBeDefined();
2630
expect(fsWorkspaceOnly).toBeDefined();
2731
expect(denyHostNetwork).toBeDefined();
2832
expect(alsoAllow).toBeDefined();
@@ -34,6 +38,13 @@ describe("policy doctor strictness", () => {
3438
expect(isPolicyValueAtLeastAsStrict(denyTools!, ["write"], ["exec"])).toBe(false);
3539
expect(isPolicyValueAtLeastAsStrict(denyTools!, ["group:runtime"], ["exec"])).toBe(true);
3640
expect(isPolicyValueAtLeastAsStrict(denyTools!, ["exec"], ["group:runtime"])).toBe(false);
41+
expect(isPolicyValueAtLeastAsStrict(denyNodeCommands!, ["system.run"], ["system.run"])).toBe(
42+
true,
43+
);
44+
expect(isPolicyValueAtLeastAsStrict(denyNodeCommands!, [], ["system.run"])).toBe(false);
45+
expect(isPolicyValueAtLeastAsStrict(denyNodeCommands!, ["system.Run"], ["system.run"])).toBe(
46+
false,
47+
);
3748
expect(isPolicyValueAtLeastAsStrict(denyHostNetwork!, true, true)).toBe(true);
3849
expect(isPolicyValueAtLeastAsStrict(denyHostNetwork!, false, true)).toBe(false);
3950
expect(isPolicyValueAtLeastAsStrict(fsWorkspaceOnly!, true, true)).toBe(true);

‎extensions/policy/src/policy-state.ts‎

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ export type PolicyGatewayExposureEvidence = {
168168
| "httpEndpoint"
169169
| "httpUrlFetch"
170170
| "nodeCommand"
171+
| "nodeDenyCommand"
171172
| "remote"
172173
| "tailscale";
173174
readonly source: string;
@@ -2860,6 +2861,31 @@ function pushGatewayNodeCommandEvidence(
28602861
entries: PolicyGatewayExposureEvidence[],
28612862
nodes: Record<string, unknown>,
28622863
): void {
2864+
const deniedCommands = new Set(
2865+
Array.isArray(nodes.denyCommands)
2866+
? nodes.denyCommands
2867+
.filter((command): command is string => typeof command === "string")
2868+
.map((command) => command.trim())
2869+
: [],
2870+
);
2871+
if (Array.isArray(nodes.denyCommands)) {
2872+
nodes.denyCommands.forEach((command, index) => {
2873+
if (typeof command !== "string") {
2874+
return;
2875+
}
2876+
const normalized = command.trim();
2877+
if (normalized === "") {
2878+
return;
2879+
}
2880+
entries.push({
2881+
id: `gateway-node-deny-command-${normalized}`,
2882+
kind: "nodeDenyCommand",
2883+
source: `oc://openclaw.config/gateway/nodes/denyCommands/#${index}`,
2884+
value: normalized,
2885+
command: normalized,
2886+
});
2887+
});
2888+
}
28632889
if (!Array.isArray(nodes.allowCommands)) {
28642890
return;
28652891
}
@@ -2868,7 +2894,7 @@ function pushGatewayNodeCommandEvidence(
28682894
return;
28692895
}
28702896
const normalized = command.trim();
2871-
if (normalized === "") {
2897+
if (normalized === "" || deniedCommands.has(normalized)) {
28722898
return;
28732899
}
28742900
entries.push({

0 commit comments

Comments
 (0)