@@ -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} ) ;
0 commit comments