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

[browsers] Impossible to observe unhandled rejection from within a sandbox #66

Open
caridy opened this issue Feb 6, 2020 · 2 comments

Comments

@caridy
Copy link
Contributor

caridy commented Feb 6, 2020

In browsers, the HostPromiseRejectionTracker depends on the identity of the Promise Intrinsic Object, which means there is no way to capture unhandled rejection produced by the Promise inside the sandbox, e.g.:

    const iframe = document.createElement('iframe');
    document.body.appendChild(iframe);
    const { contentWindow: { eval: iframeEval } } = iframe;
    // adding listeners
    window.addEventListener('error', e => console.error('onerror in outer window', e));
    window.addEventListener('unhandledrejection', e => console.error('captured onunhandledrejection in outer window with reason: ', e.reason));
    iframeEval(`
        window.addEventListener('error', e => console.error('onerror in iframe', e));
        window.addEventListener('unhandledrejection', e => console.error('captured onunhandledrejection in iframe with reason: ', e.reason));
    `);
    // trying Promise intrinsic object from iframe:
    iframeEval(`
        new Promise((resolve, reject) => {
            reject('rejection Promise intrinsic from iframe');
        });
    `);
    // trying Promise intrinsic object from outer realm:
    iframeEval(`
        new top.Promise((resolve, reject) => {
            reject('rejection Promise intrinsic from outer realm');
        });
    `);

From within the sandbox, when you do window.addEventListener('unhandledrejection') you are observing unhandled rejection from the outer realm, but that doesn't include those unhandled rejection from within the sandbox.

This seems to be a problem to be solved, it is not a security/leaking problem, but a capability problem.

@caridy caridy changed the title Impossible to observe handled rejection from within a sandbox [browsers] Impossible to observe handled rejection from within a sandbox Feb 6, 2020
@caridy caridy changed the title [browsers] Impossible to observe handled rejection from within a sandbox [browsers] Impossible to observe unhandled rejection from within a sandbox Feb 6, 2020
@caridy
Copy link
Contributor Author

caridy commented Feb 6, 2020

Just to clarify, this is not a problem in node where the unhandled rejection can be captured at the process level, e.g.:

// outer realm rejections
process.on('unhandledRejection', error => {
    // Will print "unhandledRejection err is not defined"
    console.log('unhandledRejection', error.message);
});

new Promise((_, reject) => reject(new Error('woops')));

// sandbox rejections
const util = require('util');
const vm = require('vm');

const script = new vm.Script(`new Promise((_, reject) => reject(new Error('woops')))`);
script.runInNewContext();

In the example above, both rejections will be captured by the process.on("unhandledRejection", ...) listener.

@caridy
Copy link
Contributor Author

caridy commented Feb 6, 2020

A related conversation happening here: tc39/proposal-shadowrealm#212

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

1 participant