Skip to content

Commit 68ab9f2

Browse files
committed
fix(agents): carry parent sender-policy ceiling for spawned subagents
Instead of skipping sender-policy resolution entirely for recognized subagent sessions, store the parent's resolved senderPolicy in the child's session store entry during spawn and read it back when resolving the child's capability profile. This preserves both allow AND deny ceilings. Write path (spawn chain): - agent-tools.ts captures senderPolicy from parent's capability profile - Passed through SpawnedToolContext -> createSessionsSpawnTool -> spawnSubagentDirect -> written as inheritedSenderPolicy in child's session store entry via buildDirectChildSessionPatch Read path (child resolution): - resolveConversationCapabilityProfile reads stored inheritedSenderPolicy via resolveStoredSenderPolicy when isSubagentSession is detected - Falls back to undefined when no stored policy (pre-existing sessions) Files changed: - subagent-capabilities.ts: resolveStoredSenderPolicy() + type fields - conversation-capability-profile.ts: use stored policy for subagents - spawned-context.ts: inheritedSenderPolicy in SpawnedToolContext - subagent-spawn.ts: add to context + buildDirectChildSessionPatch - sessions-spawn-tool.ts: forward inheritedSenderPolicy - openclaw-tools.ts: forward inheritedSenderPolicy - agent-tools.ts: capture senderPolicy and pass to spawn context - config/sessions/types.ts: inheritedSenderPolicy in SessionEntry - effective-tool-policy.test.ts: 5 scenarios covering allow, deny, subagent restrictions, and authorization ceiling preservation
1 parent 673cc5c commit 68ab9f2

9 files changed

Lines changed: 170 additions & 37 deletions

File tree

‎src/agents/agent-tools.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -998,6 +998,7 @@ function createOpenClawCodingToolsInternal(options?: OpenClawCodingToolsOptions)
998998
oneShotCliRun: options?.oneShotCliRun,
999999
inheritedToolAllowlist,
10001000
inheritedToolDenylist,
1001+
inheritedSenderPolicy: senderPolicy,
10011002
onYield: options?.onYield,
10021003
allowGatewaySubagentBinding: options?.allowGatewaySubagentBinding,
10031004
recordToolPrepStage: options?.recordToolPrepStage,

