Skip to content

Commit 54b11a2

Browse files
committed
fix(gateway): keep default permission labels truthful
Resolve agent defaults through canonical exec policy with one approvals snapshot per listing. Omit labels when sandbox scope or noncanonical policy pairs make them inaccurate, and preserve the existing optional picker projection. Refs: #132989
1 parent 919db9f commit 54b11a2

3 files changed

Lines changed: 138 additions & 60 deletions

File tree

‎docs/gateway/permission-modes.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ File tools recognize aliases of the session's trusted root and working directory
3030

3131
New sessions, including managed worktree sessions, inherit the configured global or per-agent tool/exec policy when no mode is specified. Creating a worktree pins the working directory without selecting a permission mode. Explicit modes and modes already saved on existing sessions remain unchanged.
3232

33-
The Control UI permission picker labels Default with the agent's configured exec posture when it matches a session mode, for example **Default (Guarded)** for `tools.exec.mode: "ask"`. Global exec settings apply first, then the agent's overrides; without either, the non-sandbox default is full access. Allowlist-only policy and non-equivalent `security`/`ask` pairs, including `ask: "always"`, keep the plain **Default** label. This is display metadata, not an authorization decision or a filesystem-access guarantee: host approval rules, sandbox restrictions, and tool policy still apply. Selecting Default clears the session override; it does not save the displayed mode into the session.
33+
The Control UI permission picker labels Default with the agent's resolved exec posture when it matches a session mode, for example **Default (Guarded)** for `tools.exec.mode: "ask"` without a stricter host approval policy. Resolution includes global settings, agent overrides, and host approval floors. Without those settings or sandboxing, the default is full access. Allowlist-only policy and non-equivalent `security`/`ask` pairs, including `ask: "always"`, keep the plain **Default** label. Agents whose sandbox configuration could apply to their sessions also keep plain **Default**, because effective policy cannot be stated at agent scope. This is display metadata, not an authorization decision or a filesystem-access guarantee; tool policy still applies. Selecting Default clears the session override; it does not save the displayed mode into the session.
3434

3535
## Policy precedence and clamping
3636

‎src/gateway/session-utils-store.ts‎

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@ import {
1414
resolveAgentModelFallbacksOverride,
1515
resolveAgentWorkspaceDir,
1616
} from "../agents/agent-scope.js";
17+
import { resolveExecDefaults } from "../agents/exec-defaults.js";
1718
import { resolveAgentAvatarUrlFromSource } from "../agents/identity-avatar-file.js";
1819
import type { ModelCatalogEntry } from "../agents/model-catalog.js";
1920
import { splitTrailingAuthProfile } from "../agents/model-ref-profile.js";
2021
import { resolveDefaultModelForAgent } from "../agents/model-selection.js";
22+
import { resolveSandboxConfigForAgent } from "../agents/sandbox/config.js";
2123
import { SESSION_PERMISSION_BY_EXEC_MODE } from "../agents/session-permission-exec-mode.js";
2224
import { insideGitCheckout } from "../agents/worktrees/git.js";
2325
import { getRuntimeConfig } from "../config/io.js";
@@ -29,14 +31,8 @@ import {
2931
} from "../config/sessions.js";
3032
import { canonicalSessionKeyMigrationRequiredError } from "../config/sessions/session-canonical-key.js";
3133
import type { OpenClawConfig } from "../config/types.openclaw.js";
32-
import {
33-
type ExecAsk,
34-
type ExecMode,
35-
type ExecSecurity,
36-
resolveExecModeFromPolicy,
37-
resolveExecPolicyForMode,
38-
} from "../infra/exec-approvals-core.js";
39-
import { applyExecPolicyLayer } from "../infra/exec-policy.js";
34+
import { resolveExecPolicyForMode } from "../infra/exec-approvals-core.js";
35+
import { loadExecApprovals } from "../infra/exec-approvals-store.js";
4036
import { normalizeAgentId, parseAgentSessionKey } from "../routing/session-key.js";
4137
import { isAcpSessionKey } from "../sessions/session-key-utils.js";
4238
import { listAgentProvenance } from "../state/agent-provenance.js";
@@ -309,14 +305,12 @@ function resolveGatewayAgentModel(
309305
};
310306
}
311307

