laverdet / isolated-vm

Secure & isolated JS environments for nodejs
ISC License
2.18k stars 154 forks source link

Passing Object as a reference to the isolated VM and expecting the properties of the original object should also be modified. #480

Closed srtk1996 closed 4 months ago

srtk1996 commented 4 months ago

I am Passing Object as a reference to the isolated VM and expecting the properties of the original object should also be modified by the isolated vm. But the original object is not getting modified. P.S :- I don't want to return the modified object I just want to achieve the functionality of modifying the reference to that object only. As other functionality of my code will be affected.

please find a dummy snippet

const isolate = new ivm.Isolate(); const context = await isolate.createContext(); let stringifiedFunc =(function(){ let x = fn_param_obj; x.price=200; })(); // Converting to an IIFE let fn_param_obj = {'price':100} let referenct_to_fn_param_obj = new ivm.Reference(fn_param_obj) await context.global.set('fn_param_obj', referenct_to_fn_param_obj); const script = await isolate.compileScript(stringifiedFunc); await script.run(context,{ timeout: vm2Config.timeout, copy : true }); console.log("expecting the fn_param_obj price property also set to 200",fn_param_obj)

laverdet commented 4 months ago

You've set price on the Reference delegate object itself, not the underlying object. Please read the "frequently asked question" on the readme.