@@ -17,6 +17,7 @@ import type { HookRunner } from "../plugins/hooks.js";
1717import { createEmptyPluginRegistry } from "../plugins/registry-empty.js" ;
1818import { setActivePluginRegistry } from "../plugins/runtime.js" ;
1919import { PluginApprovalResolutions } from "../plugins/types.js" ;
20+ import { resolveBeforeToolCallApprovalOutcome } from "./agent-tools.before-tool-call.approval.js" ;
2021import { runBeforeToolCallHook } from "./agent-tools.before-tool-call.js" ;
2122import { callGatewayTool } from "./tools/gateway.js" ;
2223
@@ -811,3 +812,81 @@ describe("runBeforeToolCallHook — embedded mode approvals", () => {
811812 }
812813 } ) ;
813814} ) ;
815+
816+ describe ( "before_tool_call approval snapshots" , ( ) => {
817+ it ( "detaches deferred approval params from mutable hook and caller objects" , async ( ) => {
818+ const baseParams = { command : "safe" , options : { cwd : "/safe" } } ;
819+ const overrideParams = { env : { MODE : "safe" } } ;
820+
821+ const outcome = await resolveBeforeToolCallApprovalOutcome ( {
822+ result : {
823+ requireApproval : {
824+ pluginId : "policy" ,
825+ title : "Needs approval" ,
826+ description : "Approval needed" ,
827+ } ,
828+ params : overrideParams ,
829+ } ,
830+ approvalMode : "defer" ,
831+ toolName : "bash" ,
832+ baseParams,
833+ } ) ;
834+
835+ baseParams . options . cwd = "/unapproved" ;
836+ overrideParams . env . MODE = "unapproved" ;
837+
838+ expect ( outcome ) . toMatchObject ( {
839+ blocked : false ,
840+ params : { command : "safe" , options : { cwd : "/safe" } } ,
841+ deferredApproval : {
842+ baseParams : { command : "safe" , options : { cwd : "/safe" } } ,
843+ overrideParams : { env : { MODE : "safe" } } ,
844+ } ,
845+ } ) ;
846+ if ( ! outcome || outcome . blocked || ! outcome . deferredApproval ) {
847+ throw new Error ( "expected deferred approval outcome" ) ;
848+ }
849+ ( outcome . params as typeof baseParams ) . options . cwd = "/outcome-mutated" ;
850+ expect ( outcome . deferredApproval . baseParams ) . toEqual ( {
851+ command : "safe" ,
852+ options : { cwd : "/safe" } ,
853+ } ) ;
854+ } ) ;
855+
856+ const sharedMemoryCases : Array <
857+ [
858+ string ,
859+ {
860+ baseParams : Record < string , unknown > ;
861+ overrideParams ?: Record < string , unknown > ;
862+ } ,
863+ ]
864+ > = [
865+ [ "base params" , { baseParams : { shared : new Uint8Array ( new SharedArrayBuffer ( 4 ) ) } } ] ,
866+ [
867+ "override params" ,
868+ {
869+ baseParams : { command : "safe" } ,
870+ overrideParams : { shared : new Uint8Array ( new SharedArrayBuffer ( 4 ) ) } ,
871+ } ,
872+ ] ,
873+ ] ;
874+
875+ it . each ( sharedMemoryCases ) ( "rejects shared memory in %s" , async ( _name , values ) => {
876+ await expect (
877+ resolveBeforeToolCallApprovalOutcome ( {
878+ result : {
879+ requireApproval : {
880+ pluginId : "policy" ,
881+ title : "Needs approval" ,
882+ description : "Approval needed" ,
883+ } ,
884+ params : values . overrideParams ,
885+ } ,
886+ approvalMode : "defer" ,
887+ toolName : "bash" ,
888+ baseParams : values . baseParams ,
889+ } ) ,
890+ ) . rejects . toThrow ( "before_tool_call mutable input isolation failed" ) ;
891+ } ) ;
892+ } ) ;
0 commit comments