Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot register ts-node into sandboxed NodeVM instance due to TypeError: 'set' on proxy: trap returned falsish #443

Open
benjamincburns opened this issue Jun 29, 2022 · 4 comments

Comments

@benjamincburns
Copy link

benjamincburns commented Jun 29, 2022

When I attempt to register ts-node into an instance of NodeVM with options.require.context = "sandbox", I observe the following error. I believe this is likely because ts-node is trying to modify some global state that the sandbox has made read only?

TypeError: 'set' on proxy: trap returned falsish for property 'Symbol(ts-node.register.instance)'
    at Object.register (/Users/bburns/projects/petshop/node_modules/ts-node/src/index.ts:586:29)
    at /Users/bburns/projects/petshop/node_modules/ts-node/register/index.js:1:78
    at BaseHandler.apply (/Users/bburns/projects/truffle/node_modules/vm2/lib/bridge.js:479:11)
    at NonStrictNodeVM.run (/Users/bburns/projects/truffle/node_modules/vm2/lib/nodevm.js:425:23)
    at NonStrictNodeVM.run (/Users/bburns/projects/truffle/packages/require/lib/index.ts:183:18)
    at DefaultResolver.loadJS (/Users/bburns/projects/truffle/node_modules/vm2/lib/resolver.js:203:7)
    at loadJS (/Users/bburns/projects/truffle/node_modules/vm2/lib/nodevm.js:248:46)
    at BaseHandler.apply (/Users/bburns/projects/truffle/node_modules/vm2/lib/bridge.js:479:11)
    at Module._extensions.<computed> (/Users/bburns/projects/truffle/node_modules/vm2/lib/setup-node-sandbox.js:144:48)
    at Proxy.requireImpl (/Users/bburns/projects/truffle/node_modules/vm2/lib/setup-node-sandbox.js:117:3)

Note that I'm using the NonStrictNodeVM workaround from #442.

@benjamincburns benjamincburns changed the title Cannot register ts-node in sandbox mode Cannot register ts-node into sandboxed NodeVM instance due to TypeError: 'set' on proxy: trap returned falsish Jun 29, 2022
@benjamincburns
Copy link
Author

As a workaround I did something like the following (actual code differs, as I'm also working around some path issues).

Important security note: I'm not using sandbox mode for security purposes. I'm just using it to make sure that transitive dependencies of the script that I'm executing in the VM are loaded with the same global context as the script that I'm executing, rather than the host's global context.

const tsNodePath = resolveInUserScriptPath(); // function not included for brevity

const compiler = require(tsNodePath).create().compile;

// see #442 comments for implementation of NonStrictNodeVM
const vm = new NonStrictNodeVM({
  allowAsync: true,
  console: "inherit",
  require: {
    builtin: [ "*" ],
    context: "sandbox",
    external: true
  },
  sandbox: { /* globals go here */ },
  compiler
})

@XmiliaH
Copy link
Collaborator

XmiliaH commented Jul 3, 2022

This is likely caused by the ts-node module trying to write to properties from a builtin module which are exposed in the sandbox as read-only.

@benjamincburns
Copy link
Author

Is there any way to allow some built-in properties to be settable?

@behnammodi
Copy link

I have the same problem, is there any update?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants