commit a9ff68075ff9de39b25b04b8fdcc5b8fb197318d
parent 92013428c4f8c2afd0cb7c77cb4bf3ba44749c50
Author: Harveer Singh <hsingh@mozilla.com>
Date: Mon, 3 Nov 2025 22:49:22 +0000
Bug 1995558: Service worker module registration should be rejected in case of any evaluation errors.r=edenchuang,dom-worker-reviewers
Differential Revision: https://phabricator.services.mozilla.com/D269457
Diffstat:
4 files changed, 24 insertions(+), 1 deletion(-)
diff --git a/dom/workers/loader/WorkerModuleLoader.cpp b/dom/workers/loader/WorkerModuleLoader.cpp
@@ -128,6 +128,11 @@ bool WorkerModuleLoader::IsDynamicImportSupported() {
return !workerPrivate->IsServiceWorker();
}
+bool WorkerModuleLoader::IsForServiceWorker() const {
+ WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate();
+ return workerPrivate && workerPrivate->IsServiceWorker();
+}
+
bool WorkerModuleLoader::CanStartLoad(ModuleLoadRequest* aRequest,
nsresult* aRvOut) {
return true;
diff --git a/dom/workers/loader/WorkerModuleLoader.h b/dom/workers/loader/WorkerModuleLoader.h
@@ -98,6 +98,8 @@ class WorkerModuleLoader : public JS::loader::ModuleLoaderBase {
return aModuleType == JS::ModuleType::JavaScript ||
aModuleType == JS::ModuleType::JSON;
}
+
+ virtual bool IsForServiceWorker() const override;
};
} // namespace mozilla::dom::workerinternals::loader
diff --git a/js/loader/ModuleLoaderBase.cpp b/js/loader/ModuleLoaderBase.cpp
@@ -1474,7 +1474,14 @@ nsresult ModuleLoaderBase::EvaluateModule(ModuleLoadRequest* aRequest) {
mozilla::dom::AutoEntryScript aes(mGlobalObject, "EvaluateModule",
NS_IsMainThread());
- return EvaluateModuleInContext(aes.cx(), aRequest, ReportModuleErrorsAsync);
+ // We would like to handle any evaluation errors synchronously for service
+ // workers immediately such that if we are performing service worker
+ // registration we can fail it right away rather than having it fail later on
+ // an event loop turn which might be too late and the registration process
+ // would have succeeded by then.
+ return EvaluateModuleInContext(
+ aes.cx(), aRequest,
+ IsForServiceWorker() ? ThrowModuleErrorsSync : ReportModuleErrorsAsync);
}
nsresult ModuleLoaderBase::EvaluateModuleInContext(
@@ -1550,6 +1557,13 @@ nsresult ModuleLoaderBase::EvaluateModuleInContext(
// If the promise is rejected, the value is unwrapped from the promise value.
if (!ThrowOnModuleEvaluationFailure(aCx, evaluationPromise, errorBehaviour)) {
LOG(("ScriptLoadRequest (%p): evaluation failed on throw", aRequest));
+
+ if (IsForServiceWorker()) {
+ // If this module eval is being done for service workers, then we would
+ // like to throw/return right from here, such that if we are in
+ // registration phase, we can fail it synchronously right away.
+ return NS_ERROR_ABORT;
+ }
}
// TODO: Bug 1973321: Prepare Bytecode encoding for dynamic import
diff --git a/js/loader/ModuleLoaderBase.h b/js/loader/ModuleLoaderBase.h
@@ -333,6 +333,8 @@ class ModuleLoaderBase : public nsISupports {
virtual bool IsDynamicImportSupported() { return true; }
+ virtual bool IsForServiceWorker() const { return false; }
+
// Called when dynamic import started successfully.
virtual void OnDynamicImportStarted(ModuleLoadRequest* aRequest) {}