From 26c4c51d5e466fa1086adc9ef86b8b51a31c3587 Mon Sep 17 00:00:00 2001 From: Tom Schuster Date: Tue, 23 Mar 2021 11:15:11 +0000 Subject: [PATCH] Bug 1536094 - Support dynamic import from content scripts (sandboxed code) r=smaug,jonco Firstly we need to find a usable ScriptLoader for code in the content script sandbox, for that we use the normal ScriptLoader associated with DOMWindow wrapped by the sandbox. Secondly we need to execute the module in the global of the sandbox instead of the "ScriptGlobal" the ScriptLoader is actually associated with. The main behavior change here comes from using xpc::NativeGlobal in HostImportModuleDynamically and passing that global around inside ScriptFetchOptions. To ensure that content-scripts and the webpage don't share imported modules, the module map (mFetchingModules and mFetchedModules) now uses a complex key of . The Global is a nullptr for normal imports from a webpage. Differential Revision: https://phabricator.services.mozilla.com/D107076 --- dom/events/EventListenerManager.cpp | 2 +- dom/locales/en-US/chrome/dom/dom.properties | 1 + dom/script/ModuleLoadRequest.cpp | 2 +- dom/script/ModuleLoadRequest.h | 2 +- dom/script/ModuleMapKey.cpp | 38 ++++ dom/script/ModuleMapKey.h | 38 ++++ dom/script/ScriptLoadRequest.cpp | 10 +- dom/script/ScriptLoadRequest.h | 14 +- dom/script/ScriptLoader.cpp | 204 ++++++++++++++------ dom/script/ScriptLoader.h | 25 ++- dom/script/moz.build | 2 + js/xpconnect/src/Sandbox.cpp | 5 + js/xpconnect/src/xpcpublic.h | 1 + xpcom/base/ErrorList.py | 3 + 14 files changed, 278 insertions(+), 69 deletions(-) create mode 100644 dom/script/ModuleMapKey.cpp create mode 100644 dom/script/ModuleMapKey.h diff --git a/dom/events/EventListenerManager.cpp b/dom/events/EventListenerManager.cpp index d9cd6176e96e..8c0dee30ce1f 100644 --- a/dom/events/EventListenerManager.cpp +++ b/dom/events/EventListenerManager.cpp @@ -1027,7 +1027,7 @@ nsresult EventListenerManager::CompileEventHandlerInternal( RefPtr fetchOptions = new ScriptFetchOptions( CORS_NONE, aElement->OwnerDoc()->GetReferrerPolicy(), aElement, - aElement->OwnerDoc()->NodePrincipal()); + aElement->OwnerDoc()->NodePrincipal(), nullptr); RefPtr eventScript = new EventScript(fetchOptions, uri); diff --git a/dom/locales/en-US/chrome/dom/dom.properties b/dom/locales/en-US/chrome/dom/dom.properties index 3ef8881e3f00..67f5fe84a5bd 100644 --- a/dom/locales/en-US/chrome/dom/dom.properties +++ b/dom/locales/en-US/chrome/dom/dom.properties @@ -317,6 +317,7 @@ ModuleSourceMalformed=Module source URI is malformed: “%S”. # LOCALIZATION NOTE: Do not translate "