Skip to content

Commit

Permalink
Bug 1778289 - Part 9: Bail out early if adding an import map is disal…
Browse files Browse the repository at this point in the history
…lowed. r=jonco,yulia

If an adding import map is disallowed, we bail out early to prevent creating a
ScriptLoadRequest.

Differential Revision: https://phabricator.services.mozilla.com/D152876
  • Loading branch information
allstarschh committed Oct 7, 2022
1 parent bfdfa39 commit 61951dd
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions dom/script/ScriptLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1122,6 +1122,23 @@ bool ScriptLoader::ProcessInlineScript(nsIScriptElement* aElement,
return false;
}
// Check if adding an import map script is allowed. If not, we bail out
// early to prevent creating a load request.
if (aScriptKind == ScriptKind::eImportMap) {
// https://whatpr.org/html/8075/scripting.html#prepare-the-script-element
// Step 31.2 type is "importmap":
// Step 1. If el's relevant global object's import maps allowed is false,
// then queue an element task on the DOM manipulation task source given el
// to fire an event named error at el, and return.
if (!mModuleLoader->IsImportMapAllowed()) {
NS_WARNING("ScriptLoader: import maps allowed is false.");
NS_DispatchToCurrentThread(
NewRunnableMethod("nsIScriptElement::FireErrorEvent", aElement,
&nsIScriptElement::FireErrorEvent));
return false;
}
}
// Inline classic scripts ignore their CORS mode and are always CORS_NONE.
CORSMode corsMode = CORS_NONE;
if (aScriptKind == ScriptKind::eModule) {
Expand Down Expand Up @@ -1189,16 +1206,8 @@ bool ScriptLoader::ProcessInlineScript(nsIScriptElement* aElement,
if (request->IsImportMapRequest()) {
// https://whatpr.org/html/8075/scripting.html#prepare-the-script-element
// Step 31.2 type is "importmap":
// Step 1. If el's relevant global object's import maps allowed is false,
// then queue an element task on the DOM manipulation task source given el
// to fire an event named error at el, and return.
if (!mModuleLoader->IsImportMapAllowed()) {
NS_WARNING("ScriptLoader: import maps allowed is false.");
NS_DispatchToCurrentThread(
NewRunnableMethod("nsIScriptElement::FireErrorEvent", aElement,
&nsIScriptElement::FireErrorEvent));
return false;
}
// Impl note: Step 1 is done above before creating a ScriptLoadRequest.
MOZ_ASSERT(mModuleLoader->IsImportMapAllowed());

// Step 2. Set el's relevant global object's import maps allowed to false.
mModuleLoader->DisallowImportMaps();
Expand Down

0 comments on commit 61951dd

Please sign in to comment.