Skip to content

Commit

Permalink
testharness.js: fix shadow realm detection (#33668)
Browse files Browse the repository at this point in the history
This commit fixes shadow realm detection to not falsely discover the
Deno main thread as a shadow realm. Because there is no reliable way to
discover shadow realms in isolation, we add a a special global to inform
testharness.js that it is running in a ShadowRealm.
  • Loading branch information
lucacasonato committed Apr 19, 2022
1 parent e53a56b commit ce3cb95
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 9 deletions.
1 change: 1 addition & 0 deletions cookie-store/serviceworker_cookieStore_cross_origin.js
@@ -1,6 +1,7 @@
self.GLOBAL = {
isWindow: () => false,
isWorker: () => false,
isShadowRealm: () => false,
};

self.addEventListener('message', async event => {
Expand Down
10 changes: 9 additions & 1 deletion resources/idlharness-shadowrealm.js
Expand Up @@ -29,7 +29,15 @@ function idl_test_shadowrealm(srcs, deps) {
promise_setup(async t => {
const realm = new ShadowRealm();
// https://github.com/web-platform-tests/wpt/issues/31996
realm.evaluate("globalThis.self = globalThis; undefined");
realm.evaluate("globalThis.self = globalThis; undefined;");

realm.evaluate(`
globalThis.self.GLOBAL = {
isWindow: function() { return false; },
isWorker: function() { return false; },
isShadowRealm: function() { return true; },
};
`);

const ss = await Promise.all(script_urls.map(url => fetch_text(url)));
for (const s of ss) {
Expand Down
10 changes: 4 additions & 6 deletions resources/testharness.js
Expand Up @@ -537,13 +537,11 @@
}
/* Shadow realm global objects are _ordinary_ objects (i.e. their prototype is
* Object) so we don't have a nice `instanceof` test to use; instead, we
* can look for the presence of web APIs that wouldn't be available in
* environments not listed above:
*
* As long as, within the shadow realm, we load the testharness before
* other libraries, this won't have any false positives, even in e.g. node
* check if the there is a GLOBAL.isShadowRealm() property
* on the global object. that was set by the test harness when it
* created the ShadowRealm.
*/
if ('AbortController' in global_scope) {
if (global_scope.GLOBAL.isShadowRealm()) {
return new ShadowRealmTestEnvironment();
}

Expand Down
8 changes: 6 additions & 2 deletions tools/serve/serve.py
Expand Up @@ -259,6 +259,7 @@ class AnyHtmlHandler(HtmlWrapperHandler):
self.GLOBAL = {
isWindow: function() { return true; },
isWorker: function() { return false; },
isShadowRealm: function() { return false; },
};
</script>
<script src="/resources/testharness.js"></script>
Expand Down Expand Up @@ -360,12 +361,13 @@ class ShadowRealmHandler(HtmlWrapperHandler):
await new Promise(r.evaluate(`
(resolve, reject) => {
(async () => {
await import("/resources/testharness.js");
%(script)s
globalThis.self.GLOBAL = {
isWindow: function() { return false; },
isWorker: function() { return false; },
isShadowRealm: function() { return true; },
};
await import("/resources/testharness.js");
%(script)s
await import("%(path)s");
})().then(resolve, (e) => reject(e.toString()));
}
Expand Down Expand Up @@ -411,6 +413,7 @@ class ClassicWorkerHandler(BaseWorkerHandler):
self.GLOBAL = {
isWindow: function() { return false; },
isWorker: function() { return true; },
isShadowRealm: function() { return false; },
};
importScripts("/resources/testharness.js");
%(script)s
Expand All @@ -428,6 +431,7 @@ class ModuleWorkerHandler(BaseWorkerHandler):
self.GLOBAL = {
isWindow: function() { return false; },
isWorker: function() { return true; },
isShadowRealm: function() { return false; },
};
import "/resources/testharness.js";
%(script)s
Expand Down
1 change: 1 addition & 0 deletions tools/wptserve/tests/functional/docroot/bar.any.worker.js
Expand Up @@ -2,6 +2,7 @@
self.GLOBAL = {
isWindow: function() { return false; },
isWorker: function() { return true; },
isShadowRealm: function() { return false; },
};
importScripts("/resources/testharness.js");

Expand Down
1 change: 1 addition & 0 deletions tools/wptserve/tests/functional/docroot/foo.any.html
Expand Up @@ -5,6 +5,7 @@
self.GLOBAL = {
isWindow: function() { return true; },
isWorker: function() { return false; },
isShadowRealm: function() { return false; },
};
</script>
<script src="/resources/testharness.js"></script>
Expand Down

0 comments on commit ce3cb95

Please sign in to comment.