asvd / jailed

execute untrusted code with custom permissions
MIT License
1k stars 73 forks source link

Cannot read property 'whenEmitted' of undefined #54

Closed josh-hemphill closed 3 years ago

josh-hemphill commented 4 years ago

I'm trying to use the library in a vue.js project, and it seems that it keeps track of it's state in the Global this, which I think is what is creating this error somehow. If I break on the error, it shows this:

/**
     * Saves the provided function as a handler for the connection
     * success Whenable event
     * 
     * @param {Function} handler to be issued upon connection
     */
    DynamicPlugin.prototype.whenConnected = 
           Plugin.prototype.whenConnected = function(handler) {
        this._connect.whenEmitted(handler);
    }

Where this is the global this and has no property _connect.

Here is the trace

TypeError: Cannot read property 'whenEmitted' of undefined
    at DynamicPlugin.whenConnected.Plugin.whenConnected (jailed.js?c164:787)
    at eval (utils.js?a2b6:70)
    at new Promise (<anonymous>)
    at promisify (utils.js?a2b6:68)
    at _callee$ (evaluation.js?5b15:15)
    at tryCatch (runtime.js?96cf:45)
    at Generator.invoke [as _invoke] (runtime.js?96cf:274)
    at Generator.prototype.<computed> [as next] (runtime.js?96cf:97)
    at asyncGeneratorStep (asyncToGenerator.js?1da1:3)
    at _next (asyncToGenerator.js?1da1:25)

This is how I'm using it:

    const plugin = new jailed.DynamicPlugin(forCompile);
    plugin.whenFailed(x => console.log(x));
    await promisify(plugin.whenConnected);
    const result = await promisify(plugin.remote.evaluateAll);
    console.log(result);
    return { rows: result, evaluate: plugin.remote.evaluate };