Bug 1901482: Fix failed requests being kept in mDynamicImportRequests. r=jonco

In StartDynamicImport, if StartModuleLoad failed, the request would be
kept in mDynamicImportRequests until shutdown.
This fixes the problem by removing it from mDynamicImportRequests once
StartModuleLoad fails.

Differential Revision: https://phabricator.services.mozilla.com/D213016
This commit is contained in:
Yoshi Cheng-Hao Huang 2024-06-10 17:48:48 +00:00
parent feb8e707db
commit d9196c184b
3 changed files with 32 additions and 0 deletions

View File

@ -45,6 +45,7 @@ support-files = [
["test_importMap_with_external_script.html"]
["test_importMap_with_nonexisting_module.html"]
["test_invalid_dynamic_import.html"]
["test_dynamic_importMap_with_external_script.html"]
["test_dynamic_importMap_load_completes.html"]
["test_shared_submodules_with_modulepreload.html"]

View File

@ -0,0 +1,23 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Security-Policy" content="script-src 'self' 'unsafe-inline';">
</head>
<title>Test an invalid dynamic import</title>
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<script>
SimpleTest.waitForExplicitFinish();
function testLoaded() {
import("data:text/javascript,void 0").then(() => {
ok(false, "data URL shouldn't be loaded");
}).catch((e) => {
ok(true, "data URL should be blocked");
}).finally(() => {
SimpleTest.finish();
});
}
</script>
<body onload='testLoaded()'></body>
</html>

View File

@ -969,6 +969,7 @@ nsresult ModuleLoaderBase::StartDynamicImport(ModuleLoadRequest* aRequest) {
nsresult rv = StartModuleLoad(aRequest);
if (NS_FAILED(rv)) {
mLoader->ReportErrorToConsole(aRequest, rv);
RemoveDynamicImport(aRequest);
FinishDynamicImportAndReject(aRequest, rv);
}
return rv;
@ -1004,6 +1005,9 @@ void ModuleLoaderBase::FinishDynamicImport(
// be handled by rejecting the dynamic module import promise in the JSAPI.
MOZ_ASSERT_IF(NS_FAILED(aResult), !aEvaluationPromise);
// The request should been removed from mDynamicImportRequests.
MOZ_ASSERT(!aRequest->mLoader->HasDynamicImport(aRequest));
// Complete the dynamic import, report failures indicated by aResult or as a
// pending exception on the context.
@ -1090,6 +1094,10 @@ void ModuleLoaderBase::CancelDynamicImport(ModuleLoadRequest* aRequest,
RefPtr<ScriptLoadRequest> req = mDynamicImportRequests.Steal(aRequest);
if (!aRequest->IsCanceled()) {
// If the mDynamicPromise has been cleared, then it should be remove from
// mDynamicImportRequests as well.
MOZ_ASSERT(aRequest->mDynamicPromise);
aRequest->Cancel();
// FinishDynamicImport must happen exactly once for each dynamic import
// request. If the load is aborted we do it when we remove the request