@@ -462,6 +462,52 @@ describe("gateway server cron", () => {
462462 }
463463 } ) ;
464464
465+ test ( "rejects unsafe custom session ids on add and update" , async ( ) => {
466+ const { prevSkipCron } = await setupCronTestRun ( {
467+ tempPrefix : "openclaw-gw-cron-bad-session-target-" ,
468+ cronEnabled : false ,
469+ } ) ;
470+
471+ const { server, ws } = await startServerWithClient ( ) ;
472+ await connectOk ( ws ) ;
473+
474+ try {
475+ const addRes = await rpcReq ( ws , "cron.add" , {
476+ name : "bad custom session" ,
477+ enabled : true ,
478+ schedule : { kind : "every" , everyMs : 60_000 } ,
479+ sessionTarget : "session:../../outside" ,
480+ wakeMode : "now" ,
481+ payload : { kind : "agentTurn" , message : "hello" } ,
482+ } ) ;
483+ expect ( addRes . ok ) . toBe ( false ) ;
484+ expect ( addRes . error ?. message ) . toContain ( "invalid cron sessionTarget session id" ) ;
485+
486+ const validRes = await rpcReq ( ws , "cron.add" , {
487+ name : "good custom session" ,
488+ enabled : true ,
489+ schedule : { kind : "every" , everyMs : 60_000 } ,
490+ sessionTarget : "session:project-alpha:ops" ,
491+ wakeMode : "now" ,
492+ payload : { kind : "agentTurn" , message : "hello" } ,
493+ } ) ;
494+ expect ( validRes . ok ) . toBe ( true ) ;
495+ const jobId = ( validRes . payload as { id ?: unknown } | null ) ?. id ;
496+ expect ( typeof jobId ) . toBe ( "string" ) ;
497+
498+ const updateRes = await rpcReq ( ws , "cron.update" , {
499+ id : jobId ,
500+ patch : {
501+ sessionTarget : "session:..\\outside" ,
502+ } ,
503+ } ) ;
504+ expect ( updateRes . ok ) . toBe ( false ) ;
505+ expect ( updateRes . error ?. message ) . toContain ( "invalid cron sessionTarget session id" ) ;
506+ } finally {
507+ await cleanupCronTestRun ( { ws, server, prevSkipCron } ) ;
508+ }
509+ } ) ;
510+
465511 test ( "writes cron run history and auto-runs due jobs" , async ( ) => {
466512 const { prevSkipCron } = await setupCronTestRun ( {
467513 tempPrefix : "openclaw-gw-cron-log-" ,
@@ -559,6 +605,44 @@ describe("gateway server cron", () => {
559605 }
560606 } , 45_000 ) ;
561607
608+ test ( "fails closed for persisted unsafe custom session ids" , async ( ) => {
609+ const now = Date . now ( ) ;
610+ const { prevSkipCron } = await setupCronTestRun ( {
611+ tempPrefix : "openclaw-gw-cron-persisted-bad-session-target-" ,
612+ cronEnabled : false ,
613+ jobs : [
614+ {
615+ id : "bad-custom-session-job" ,
616+ name : "bad custom session job" ,
617+ enabled : true ,
618+ createdAtMs : now ,
619+ updatedAtMs : now ,
620+ schedule : { kind : "every" , everyMs : 60_000 } ,
621+ sessionTarget : "session:../../outside" ,
622+ wakeMode : "now" ,
623+ payload : { kind : "agentTurn" , message : "hello" } ,
624+ state : { } ,
625+ } ,
626+ ] ,
627+ } ) ;
628+
629+ cronIsolatedRun . mockClear ( ) ;
630+ const { server, ws } = await startServerWithClient ( ) ;
631+ await connectOk ( ws ) ;
632+
633+ try {
634+ const runRes = await rpcReq ( ws , "cron.run" , {
635+ id : "bad-custom-session-job" ,
636+ mode : "force" ,
637+ } ) ;
638+ expect ( runRes . ok ) . toBe ( false ) ;
639+ expect ( runRes . error ?. message ) . toContain ( "invalid cron sessionTarget session id" ) ;
640+ expect ( cronIsolatedRun ) . not . toHaveBeenCalled ( ) ;
641+ } finally {
642+ await cleanupCronTestRun ( { ws, server, prevSkipCron } ) ;
643+ }
644+ } ) ;
645+
562646 test ( "returns from cron.run immediately while isolated work continues in background" , async ( ) => {
563647 const { prevSkipCron } = await setupCronTestRun ( {
564648 tempPrefix : "openclaw-gw-cron-run-detached-" ,
0 commit comments