diff --git a/dom/workers/ScriptLoader.cpp b/dom/workers/ScriptLoader.cpp index bdc4efed23..758c1660e7 100644 --- a/dom/workers/ScriptLoader.cpp +++ b/dom/workers/ScriptLoader.cpp @@ -1202,7 +1202,7 @@ private: // load group's appId and browser element flag. MOZ_ASSERT(NS_LoadGroupMatchesPrincipal(channelLoadGroup, channelPrincipal)); - mWorkerPrivate->SetPrincipal(channelPrincipal, channelLoadGroup); + mWorkerPrivate->SetPrincipalOnMainThread(channelPrincipal, channelLoadGroup); // We did inherit CSP in bug 1223647. If we do not already have a CSP, we // should get it from the HTTP headers on the worker script. @@ -1290,7 +1290,14 @@ private: #endif mWorkerPrivate->InitChannelInfo(aChannelInfo); - mWorkerPrivate->SetPrincipal(responsePrincipal, loadGroup); + + // Override the principal on the WorkerPrivate. We just asserted that + // this is the same as our current WorkerPrivate principal, so this is + // almost a no-op. We must do, it though, in order to avoid accidentally + // propagating the CSP object back to the ServiceWorkerRegistration + // principal. If bug 965637 is fixed then this can be removed. + rv = mWorkerPrivate->SetPrincipalOnMainThread(responsePrincipal, loadGroup); + MOZ_DIAGNOSTIC_ASSERT(NS_SUCCEEDED(rv)); rv = mWorkerPrivate->SetCSPFromHeaderValues(aCSPHeaderValue, aCSPReportOnlyHeaderValue); diff --git a/dom/workers/ServiceWorkerPrivate.cpp b/dom/workers/ServiceWorkerPrivate.cpp index c66d98b0ea..f30ae67a19 100644 --- a/dom/workers/ServiceWorkerPrivate.cpp +++ b/dom/workers/ServiceWorkerPrivate.cpp @@ -1779,6 +1779,11 @@ ServiceWorkerPrivate::SpawnWorkerIfNeeded(WakeUpReason aWhy, WorkerPrivate::OverrideLoadInfoLoadGroup(info); + rv = info.SetPrincipalOnMainThread(info.mPrincipal, info.mLoadGroup); + if (NS_WARN_IF(NS_FAILED(rv))) { + return rv; + } + AutoJSAPI jsapi; jsapi.Init(); ErrorResult error; diff --git a/dom/workers/WorkerPrivate.cpp b/dom/workers/WorkerPrivate.cpp index 01a8368c81..7e96fc6ad3 100644 --- a/dom/workers/WorkerPrivate.cpp +++ b/dom/workers/WorkerPrivate.cpp @@ -3679,47 +3679,53 @@ WorkerPrivateParent::SetBaseURI(nsIURI* aBaseURI) nsContentUtils::GetUTFOrigin(aBaseURI, mLocationInfo.mOrigin); } -template -void -WorkerPrivateParent::SetPrincipal(nsIPrincipal* aPrincipal, - nsILoadGroup* aLoadGroup) +nsresult +WorkerLoadInfo::SetPrincipalOnMainThread(nsIPrincipal* aPrincipal, + nsILoadGroup* aLoadGroup) { AssertIsOnMainThread(); MOZ_ASSERT(NS_LoadGroupMatchesPrincipal(aLoadGroup, aPrincipal)); - MOZ_ASSERT(!mLoadInfo.mPrincipalInfo); - mLoadInfo.mPrincipal = aPrincipal; - mLoadInfo.mPrincipalIsSystem = nsContentUtils::IsSystemPrincipal(aPrincipal); + mPrincipal = aPrincipal; + mPrincipalIsSystem = nsContentUtils::IsSystemPrincipal(aPrincipal); - aPrincipal->GetCsp(getter_AddRefs(mLoadInfo.mCSP)); + nsresult rv = aPrincipal->GetCsp(getter_AddRefs(mCSP)); + NS_ENSURE_SUCCESS(rv, rv); - if (mLoadInfo.mCSP) { - mLoadInfo.mCSP->GetAllowsEval(&mLoadInfo.mReportCSPViolations, - &mLoadInfo.mEvalAllowed); + if (mCSP) { + mCSP->GetAllowsEval(&mReportCSPViolations, &mEvalAllowed); // Set ReferrerPolicy bool hasReferrerPolicy = false; - uint32_t rp = mozilla::net::RP_Default; + uint32_t rp = mozilla::net::RP_Unset; - nsresult rv = mLoadInfo.mCSP->GetReferrerPolicy(&rp, &hasReferrerPolicy); - NS_ENSURE_SUCCESS_VOID(rv); + rv = mCSP->GetReferrerPolicy(&rp, &hasReferrerPolicy); + NS_ENSURE_SUCCESS(rv, rv); if (hasReferrerPolicy) { - mLoadInfo.mReferrerPolicy = static_cast(rp); + mReferrerPolicy = static_cast(rp); } } else { - mLoadInfo.mEvalAllowed = true; - mLoadInfo.mReportCSPViolations = false; + mEvalAllowed = true; + mReportCSPViolations = false; } - mLoadInfo.mLoadGroup = aLoadGroup; + mLoadGroup = aLoadGroup; - mLoadInfo.mPrincipalInfo = new PrincipalInfo(); - mLoadInfo.mOriginAttributes = nsContentUtils::GetOriginAttributes(aLoadGroup); + mPrincipalInfo = new PrincipalInfo(); + mOriginAttributes = nsContentUtils::GetOriginAttributes(aLoadGroup); - nsContentUtils::GetUTFOrigin(aPrincipal, mLoadInfo.mOrigin); + rv = PrincipalToPrincipalInfo(aPrincipal, mPrincipalInfo); + NS_ENSURE_SUCCESS(rv, rv); - MOZ_ALWAYS_SUCCEEDS( - PrincipalToPrincipalInfo(aPrincipal, mLoadInfo.mPrincipalInfo)); + return NS_OK; +} + +template +nsresult +WorkerPrivateParent::SetPrincipalOnMainThread(nsIPrincipal* aPrincipal, + nsILoadGroup* aLoadGroup) +{ + return mLoadInfo.SetPrincipalOnMainThread(aPrincipal, aLoadGroup); } template diff --git a/dom/workers/WorkerPrivate.h b/dom/workers/WorkerPrivate.h index b82e994028..1c9cb10ded 100644 --- a/dom/workers/WorkerPrivate.h +++ b/dom/workers/WorkerPrivate.h @@ -656,8 +656,8 @@ public: return mLoadInfo.mPrincipal; } - void - SetPrincipal(nsIPrincipal* aPrincipal, nsILoadGroup* aLoadGroup); + nsresult + SetPrincipalOnMainThread(nsIPrincipal* aPrincipal, nsILoadGroup* aLoadGroup); bool UsesSystemPrincipal() const diff --git a/dom/workers/Workers.h b/dom/workers/Workers.h index 6b0a0158d2..541dfed6aa 100644 --- a/dom/workers/Workers.h +++ b/dom/workers/Workers.h @@ -268,6 +268,8 @@ struct WorkerLoadInfo ~WorkerLoadInfo(); void StealFrom(WorkerLoadInfo& aOther); + + nsresult SetPrincipalOnMainThread(nsIPrincipal* aPrincipal, nsILoadGroup* aLoadGroup); }; // All of these are implemented in RuntimeService.cpp