Skip to content

Commit aa27ae9

Browse files
giodl73-repoGio Della-Libera
andauthored
policy: preview review-required gateway repairs (#99776)
* policy: preview review-required gateway repairs * docs(policy): document gateway review previews * docs(policy): document gateway review previews * docs(policy): document gateway review previews * docs(policy): document gateway review previews --------- Co-authored-by: Gio Della-Libera <[email protected]>
1 parent 0d86f64 commit aa27ae9

6 files changed

Lines changed: 285 additions & 3 deletions

File tree

‎docs/cli/policy.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -975,6 +975,13 @@ more than the scoped policy target. Gateway HTTP URL-fetch allowlist findings
975975
remain manual because automatic repair cannot choose the correct endpoint URL
976976
allowlist values.
977977

978+
Gateway bind and node-command findings stay review-required. When
979+
`policy/gateway-non-loopback-bind` or `policy/gateway-node-command-denied`
980+
can be mapped to a config path, `doctor --fix` reports the proposed
981+
`gateway.bind` or `gateway.nodes.denyCommands` change as skipped preview
982+
guidance. It does not apply the change, and the finding does not count as
983+
repaired until an operator reviews and updates config or policy.
984+
978985
```jsonc
979986
{
980987
"plugins": {

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

Lines changed: 122 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1797,6 +1797,127 @@ describe("registerPolicyDoctorChecks", () => {
17971797
});
17981798
});
17991799

1800+
it("previews review-required gateway bind repair without mutating config", async () => {
1801+
const configPath = join(workspaceDir, "openclaw.jsonc");
1802+
const cfg = {
1803+
...cfgWithPolicy({ workspaceRepairs: true }),
1804+
gateway: { bind: "lan" },
1805+
} as unknown as OpenClawConfig;
1806+
await fs.writeFile(configPath, "{}", "utf-8");
1807+
await fs.writeFile(
1808+
join(workspaceDir, "policy.jsonc"),
1809+
JSON.stringify({ gateway: { exposure: { allowNonLoopbackBind: false } } }),
1810+
"utf-8",
1811+
);
1812+
1813+
const result = await runPolicyRepairCheck(
1814+
"policy/gateway-non-loopback-bind",
1815+
repairCtx(configPath, cfg),
1816+
);
1817+
1818+
expect(result.status).toBe("skipped");
1819+
expect(result.reason).toBe("policy repair requires review before changing config");
1820+
expect(result.changes).toEqual([
1821+
"Review required: set gateway.bind=loopback for policy conformance.",
1822+
]);
1823+
expect(result.warnings).toEqual([
1824+
"Review required: set gateway.bind=loopback for policy conformance.",
1825+
]);
1826+
expect(result.effects).toEqual([
1827+
{
1828+
kind: "config",
1829+
action: "would-set-after-review",
1830+
target: "gateway.bind=loopback",
1831+
dryRunSafe: true,
1832+
},
1833+
]);
1834+
expect(result.config.gateway?.bind).toBe("lan");
1835+
expect(result.remainingFindings).toHaveLength(1);
1836+
});
1837+
1838+
it("previews review-required custom gateway bind repair without mutating config", async () => {
1839+
const configPath = join(workspaceDir, "openclaw.jsonc");
1840+
const cfg = {
1841+
...cfgWithPolicy({ workspaceRepairs: true }),
1842+
gateway: { bind: "custom", customBindHost: "10.0.0.4" },
1843+
} as unknown as OpenClawConfig;
1844+
await fs.writeFile(configPath, "{}", "utf-8");
1845+
await fs.writeFile(
1846+
join(workspaceDir, "policy.jsonc"),
1847+
JSON.stringify({ gateway: { exposure: { allowNonLoopbackBind: false } } }),
1848+
"utf-8",
1849+
);
1850+
1851+
const result = await runPolicyRepairCheck(
1852+
"policy/gateway-non-loopback-bind",
1853+
repairCtx(configPath, cfg),
1854+
);
1855+
1856+
expect(result.findings).toEqual([
1857+
expect.objectContaining({
1858+
ocPath: "oc://openclaw.config/gateway/customBindHost",
1859+
}),
1860+
]);
1861+
expect(result.status).toBe("skipped");
1862+
expect(result.changes).toEqual([
1863+
"Review required: set gateway.bind=loopback for policy conformance.",
1864+
]);
1865+
expect(result.warnings).toEqual([
1866+
"Review required: set gateway.bind=loopback for policy conformance.",
1867+
]);
1868+
expect(result.effects).toEqual([
1869+
{
1870+
kind: "config",
1871+
action: "would-set-after-review",
1872+
target: "gateway.bind=loopback",
1873+
dryRunSafe: true,
1874+
},
1875+
]);
1876+
expect(result.config.gateway).toMatchObject({
1877+
bind: "custom",
1878+
customBindHost: "10.0.0.4",
1879+
});
1880+
expect(result.remainingFindings).toHaveLength(1);
1881+
});
1882+
1883+
it("previews review-required gateway node command repairs without mutating config", async () => {
1884+
const configPath = join(workspaceDir, "openclaw.jsonc");
1885+
const cfg = {
1886+
...cfgWithPolicy({ workspaceRepairs: true }),
1887+
gateway: { nodes: { denyCommands: ["mcp.help"] } },
1888+
} as unknown as OpenClawConfig;
1889+
await fs.writeFile(configPath, "{}", "utf-8");
1890+
await fs.writeFile(
1891+
join(workspaceDir, "policy.jsonc"),
1892+
JSON.stringify({ gateway: { nodes: { denyCommands: ["mcp.help", "system.run"] } } }),
1893+
"utf-8",
1894+
);
1895+
1896+
const result = await runPolicyRepairCheck(
1897+
"policy/gateway-node-command-denied",
1898+
repairCtx(configPath, cfg),
1899+
);
1900+
1901+
expect(result.status).toBe("skipped");
1902+
expect(result.reason).toBe("policy repair requires review before changing config");
1903+
expect(result.changes).toEqual([
1904+
"Review required: add system.run to gateway.nodes.denyCommands for policy conformance.",
1905+
]);
1906+
expect(result.warnings).toEqual([
1907+
"Review required: add system.run to gateway.nodes.denyCommands for policy conformance.",
1908+
]);
1909+
expect(result.effects).toEqual([
1910+
{
1911+
kind: "config",
1912+
action: "would-append-after-review",
1913+
target: "gateway.nodes.denyCommands += system.run",
1914+
dryRunSafe: true,
1915+
},
1916+
]);
1917+
expect(result.config.gateway?.nodes?.denyCommands).toEqual(["mcp.help"]);
1918+
expect(result.remainingFindings).toHaveLength(1);
1919+
});
1920+
18001921
it("repairs automatic channel ingress narrowing findings", async () => {
18011922
const configPath = join(workspaceDir, "openclaw.jsonc");
18021923
const cfg = {
@@ -2161,7 +2282,7 @@ describe("registerPolicyDoctorChecks", () => {
21612282
]);
21622283
});
21632284

2164-
it("does not register repair for review-required policy findings", () => {
2285+
it("does not register repair for non-previewable policy findings", () => {
21652286
const check = registerChecks().find(
21662287
(entry) => entry.id === "policy/gateway-http-url-fetch-unrestricted",
21672288
);
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
// Policy review-required repairs surface proposed config changes without applying them.
2+
import type {
3+
HealthFinding,
4+
HealthRepairContext,
5+
HealthRepairEffect,
6+
HealthRepairResult,
7+
} from "openclaw/plugin-sdk/health";
8+
import { POLICY_FIX_METADATA_BY_CHECK_ID } from "./fix-metadata.js";
9+
import { CHECK_IDS, type POLICY_CHECK_IDS } from "./metadata.js";
10+
11+
type PolicyCheckId = (typeof POLICY_CHECK_IDS)[number];
12+
13+
const REVIEW_REQUIRED_REPAIR_CHECK_IDS = new Set<PolicyCheckId>([
14+
CHECK_IDS.policyGatewayNonLoopbackBind,
15+
CHECK_IDS.policyGatewayNodeCommandDenied,
16+
]);
17+
18+
export function previewPolicyReviewRequiredRepair(
19+
_ctx: HealthRepairContext,
20+
findings: readonly HealthFinding[],
21+
checkId: PolicyCheckId,
22+
): Promise<HealthRepairResult> {
23+
const metadata = POLICY_FIX_METADATA_BY_CHECK_ID.get(checkId);
24+
if (!REVIEW_REQUIRED_REPAIR_CHECK_IDS.has(checkId) || metadata?.fixClass !== "reviewRequired") {
25+
return Promise.resolve({
26+
status: "skipped",
27+
reason: "policy finding does not have a review-required repair preview",
28+
changes: [],
29+
});
30+
}
31+
if (
32+
findings.length === 0 ||
33+
findings.some(
34+
(finding) =>
35+
finding.checkId !== checkId ||
36+
POLICY_FIX_METADATA_BY_CHECK_ID.get(finding.checkId)?.fixClass !== "reviewRequired",
37+
)
38+
) {
39+
return Promise.resolve({
40+
status: "skipped",
41+
reason: "policy finding is not classified as review-required",
42+
changes: [],
43+
});
44+
}
45+
46+
const previews = findings.flatMap((finding) => previewForFinding(finding, checkId));
47+
if (previews.length === 0) {
48+
return Promise.resolve({
49+
status: "skipped",
50+
reason: "policy review-required repair had no previewable config changes",
51+
changes: [],
52+
});
53+
}
54+
55+
return Promise.resolve({
56+
status: "skipped",
57+
reason: "policy repair requires review before changing config",
58+
changes: uniqueStrings(previews.map((preview) => preview.change)),
59+
warnings: uniqueStrings(previews.map((preview) => preview.change)),
60+
effects: uniqueEffects(previews.map((preview) => preview.effect)),
61+
});
62+
}
63+
64+
function previewForFinding(
65+
finding: HealthFinding,
66+
checkId: PolicyCheckId,
67+
): readonly { readonly change: string; readonly effect: HealthRepairEffect }[] {
68+
switch (checkId) {
69+
case CHECK_IDS.policyGatewayNonLoopbackBind:
70+
return previewGatewayLoopbackBind(finding);
71+
case CHECK_IDS.policyGatewayNodeCommandDenied:
72+
return previewGatewayNodeDenyCommand(finding);
73+
default:
74+
return [];
75+
}
76+
}
77+
78+
function previewGatewayLoopbackBind(
79+
finding: HealthFinding,
80+
): readonly { readonly change: string; readonly effect: HealthRepairEffect }[] {
81+
if (
82+
finding.ocPath !== "oc://openclaw.config/gateway/bind" &&
83+
finding.ocPath !== "oc://openclaw.config/gateway/customBindHost"
84+
) {
85+
return [];
86+
}
87+
return [
88+
{
89+
change: "Review required: set gateway.bind=loopback for policy conformance.",
90+
effect: {
91+
kind: "config",
92+
action: "would-set-after-review",
93+
target: "gateway.bind=loopback",
94+
dryRunSafe: true,
95+
},
96+
},
97+
];
98+
}
99+
100+
function previewGatewayNodeDenyCommand(
101+
finding: HealthFinding,
102+
): readonly { readonly change: string; readonly effect: HealthRepairEffect }[] {
103+
const command = finding.message.match(/Gateway node command '([^']+)'/)?.[1]?.trim();
104+
if (
105+
command === undefined ||
106+
command === "" ||
107+
finding.ocPath !== "oc://openclaw.config/gateway/nodes/denyCommands"
108+
) {
109+
return [];
110+
}
111+
return [
112+
{
113+
change: `Review required: add ${command} to gateway.nodes.denyCommands for policy conformance.`,
114+
effect: {
115+
kind: "config",
116+
action: "would-append-after-review",
117+
target: `gateway.nodes.denyCommands += ${command}`,
118+
dryRunSafe: true,
119+
},
120+
},
121+
];
122+
}
123+
124+
function uniqueStrings(values: readonly string[]): readonly string[] {
125+
return [...new Set(values)];
126+
}
127+
128+
function uniqueEffects(values: readonly HealthRepairEffect[]): readonly HealthRepairEffect[] {
129+
const seen = new Set<string>();
130+
return values.filter((value) => {
131+
const key = JSON.stringify(value);
132+
if (seen.has(key)) {
133+
return false;
134+
}
135+
seen.add(key);
136+
return true;
137+
});
138+
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { HealthCheck, HealthFinding } from "openclaw/plugin-sdk/health";
33
import type { PolicyEvidence } from "../../policy-state.js";
44
import { repairPolicyAutomaticNarrower } from "../automatic-repairs.js";
55
import { CHECK_IDS } from "../metadata.js";
6+
import { previewPolicyReviewRequiredRepair } from "../review-required-repairs.js";
67
import type { PolicyDoctorCheckDeps } from "../types.js";
78
import { readPolicyBoolean, readStringList } from "../utils.js";
89

@@ -17,6 +18,13 @@ export function createPolicyGatewayChecks(deps: PolicyDoctorCheckDeps): readonly
1718
async detect(ctx) {
1819
return findingsForCheck(await evaluatePolicy(ctx), CHECK_IDS.policyGatewayNonLoopbackBind);
1920
},
21+
repair(ctx, findings) {
22+
return previewPolicyReviewRequiredRepair(
23+
ctx,
24+
findings,
25+
CHECK_IDS.policyGatewayNonLoopbackBind,
26+
);
27+
},
2028
};
2129
const policyGatewayAuthDisabledCheck: HealthCheck = {
2230
id: CHECK_IDS.policyGatewayAuthDisabled,
@@ -108,6 +116,13 @@ export function createPolicyGatewayChecks(deps: PolicyDoctorCheckDeps): readonly
108116
async detect(ctx) {
109117
return findingsForCheck(await evaluatePolicy(ctx), CHECK_IDS.policyGatewayNodeCommandDenied);
110118
},
119+
repair(ctx, findings) {
120+
return previewPolicyReviewRequiredRepair(
121+
ctx,
122+
findings,
123+
CHECK_IDS.policyGatewayNodeCommandDenied,
124+
);
125+
},
111126
};
112127

113128
return [

‎src/flows/doctor-repair-flow.test.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ describe("runDoctorHealthRepairs", () => {
292292
return {
293293
status: "skipped",
294294
reason: "manual confirmation required",
295-
changes: [],
295+
changes: ["Review required before changing gateway.mode."],
296296
};
297297
},
298298
}),
@@ -304,6 +304,7 @@ describe("runDoctorHealthRepairs", () => {
304304
expect(result.checksRepaired).toBe(0);
305305
expect(result.checksValidated).toBe(0);
306306
expect(result.remainingFindings).toEqual([]);
307+
expect(result.changes).toEqual(["Review required before changing gateway.mode."]);
307308
expect(result.warnings).toEqual(["test/skipped repair skipped: manual confirmation required"]);
308309
});
309310

‎src/flows/doctor-repair-flow.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ async function runSplitHealthCheck(
130130
warnings.push(...(result.warnings ?? []));
131131
diffs.push(...(result.diffs ?? []));
132132
effects.push(...(result.effects ?? []));
133+
changes.push(...result.changes);
133134
const status = result.status ?? "repaired";
134135
if (status !== "repaired") {
135136
warnings.push(`${check.id} repair ${status}${result.reason ? `: ${result.reason}` : ""}`);
@@ -138,7 +139,6 @@ async function runSplitHealthCheck(
138139
if (result.config !== undefined && opts.dryRun !== true) {
139140
cfg = result.config;
140141
}
141-
changes.push(...result.changes);
142142
checksRepaired++;
143143
if (opts.dryRun === true) {
144144
return repairRunResult(cfg, findings, remainingFindings, changes, warnings, diffs, effects, {

0 commit comments

Comments
 (0)