‎src/agents/conversation-capability-profile.ts‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { resolveSenderToolPolicy } from "./sender-tool-policy.js";
2424
import {
2525
isSubagentEnvelopeSession,
2626
resolveSubagentCapabilityStore,
27+
resolveStoredSenderPolicy,
2728
} from "./subagent-capabilities.js";
2829
import type { PromptMode } from "./system-prompt.types.js";
2930
import {
@@ -224,15 +225,20 @@ export function resolveConversationCapabilityProfile(
224225
});
225226
// Owner WebChat intentionally has no external sender identity. Its trusted
226227
// owner state must not fall through to the wildcard policy for guests.
227-
// Spawned subagents inherit the parent's already-resolved sender policy and
228-
// must not re-resolve without a sender identity, which would match the
229-
// wildcard "*" entry and strip filesystem/runtime tools.
228+
// Spawned subagents inherit the parent's already-resolved sender policy so
229+
// the authorization ceiling (both allow and deny) is preserved. When no
230+
// stored policy exists (pre-existing sessions), undefined is safe — the
231+
// parent already resolved the correct policy during spawn.
230232
const isOwnerInternalSession =
231233
params.senderIsOwner === true &&
232234
normalizeMessageChannel(messageProvider ?? params.messageChannel) === INTERNAL_MESSAGE_CHANNEL;
233-
const senderPolicy =
234-
isOwnerInternalSession || isSubagentSession
235-
? undefined
235+
const senderPolicy = isOwnerInternalSession
236+
? undefined
237+
: isSubagentSession
238+
? resolveStoredSenderPolicy(subagentSessionKey, {
239+
cfg: params.config,
240+
store: subagentStore,
241+
})
236242
: resolveSenderToolPolicy({
237243
config: params.config,
238244
agentId: effective.agentId,

‎src/agents/embedded-agent-runner/effective-tool-policy.test.ts‎

Lines changed: 103 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -292,22 +292,20 @@ describe("applyFinalEffectiveToolPolicy", () => {
292292
expect(filtered).toStrictEqual([]);
293293
});
294294

295-
// ── fix #109025: sender-scoped tool policy for subagents ──────────────
295+
// ── fix #109025: carry parent sender-policy ceiling for subagents ──────
296296
//
297297
// A spawned subagent has no external sender identity, so
298298
// `resolveSenderToolPolicy` would fall through all exact sender matchers
299299
// and return the wildcard "*" entry, stripping filesystem/runtime tools.
300-
// The fix skips sender-policy resolution for recognized subagent
301-
// envelopes (`isSubagentEnvelopeSession`), letting the child inherit the
302-
// parent's already-resolved policy. Subagent restrictions are still
303-
// applied independently so the final clamp is preserved.
300+
// The fix carries the parent's resolved senderPolicy into the child's
301+
// session store entry during spawn, then reads it back when resolving the
302+
// child's capability profile. This preserves both allow AND deny ceilings.
304303
//
305-
// 1. Parent with exact E164 identity → all tools pass sender policy
306-
// 2. Anonymous user (no sender fields) → wildcard deny strips tools
307-
// 3. Spawned subagent from E164 parent → senderPolicy skipped, subagent
308-
// restrictions still narrow the final inventory
309-
// 4. Spawned subagent without subagent → subagent restrictions still
310-
// restrict (inherited allowlist applies on top)
304+
// 1. Parent with exact E164 allow → all tools pass sender policy
305+
// 2. Anonymous (no sender fields) → wildcard deny strips tools
306+
// 3. Subagent (no sender fields) → inherited allow from store
307+
// 4. Subagent + subagent denies → inherited allow + subagent deny
308+
// 5. Subagent with inherited deny → parent's deny ceiling preserved
311309

312310
it("fix #109025: parent with E164 gets exact toolsBySender allow (no denies)", () => {
313311
// WhatsApp parent with E164 → exact allow override, no wildcard denies
@@ -355,39 +353,67 @@ describe("applyFinalEffectiveToolPolicy", () => {
355353
expect(filtered.map((t) => t.name)).toEqual(["message"]);
356354
});
357355

358-
it("fix #109025: spawned subagent skips sender policy, retains parent tools", () => {
359-
// Subagent with subagent session key → isSubagentEnvelopeSession
360-
// detected → senderPolicy = undefined (skipped), so exec/fs_read survive
361-
// the sender-policy step. Subagent restrictions still apply but nothing
362-
// is configured here, so all tools pass.
356+
it("fix #109025: subagent inherits parent allow policy via stored senderPolicy", async () => {
357+
// Realistic subagent scenario: subagent has NO sender fields, but the
358+
// parent's resolved senderPolicy was stored during spawn as
359+
// inheritedSenderPolicy in the session store entry.
360+
const agentId = `subagent-allow-inherit-${Date.now()}-${Math.random().toString(16).slice(2)}`;
361+
const sessionKey = `agent:${agentId}:subagent:child`;
362+
const storePath = createSessionStorePath("openclaw-subagent-allow-inherit", agentId);
363+
await writeSessionEntries(storePath, {
364+
[sessionKey]: {
365+
sessionId: "child-session",
366+
updatedAt: Date.now(),
367+
spawnDepth: 1,
368+
subagentRole: "orchestrator",
369+
subagentControlScope: "children",
370+
// Parent had exact E164 allow:["*"] override stored during spawn
371+
inheritedSenderPolicy: { allow: ["*"] },
372+
},
373+
});
374+
375+
// No sender fields — resolveStoredSenderPolicy reads from store
363376
const filtered = applyFinalPolicy({
364377
bundledTools: [makeTool("exec"), makeTool("fs_read"), makeTool("message")],
365378
config: {
379+
session: { store: storePath },
366380
tools: {
367381
toolsBySender: {
368382
"e164:+1234567890": { allow: ["*"] },
369383
"*": { deny: ["exec", "process", "fs_read", "fs_write"] },
370384
},
371385
},
372386
},
373-
sessionKey: "subagent:child::main",
374-
sandboxSessionKey: "subagent:child::main",
375-
spawnedBy: "main",
387+
sessionKey,
388+
sandboxSessionKey: sessionKey,
389+
spawnedBy: "parent",
376390
messageProvider: "whatsapp",
377-
senderE164: "+1234567890",
378-
senderIsOwner: false,
379391
warn: () => {},
380392
});
381393

382-
// Subagent inherits parent's tools via senderPolicy skip
394+
// Subagent inherits parent's allow — wildcard deny not applied
383395
expect(filtered.map((t) => t.name)).toEqual(
384396
expect.arrayContaining(["exec", "fs_read", "message"]),
385397
);
386398
});
387399

388-
it("fix #109025: subagent restrictions still narrow tools after sender-policy skip", () => {
389-
// Subagent skips sender policy (retains parent tools), but
390-
// subagent tools.deny still removes the configured restricted tools.
400+
it("fix #109025: subagent restrictions still narrow tools after inheritance", async () => {
401+
// Subagent inherits parent's allow policy, but subagent tools.deny
402+
// still removes the configured restricted tools.
403+
const agentId = `subagent-restrict-${Date.now()}-${Math.random().toString(16).slice(2)}`;
404+
const sessionKey = `agent:${agentId}:subagent:restricted`;
405+
const storePath = createSessionStorePath("openclaw-subagent-restrict", agentId);
406+
await writeSessionEntries(storePath, {
407+
[sessionKey]: {
408+
sessionId: "restricted-session",
409+
updatedAt: Date.now(),
410+
spawnDepth: 1,
411+
subagentRole: "orchestrator",
412+
subagentControlScope: "children",
413+
inheritedSenderPolicy: { allow: ["*"] },
414+
},
415+
});
416+
391417
const filtered = applyFinalPolicy({
392418
bundledTools: [
393419
makeTool("exec"),
@@ -396,6 +422,7 @@ describe("applyFinalEffectiveToolPolicy", () => {
396422
makeTool("message"),
397423
],
398424
config: {
425+
session: { store: storePath },
399426
tools: {
400427
toolsBySender: {
401428
"e164:+1234567890": { allow: ["*"] },
@@ -408,20 +435,65 @@ describe("applyFinalEffectiveToolPolicy", () => {
408435
},
409436
},
410437
},
411-
sessionKey: "subagent:child::main",
412-
sandboxSessionKey: "subagent:child::main",
413-
spawnedBy: "main",
438+
sessionKey,
439+
sandboxSessionKey: sessionKey,
440+
spawnedBy: "parent",
414441
messageProvider: "whatsapp",
415-
senderE164: "+1234567890",
416-
senderIsOwner: false,
417442
warn: () => {},
418443
});
419444

420-
// Subagent retains exec and fs_read (sender policy skipped), but
445+
// Subagent retains exec and fs_read (inherited allow), but
421446
// fs_write is removed by the subagent restriction layer
422447
expect(filtered.map((t) => t.name)).toEqual(
423448
expect.arrayContaining(["exec", "fs_read", "message"]),
424449
);
425450
expect(filtered.map((t) => t.name)).not.toContain("fs_write");
426451
});
452+
453+
it("fix #109025: subagent inherits parent deny ceiling — no privilege escalation", async () => {
454+
// Parent had exact E164 deny:["exec","fs_read"] — the child must NOT
455+
// regain those tools even though it has no sender fields.
456+
const agentId = `subagent-deny-ceiling-${Date.now()}-${Math.random().toString(16).slice(2)}`;
457+
const sessionKey = `agent:${agentId}:subagent:denied`;
458+
const storePath = createSessionStorePath("openclaw-subagent-deny-ceiling", agentId);
459+
await writeSessionEntries(storePath, {
460+
[sessionKey]: {
461+
sessionId: "denied-session",
462+
updatedAt: Date.now(),
463+
spawnDepth: 1,
464+
subagentRole: "orchestrator",
465+
subagentControlScope: "children",
466+
// Parent had exact deny — child must also have this ceiling
467+
inheritedSenderPolicy: { deny: ["exec", "fs_read"] },
468+
},
469+
});
470+
471+
const filtered = applyFinalPolicy({
472+
bundledTools: [
473+
makeTool("exec"),
474+
makeTool("fs_read"),
475+
makeTool("fs_write"),
476+
makeTool("message"),
477+
],
478+
config: {
479+
session: { store: storePath },
480+
tools: {
481+
toolsBySender: {
482+
"e164:+1234567890": { deny: ["exec", "fs_read"] },
483+
"*": { allow: ["*"] },
484+
},
485+
},
486+
},
487+
sessionKey,
488+
sandboxSessionKey: sessionKey,
489+
spawnedBy: "parent",
490+
messageProvider: "whatsapp",
491+
warn: () => {},
492+
});
493+
494+
// Subagent loses exec and fs_read — parent's deny ceiling preserved
495+
expect(filtered.map((t) => t.name)).toEqual(expect.arrayContaining(["fs_write", "message"]));
496+
expect(filtered.map((t) => t.name)).not.toContain("exec");
497+
expect(filtered.map((t) => t.name)).not.toContain("fs_read");
498+
});
427499
});

‎src/agents/openclaw-tools.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,7 @@ export function createOpenClawTools(
598598
workspaceDir: spawnWorkspaceDir,
599599
inheritedToolAllowlist: options?.inheritedToolAllowlist,
600600
inheritedToolDenylist: options?.inheritedToolDenylist,
601+
inheritedSenderPolicy: options?.inheritedSenderPolicy,
601602
}),
602603
]
603604
: []),

‎src/agents/spawned-context.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { normalizeOptionalString } from "@openclaw/normalization-core/string-coe
77
import type { OpenClawConfig } from "../config/types.openclaw.js";
88
import { normalizeAgentId, parseAgentSessionKey } from "../routing/session-key.js";
99
import { resolveAgentWorkspaceDir } from "./agent-scope.js";
10+
import type { SandboxToolPolicy } from "./sandbox/types.js";
1011

1112
export type SpawnedRunMetadata = {
1213
spawnedBy?: string | null;
@@ -24,6 +25,8 @@ export type SpawnedToolContext = {
2425
workspaceDir?: string;
2526
inheritedToolAllowlist?: string[];
2627
inheritedToolDenylist?: string[];
28+
/** Parent's resolved senderPolicy carried to the spawned child. */
29+
inheritedSenderPolicy?: SandboxToolPolicy;
2730
};
2831

2932
type NormalizedSpawnedRunMetadata = {

‎src/agents/subagent-capabilities.ts‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
normalizeInheritedToolAllowlist,
2525
normalizeInheritedToolDenylist,
2626
} from "./inherited-tool-deny.js";
27+
import type { SandboxToolPolicy } from "./sandbox/types.js";
2728
import { getSubagentDepthFromSessionStore } from "./subagent-depth.js";
2829

2930
/** Resolved role for a main session, orchestrating subagent, or leaf subagent. */
@@ -45,6 +46,7 @@ type SessionCapabilityEntry = {
4546
spawnedBy?: unknown;
4647
inheritedToolAllow?: unknown;
4748
inheritedToolDeny?: unknown;
49+
inheritedSenderPolicy?: unknown;
4850
};
4951

5052
/** Minimal persisted session-store shape needed to resolve subagent capabilities. */
@@ -58,6 +60,7 @@ export type SessionCapabilityStore = Record<
5860
spawnedBy?: unknown;
5961
inheritedToolAllow?: unknown;
6062
inheritedToolDeny?: unknown;
63+
inheritedSenderPolicy?: unknown;
6164
}
6265
>;
6366

@@ -383,3 +386,39 @@ export function resolveStoredSubagentInheritedToolAllowlist(
383386
});
384387
return normalizeInheritedToolAllowlist(entry?.inheritedToolAllow);
385388
}
389+
390+
/**
391+
* Resolve a stored parent sender policy inherited by a subagent session.
392+
* Returns the parent's resolved senderPolicy, or undefined if none was stored.
393+
*/
394+
export function resolveStoredSenderPolicy(
395+
sessionKey: string | undefined | null,
396+
opts?: {
397+
cfg?: OpenClawConfig;
398+
store?: SessionCapabilityStore;
399+
},
400+
): SandboxToolPolicy | undefined {
401+
const normalizedSessionKey = normalizeOptionalString(sessionKey);
402+
if (!normalizedSessionKey || !shouldInspectStoredSubagentEnvelope(normalizedSessionKey)) {
403+
return undefined;
404+
}
405+
const store = resolveSubagentCapabilityStore(normalizedSessionKey, opts);
406+
const entry = resolveSessionCapabilityEntry({
407+
sessionKey: normalizedSessionKey,
408+
cfg: opts?.cfg,
409+
store,
410+
});
411+
const raw = entry?.inheritedSenderPolicy;
412+
if (!raw || typeof raw !== "object") {
413+
return undefined;
414+
}
415+
const policy = raw as Record<string, unknown>;
416+
const result: SandboxToolPolicy = {};
417+
if (Array.isArray(policy.allow)) {
418+
result.allow = policy.allow as string[];
419+
}
420+
if (Array.isArray(policy.deny)) {
421+
result.deny = policy.deny as string[];
422+
}
423+
return result.allow || result.deny ? result : undefined;
424+
}

‎src/agents/subagent-spawn.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
resolveThreadBindingSpawnPolicy,
2929
} from "../channels/thread-bindings-policy.js";
3030
import type { SessionEntry } from "../config/sessions/types.js";
31+
import type { SandboxToolPolicy } from "./sandbox/types.js";
3132
import type { OpenClawConfig } from "../config/types.openclaw.js";
3233
import type { SubagentSpawnPreparation } from "../context-engine/types.js";
3334
import { stringifyRouteThreadId } from "../plugin-sdk/channel-route.js";
@@ -183,6 +184,8 @@ type SpawnSubagentContext = {
183184
workspaceDir?: string;
184185
inheritedToolAllowlist?: string[];
185186
inheritedToolDenylist?: string[];
187+
/** Parent's resolved senderPolicy carried to the spawned child session. */
188+
inheritedSenderPolicy?: SandboxToolPolicy;
186189
};
187190

188191
type SpawnSubagentResult = {
@@ -313,6 +316,9 @@ function buildDirectChildSessionPatch(patch: Record<string, unknown>): Partial<S
313316
if (inheritedToolAllow.length > 0) {
314317
entry.inheritedToolAllow = inheritedToolAllow;
315318
}
319+
if (patch.inheritedSenderPolicy && typeof patch.inheritedSenderPolicy === "object") {
320+
entry.inheritedSenderPolicy = patch.inheritedSenderPolicy;
321+
}
316322
if (typeof patch.thinkingLevel === "string" && patch.thinkingLevel.trim()) {
317323
entry.thinkingLevel = patch.thinkingLevel.trim();
318324
}
@@ -1300,6 +1306,7 @@ export async function spawnSubagentDirect(
13001306
subagentControlScope: childCapabilities.controlScope,
13011307
...inheritedToolAllowPatch(ctx.inheritedToolAllowlist),
13021308
...inheritedToolDenyPatch(ctx.inheritedToolDenylist),
1309+
...(ctx.inheritedSenderPolicy ? { inheritedSenderPolicy: ctx.inheritedSenderPolicy } : {}),
13031310
...plan.initialSessionPatch,
13041311
};
13051312

‎src/agents/tools/sessions-spawn-tool.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,7 @@ export function createSessionsSpawnTool(
394394
sandboxed: opts?.sandboxed,
395395
inheritedToolAllowlist: opts?.inheritedToolAllowlist,
396396
inheritedToolDenylist: opts?.inheritedToolDenylist,
397+
inheritedSenderPolicy: opts?.inheritedSenderPolicy,
397398
},
398399
);
399400
const childSessionKey = result.childSessionKey?.trim();
@@ -491,6 +492,7 @@ export function createSessionsSpawnTool(
491492
workspaceDir: opts?.workspaceDir,
492493
inheritedToolAllowlist: opts?.inheritedToolAllowlist,
493494
inheritedToolDenylist: opts?.inheritedToolDenylist,
495+
inheritedSenderPolicy: opts?.inheritedSenderPolicy,
494496
},
495497
);
496498

‎src/config/sessions/types.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,8 @@ export type SessionEntry = SessionRestartRecoveryState &
292292
inheritedToolDeny?: string[];
293293
/** Session-scoped tool allow entries inherited from the caller that created this session. */
294294
inheritedToolAllow?: string[];
295+
/** Parent's resolved sender policy carried to a spawned child session. */
296+
inheritedSenderPolicy?: unknown;
295297
systemSent?: boolean;
296298
abortedLastRun?: boolean;
297299
/** Interrupted run generations whose late lifecycle events must be ignored. */

0 commit comments

Comments
 (0)