312-
function configuredPermissionLabel(policy: {
313-
mode?: ExecMode;
314-
security: ExecSecurity;
315-
ask: ExecAsk;
316-
}): GatewayAgentRow["defaultPermissionMode"] {
317-
const mode = policy.mode ?? resolveExecModeFromPolicy(policy);
308+
function resolvedPermissionLabel(
309+
policy: Pick<ReturnType<typeof resolveExecDefaults>, "mode" | "security" | "ask">,
310+
): GatewayAgentRow["defaultPermissionMode"] {
311+
const { mode } = policy;
318312
const canonical = resolveExecPolicyForMode(mode);
319-
// Display configured posture, never authorization. Allowlist has no matching
313+
// Display resolved posture, never authorization. Allowlist has no matching
320314
// session mode; lossy security/ask pairs must also remain unlabeled.
321315
return mode !== "allowlist" &&
322316
policy.security === canonical.security &&
@@ -341,12 +335,8 @@ export function listAgentsForGateway(
341335
agents: GatewayAgentRow[];
342336
} {
343337
const basic = listGatewayAgentsBasic(cfg);
344-
const globalExecPolicy = applyExecPolicyLayer({ security: "full", ask: "off" }, cfg.tools?.exec);
345-
const globalPermissionLabel = configuredPermissionLabel(globalExecPolicy);
346-
const configuredById = new Map<
347-
string,
348-
Pick<GatewayAgentRow, "identity" | "defaultPermissionMode">
349-
>();
338+
const execApprovals = loadExecApprovals();
339+
const identityById = new Map<string, GatewayAgentRow["identity"]>();
350340
for (const entry of listAgentEntries(cfg)) {
351341
if (!entry?.id) {
352342
continue;
@@ -363,12 +353,7 @@ export function listAgentsForGateway(
363353
avatarUrl,
364354
}
365355
: undefined;
366-
configuredById.set(agentId, {
367-
identity,
368-
defaultPermissionMode: configuredPermissionLabel(
369-
applyExecPolicyLayer(globalExecPolicy, entry.tools?.exec),
370-
),
371-
});
356+
identityById.set(agentId, identity);
372357
}
373358
const roster = options?.includeSystem
374359
? basic.agents
@@ -378,8 +363,13 @@ export function listAgentsForGateway(
378363
);
379364
const agents = roster.map((entry) => {
380365
const { id } = entry;
381-
const meta = configuredById.get(id);
382-
const defaultPermissionMode = meta ? meta.defaultPermissionMode : globalPermissionLabel;
366+
const execDefaults = resolveExecDefaults({ cfg, agentId: id, execApprovals });
367+
// This label must never overstate permissiveness. When sandbox policy can vary
368+
// by session, the effective policy is unknowable at agent scope: omit the label.
369+
const defaultPermissionMode =
370+
resolveSandboxConfigForAgent(cfg, id).mode === "off"
371+
? resolvedPermissionLabel(execDefaults)
372+
: undefined;
383373
const resolvedModel = resolveDefaultModelForAgent({ cfg, agentId: id });
384374
const model = resolveGatewayAgentModel(cfg, id, resolvedModel);
385375
const sessionKey = resolveAgentMainSessionKey({ cfg, agentId: id });
@@ -422,7 +412,7 @@ export function listAgentsForGateway(
422412
id,
423413
...(options?.includeSystem ? { kind: entry.kind } : {}),
424414
name: entry.name,
425-
identity: meta?.identity,
415+
identity: identityById.get(id),
426416
workspace,
427417
workspaceGit,
428418
agentRuntime,

‎src/gateway/session-utils.test.ts‎

Lines changed: 117 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import os from "node:os";
55
import path from "node:path";
66
import { afterAll, beforeEach, describe, expect, onTestFinished, test, vi } from "vitest";
77
import { writeAcpSessionMetaForMigration } from "../acp/runtime/session-meta.js";
8+
import { resolveExecDefaults } from "../agents/exec-defaults.js";
89
import { resolveLegacyInheritedAuthAgentId } from "../agents/legacy-inherited-auth-dir.js";
10+
import { SESSION_PERMISSION_BY_EXEC_MODE } from "../agents/session-permission-exec-mode.js";
911
import { resetConfigRuntimeState, setRuntimeConfigSnapshot } from "../config/config.js";
1012
import type { OpenClawConfig } from "../config/config.js";
1113
import { retainLegacyDefaultAgentId } from "../config/legacy.default-agent-owner.js";
@@ -19,6 +21,8 @@ import {
1921
} from "../config/sessions/session-accessor.js";
2022
import { resolveSqliteTargetFromSessionStorePath } from "../config/sessions/session-sqlite-target.js";
2123
import type { CronJob } from "../cron/types.js";
24+
import type { ExecApprovalsFile } from "../infra/exec-approvals-core.js";
25+
import * as execApprovalsStore from "../infra/exec-approvals-store.js";
2226
import { clearPluginMetadataLifecycleCaches } from "../plugins/plugin-metadata-lifecycle.js";
2327
import { createEmptyPluginRegistry } from "../plugins/registry-empty.js";
2428
import { resetPluginRuntimeStateForTest, setActivePluginRegistry } from "../plugins/runtime.js";
@@ -3722,43 +3726,127 @@ describe("gateway session utils", () => {
37223726
[{ mode: "ask" }, { host: "sandbox" }, "guarded"],
37233727
] as const)(
37243728
"listAgentsForGateway labels global %j plus agent %j as %s",
3725-
(globalExec, agentExec, expected) => {
3726-
const cfg: OpenClawConfig = {
3727-
tools: { exec: globalExec },
3728-
agents: { entries: { main: { tools: { exec: agentExec } } } },
3729-
};
3730-
const original = structuredClone(cfg);
3729+
async (globalExec, agentExec, expected) => {
3730+
await withStateDirEnv("openclaw-agent-permission-label-", async () => {
3731+
const cfg: OpenClawConfig = {
3732+
tools: { exec: globalExec },
3733+
agents: { entries: { main: { tools: { exec: agentExec } } } },
3734+
};
3735+
const original = structuredClone(cfg);
3736+
const agent = listAgentsForGateway(cfg).agents.find((entry) => entry.id === "main");
3737+
if (expected === undefined) {
3738+
expect(agent).not.toHaveProperty("defaultPermissionMode");
3739+
} else {
3740+
expect(agent).toHaveProperty("defaultPermissionMode", expected);
3741+
const resolved = resolveExecDefaults({ cfg, agentId: "main" });
3742+
expect(agent?.defaultPermissionMode).toBe(SESSION_PERMISSION_BY_EXEC_MODE[resolved.mode]);
3743+
}
3744+
expect(cfg).toEqual(original);
3745+
});
3746+
},
3747+
);
3748+
3749+
test.each<{
3750+
name: string;
3751+
cfg: OpenClawConfig;
3752+
approvals: ExecApprovalsFile;
3753+
expected: SessionEntry["permissionMode"];
3754+
}>([
3755+
{
3756+
name: "auto tightened by approvals",
3757+
cfg: { tools: { exec: { mode: "auto" } } },
3758+
approvals: { version: 1, defaults: { security: "deny", ask: "off" } },
3759+
expected: undefined,
3760+
},
3761+
{
3762+
name: "full tightened by approvals",
3763+
cfg: { tools: { exec: { mode: "full" } } },
3764+
approvals: { version: 1, defaults: { security: "deny", ask: "off" } },
3765+
expected: "read-only",
3766+
},
3767+
{
3768+
name: "full tightened by agent approvals",
3769+
cfg: { tools: { exec: { mode: "full" } } },
3770+
approvals: { version: 1, agents: { main: { security: "allowlist", ask: "on-miss" } } },
3771+
expected: "guarded",
3772+
},
3773+
{
3774+
name: "wildcard approvals always asking",
3775+
cfg: { tools: { exec: { mode: "ask" } } },
3776+
approvals: { version: 1, agents: { "*": { ask: "always" } } },
3777+
expected: undefined,
3778+
},
3779+
...(["all", "non-main"] as const).flatMap((mode) => [
3780+
{
3781+
name: `global sandbox ${mode}`,
3782+
cfg: { agents: { defaults: { sandbox: { mode } } } },
3783+
approvals: { version: 1 as const },
3784+
expected: undefined,
3785+
},
3786+
{
3787+
name: `agent sandbox ${mode}`,
3788+
cfg: { agents: { entries: { main: { sandbox: { mode } } } } },
3789+
approvals: { version: 1 as const },
3790+
expected: undefined,
3791+
},
3792+
]),
3793+
{
3794+
name: "agent disabling global sandbox",
3795+
cfg: {
3796+
tools: { exec: { mode: "ask" } },
3797+
agents: {
3798+
defaults: { sandbox: { mode: "all" } },
3799+
entries: { main: { sandbox: { mode: "off" } } },
3800+
},
3801+
},
3802+
approvals: { version: 1 },
3803+
expected: "guarded",
3804+
},
3805+
])("listAgentsForGateway never overstates $name", async ({ cfg, approvals, expected }) => {
3806+
await withStateDirEnv("openclaw-agent-permission-floor-", async () => {
3807+
execApprovalsStore.saveExecApprovals(approvals);
37313808
const agent = listAgentsForGateway(cfg).agents.find((entry) => entry.id === "main");
3809+
expect(agent).toBeDefined();
37323810
if (expected === undefined) {
37333811
expect(agent).not.toHaveProperty("defaultPermissionMode");
37343812
} else {
37353813
expect(agent).toHaveProperty("defaultPermissionMode", expected);
37363814
}
3737-
expect(cfg).toEqual(original);
3738-
},
3739-
);
3815+
for (const sessionKey of ["agent:main:main", "agent:main:other"]) {
3816+
const resolved = resolveExecDefaults({ cfg, agentId: "main", sessionKey });
3817+
expect([undefined, SESSION_PERMISSION_BY_EXEC_MODE[resolved.mode]]).toContain(
3818+
agent?.defaultPermissionMode,
3819+
);
3820+
}
3821+
});
3822+
});
37403823

3741-
test("listAgentsForGateway keeps configured permission labels scoped to their agent", () => {
3742-
const cfg: OpenClawConfig = {
3743-
tools: { exec: { mode: "ask" } },
3744-
agents: {
3745-
entries: {
3746-
guarded: {},
3747-
restricted: { tools: { exec: { mode: "allowlist" } } },
3748-
workspace: { tools: { exec: { mode: "auto" } } },
3824+
test("listAgentsForGateway shares one approvals read across agent permission labels", async () => {
3825+
await withStateDirEnv("openclaw-agent-permission-roster-", async () => {
3826+
const cfg: OpenClawConfig = {
3827+
tools: { exec: { mode: "ask" } },
3828+
agents: {
3829+
entries: {
3830+
guarded: {},
3831+
restricted: { tools: { exec: { mode: "allowlist" } } },
3832+
workspace: { tools: { exec: { mode: "auto" } } },
3833+
},
37493834
},
3750-
},
3751-
};
3752-
expect(
3753-
listAgentsForGateway(cfg).agents.map(({ id, defaultPermissionMode }) => [
3754-
id,
3755-
defaultPermissionMode,
3756-
]),
3757-
).toEqual([
3758-
["guarded", "guarded"],
3759-
["restricted", undefined],
3760-
["workspace", "workspace"],
3761-
]);
3835+
};
3836+
const loadApprovals = vi.spyOn(execApprovalsStore, "loadExecApprovals");
3837+
onTestFinished(() => loadApprovals.mockRestore());
3838+
expect(
3839+
listAgentsForGateway(cfg).agents.map(({ id, defaultPermissionMode }) => [
3840+
id,
3841+
defaultPermissionMode,
3842+
]),
3843+
).toEqual([
3844+
["guarded", "guarded"],
3845+
["restricted", undefined],
3846+
["workspace", "workspace"],
3847+
]);
3848+
expect(loadApprovals).toHaveBeenCalledTimes(1);
3849+
});
37623850
});
37633851

37643852
test("listAgentsForGateway includes effective workspace + model for default agent", () => {

0 commit comments

Comments
 (0)