Skip to content

Commit ba5f913

Browse files
committed
fix: show effective exec approval policy in Devices
1 parent 9204ab8 commit ba5f913

10 files changed

Lines changed: 209 additions & 24 deletions

File tree

‎apps/shared/OpenClawKit/Sources/OpenClawProtocol/GatewayModels.swift‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17893,24 +17893,28 @@ public struct ExecApprovalsSnapshot: Codable, Sendable {
1789317893
public let exists: Bool
1789417894
public let hash: String
1789517895
public let file: [String: AnyCodable]
17896+
public let resolveddefaults: [String: AnyCodable]?
1789617897

1789717898
public init(
1789817899
path: String,
1789917900
exists: Bool,
1790017901
hash: String,
17901-
file: [String: AnyCodable])
17902+
file: [String: AnyCodable],
17903+
resolveddefaults: [String: AnyCodable]? = nil)
1790217904
{
1790317905
self.path = path
1790417906
self.exists = exists
1790517907
self.hash = hash
1790617908
self.file = file
17909+
self.resolveddefaults = resolveddefaults
1790717910
}
1790817911

1790917912
private enum CodingKeys: String, CodingKey {
1791017913
case path
1791117914
case exists
1791217915
case hash
1791317916
case file
17917+
case resolveddefaults = "resolvedDefaults"
1791417918
}
1791517919
}
1791617920

‎packages/gateway-protocol/src/schema/exec-approvals.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ export const ExecApprovalsSnapshotSchema = closedObject({
7777
exists: Type.Boolean(),
7878
hash: NonEmptyString,
7979
file: ExecApprovalsFileSchema,
80+
resolvedDefaults: Type.Optional(ExecApprovalsResolvedDefaultsSchema),
8081
});
8182

