tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

commit 19e9ef1bce3ed49c583c7ec0ea647d7aef794aaa
parent 04a9348e2e941256fb9da68a766b08b5a00c52f8
Author: Harveer Singh <hsingh@mozilla.com>
Date:   Fri, 14 Nov 2025 15:53:26 +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:
Mdom/workers/loader/WorkerModuleLoader.cpp | 5+++++
Mdom/workers/loader/WorkerModuleLoader.h | 2++
Mjs/loader/ModuleLoaderBase.cpp | 16+++++++++++++++-
Mjs/loader/ModuleLoaderBase.h | 2++
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 @@ -1478,7 +1478,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( @@ -1554,6 +1561,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) {}