8283
const NativeExecApprovalActionSchema = Type.Union([

‎src/gateway/server-methods/exec-approvals.test.ts‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,37 @@ function makeSnapshot(file: ExecApprovalsFile = { version: 1, agents: {} }) {
2929
}
3030

3131
describe("exec approvals gateway methods", () => {
32+
it("reports runtime defaults for fresh local approval state", async () => {
33+
ensureExecApprovalsSnapshotMock.mockResolvedValueOnce(makeSnapshot({ version: 1, agents: {} }));
34+
const respond = vi.fn();
35+
36+
await expectDefined(
37+
execApprovalsHandlers["exec.approvals.get"],
38+
'execApprovalsHandlers["exec.approvals.get"] test invariant',
39+
)({
40+
req: { type: "req", id: "req-fresh", method: "exec.approvals.get", params: {} },
41+
params: {},
42+
client: null,
43+
isWebchatConnect: () => false,
44+
respond,
45+
context: {} as never,
46+
});
47+
48+
expect(respond).toHaveBeenCalledWith(
49+
true,
50+
expect.objectContaining({
51+
file: { version: 1, agents: {} },
52+
resolvedDefaults: {
53+
security: "full",
54+
ask: "off",
55+
askFallback: "deny",
56+
autoAllowSkills: false,
57+
},
58+
}),
59+
undefined,
60+
);
61+
});
62+
3263
it("returns a structured unavailable error when local approvals get cannot read state", async () => {
3364
ensureExecApprovalsSnapshotMock.mockRejectedValueOnce(
3465
new Error("permission denied while ensuring approvals"),

‎src/gateway/server-methods/exec-approvals.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
mergeExecApprovalsSocketDefaults,
1919
normalizeExecApprovals,
2020
readExecApprovalsSnapshot,
21+
resolveExecApprovalsFromFile,
2122
updateExecApprovals,
2223
type ExecApprovalsFile,
2324
type ExecApprovalsSnapshot,
@@ -104,6 +105,7 @@ function toExecApprovalsPayload(snapshot: ExecApprovalsSnapshot) {
104105
exists: snapshot.exists,
105106
hash: snapshot.hash,
106107
file: redactExecApprovals(snapshot.file),
108+
resolvedDefaults: resolveExecApprovalsFromFile({ file: snapshot.file }).defaults,
107109
};
108110
}
109111

‎src/node-host/invoke.test.ts‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,10 +344,27 @@ describe("node host invoke", () => {
344344
fs.rmSync(path.dirname(payload.path), { recursive: true, force: true });
345345
});
346346

347-
it("returns a redacted exec approvals snapshot", async () => {
347+
it.each([
348+
{ label: "when params are omitted", params: undefined, resolvedDefaults: undefined },
349+
{
350+
label: "when resolved defaults are not requested",
351+
params: { includeResolvedDefaults: false },
352+
resolvedDefaults: undefined,
353+
},
354+
{
355+
label: "with resolved defaults when requested",
356+
params: { includeResolvedDefaults: true },
357+
resolvedDefaults: {
358+
security: "full",
359+
ask: "off",
360+
askFallback: "deny",
361+
autoAllowSkills: false,
362+
},
363+
},
364+
])("returns a redacted exec approvals snapshot $label", async ({ params, resolvedDefaults }) => {
348365
execApprovalsStoreMock.hasEnsureResult = true;
349366
execApprovalsStoreMock.ensureResult = createExecApprovalsSnapshot();
350-
const result = await invokeExecApprovals("system.execApprovals.get");
367+
const result = await invokeExecApprovals("system.execApprovals.get", params);
351368
const payload = JSON.parse(result.payloadJSON ?? "{}") as ExecApprovalsSnapshot;
352369
expect(payload).toEqual({
353370
path: "/tmp/exec-approvals.json",
@@ -357,6 +374,7 @@ describe("node host invoke", () => {
357374
version: 1,
358375
socket: { path: "/tmp/exec-approvals.sock" },
359376
},
377+
...(resolvedDefaults ? { resolvedDefaults } : {}),
360378
});
361379
});
362380

‎src/node-host/invoke.ts‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
normalizeExecApprovals,
1414
readExecApprovalsSnapshot,
1515
resolveAllowAlwaysPatternCoverage,
16+
resolveExecApprovalsFromFile,
1617
updateExecApprovals,
1718
type ExecAsk,
1819
type ExecApprovalsFile,
@@ -674,13 +675,33 @@ async function dispatchInvoke(
674675
return;
675676
}
676677
if (command === "system.execApprovals.get") {
678+
let includeResolvedDefaults = false;
679+
try {
680+
if (frame.paramsJSON != null) {
681+
const params = decodeParams<unknown>(frame.paramsJSON);
682+
if (
683+
!isRecord(params) ||
684+
(params.includeResolvedDefaults !== undefined &&
685+
typeof params.includeResolvedDefaults !== "boolean")
686+
) {
687+
throw new Error("INVALID_REQUEST: includeResolvedDefaults must be boolean");
688+
}
689+
includeResolvedDefaults = params.includeResolvedDefaults === true;
690+
}
691+
} catch (err) {
692+
await sendInvalidRequestResult(client, frame, err);
693+
return;
694+
}
677695
try {
678696
const snapshot = await ensureExecApprovalsSnapshot();
679-
const payload: ExecApprovalsSnapshot = {
697+
const payload = {
680698
path: snapshot.path,
681699
exists: snapshot.exists,
682700
hash: snapshot.hash,
683701
file: redactExecApprovals(snapshot.file),
702+
...(includeResolvedDefaults
703+
? { resolvedDefaults: resolveExecApprovalsFromFile({ file: snapshot.file }).defaults }
704+
: {}),
684705
};
685706
await sendJsonPayloadResult(client, frame, payload);
686707
} catch (err) {

‎ui/src/lib/nodes/index.ts‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,17 @@ export type DevicePairingList = {
8686
paired: PairedDevice[];
8787
};
8888

89+
export type ExecSecurity = "deny" | "allowlist" | "full";
90+
export type ExecAsk = "off" | "on-miss" | "always";
8991
type ExecApprovalsDefaults = {
90-
security?: string;
91-
ask?: string;
92-
askFallback?: string;
92+
security?: ExecSecurity;
93+
ask?: ExecAsk;
94+
askFallback?: ExecSecurity;
9395
autoAllowSkills?: boolean;
9496
};
9597

98+
export type ExecApprovalsResolvedDefaults = Required<ExecApprovalsDefaults>;
99+
96100
export type ExecApprovalsAllowlistEntry = {
97101
id?: string;
98102
pattern: string;
@@ -120,6 +124,7 @@ type FileExecApprovalsSnapshot = {
120124
exists: boolean;
121125
hash: string;
122126
file: ExecApprovalsFile;
127+
resolvedDefaults?: ExecApprovalsResolvedDefaults;
123128
};
124129

125130
type NativeExecApprovalRule = {

‎ui/src/pages/devices/view-exec-approvals.ts‎

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ import {
1515
isNativeExecApprovalsSnapshot,
1616
type ExecApprovalsAllowlistEntry,
1717
type ExecApprovalsFile,
18+
type ExecApprovalsResolvedDefaults,
19+
type ExecAsk,
20+
type ExecSecurity,
1821
type NativeExecApprovalsSnapshot,
1922
} from "../../lib/nodes/index.ts";
2023
import {
@@ -24,16 +27,6 @@ import {
2427
} from "./view-shared.ts";
2528
import type { DevicesProps } from "./view.types.ts";
2629

27-
type ExecSecurity = "deny" | "allowlist" | "full";
28-
type ExecAsk = "off" | "on-miss" | "always";
29-
30-
type ExecApprovalsResolvedDefaults = {
31-
security: ExecSecurity;
32-
ask: ExecAsk;
33-
askFallback: ExecSecurity;
34-
autoAllowSkills: boolean;
35-
};
36-
3730
type ExecApprovalsAgentOption = {
3831
id: string;
3932
name?: string;
@@ -96,13 +89,19 @@ function normalizeAsk(value?: string): ExecAsk {
9689

9790
function resolveExecApprovalsDefaults(
9891
form: ExecApprovalsFile | null,
92+
reported: ExecApprovalsResolvedDefaults | undefined,
93+
includeWildcard: boolean,
9994
): ExecApprovalsResolvedDefaults {
10095
const defaults = form?.defaults ?? {};
96+
const wildcard = includeWildcard ? (form?.agents?.["*"] ?? {}) : {};
10197
return {
102-
security: normalizeSecurity(defaults.security),
103-
ask: normalizeAsk(defaults.ask),
104-
askFallback: normalizeSecurity(defaults.askFallback ?? "deny"),
105-
autoAllowSkills: defaults.autoAllowSkills ?? false,
98+
security: normalizeSecurity(wildcard.security ?? defaults.security ?? reported?.security),
99+
ask: normalizeAsk(wildcard.ask ?? defaults.ask ?? reported?.ask),
100+
askFallback: normalizeSecurity(
101+
wildcard.askFallback ?? defaults.askFallback ?? reported?.askFallback ?? "deny",
102+
),
103+
autoAllowSkills:
104+
wildcard.autoAllowSkills ?? defaults.autoAllowSkills ?? reported?.autoAllowSkills ?? false,
106105
};
107106
}
108107

@@ -165,7 +164,6 @@ export function resolveExecApprovalsState(props: DevicesProps): ExecApprovalsSta
165164
const fileSnapshot = snapshot && !isNativeExecApprovalsSnapshot(snapshot) ? snapshot : null;
166165
const form = nativePolicy ? null : (props.execApprovalsForm ?? fileSnapshot?.file ?? null);
167166
const ready = Boolean(form || nativePolicy);
168-
const defaults = resolveExecApprovalsDefaults(form);
169167
const agents = resolveExecApprovalsAgents(props.configForm, form);
170168
const targetNodes = resolveExecApprovalsNodes(props.nodes);
171169
const target = props.execApprovalsTarget;
@@ -175,6 +173,11 @@ export function resolveExecApprovalsState(props: DevicesProps): ExecApprovalsSta
175173
targetNodeId = null;
176174
}
177175
const selectedScope = resolveExecApprovalsScope(props.execApprovalsSelectedAgent, agents);
176+
const defaults = resolveExecApprovalsDefaults(
177+
form,
178+
fileSnapshot?.resolvedDefaults,
179+
selectedScope !== EXEC_APPROVALS_DEFAULT_SCOPE,
180+
);
178181
const selectedAgent =
179182
selectedScope !== EXEC_APPROVALS_DEFAULT_SCOPE
180183
? (((form?.agents ?? {})[selectedScope] as Record<string, unknown> | undefined) ?? null)

‎ui/src/pages/devices/view-shared.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ export function resolveNodeTargets(
4141
nodes: Array<Record<string, unknown>>,
4242
requiredCommands: string[],
4343
): NodeTargetOption[] {
44-
const required = new Set(requiredCommands);
4544
const list: NodeTargetOption[] = [];
4645

4746
for (const node of nodes) {
4847
const commands = Array.isArray(node.commands) ? node.commands : [];
49-
const supports = commands.some((cmd) => required.has(String(cmd)));
48+
const advertised = new Set(commands.map(String));
49+
const supports = requiredCommands.every((command) => advertised.has(command));
5050
if (!supports) {
5151
continue;
5252
}

0 commit comments

Comments
 (0)