From 64fdf9d70259c361639ed09aaaf1f2978cb8ceff Mon Sep 17 00:00:00 2001 From: Thomas Nguyen Date: Fri, 1 Apr 2016 11:52:28 +0800 Subject: [PATCH] Bug 1251378 - Use referrer policy of worker instead of default. r=ehsan MozReview-Commit-ID: I8X8nMkaS9V --- dom/fetch/FetchDriver.cpp | 12 +++++++----- dom/fetch/InternalRequest.cpp | 2 ++ dom/fetch/InternalRequest.h | 22 ++++++++++++++++++++++ dom/fetch/Request.cpp | 17 +++++++++++++++++ 4 files changed, 48 insertions(+), 5 deletions(-) diff --git a/dom/fetch/FetchDriver.cpp b/dom/fetch/FetchDriver.cpp index 90c6a583903c..b703ac44c880 100644 --- a/dom/fetch/FetchDriver.cpp +++ b/dom/fetch/FetchDriver.cpp @@ -252,11 +252,15 @@ FetchDriver::HttpFetch() // Step 2. Set the referrer. nsAutoString referrer; mRequest->GetReferrer(referrer); + + // The Referrer Policy in Request can be used to override a referrer policy + // associated with an environment settings object. + // If there's no Referrer Policy in the request, it should be inherited + // from environment. ReferrerPolicy referrerPolicy = mRequest->ReferrerPolicy_(); - net::ReferrerPolicy net_referrerPolicy = net::RP_Unset; + net::ReferrerPolicy net_referrerPolicy = mRequest->GetEnvironmentReferrerPolicy(); switch (referrerPolicy) { case ReferrerPolicy::_empty: - net_referrerPolicy = net::RP_Default; break; case ReferrerPolicy::No_referrer: net_referrerPolicy = net::RP_No_Referrer; @@ -294,12 +298,10 @@ FetchDriver::HttpFetch() rv = NS_NewURI(getter_AddRefs(referrerURI), referrer, nullptr, nullptr); NS_ENSURE_SUCCESS(rv, rv); - uint32_t documentReferrerPolicy = mDocument ? mDocument->GetReferrerPolicy() : - net::RP_Default; rv = httpChan->SetReferrerWithPolicy(referrerURI, referrerPolicy == ReferrerPolicy::_empty ? - documentReferrerPolicy : + mRequest->GetEnvironmentReferrerPolicy() : net_referrerPolicy); NS_ENSURE_SUCCESS(rv, rv); } diff --git a/dom/fetch/InternalRequest.cpp b/dom/fetch/InternalRequest.cpp index 7d5800531767..1b9f1b43d69a 100644 --- a/dom/fetch/InternalRequest.cpp +++ b/dom/fetch/InternalRequest.cpp @@ -40,6 +40,7 @@ InternalRequest::GetRequestConstructorCopy(nsIGlobalObject* aGlobal, ErrorResult copy->mPreserveContentCodings = true; // The default referrer is already about:client. copy->mReferrerPolicy = mReferrerPolicy; + copy->mEnvironmentReferrerPolicy = mEnvironmentReferrerPolicy; copy->mContentPolicyType = mContentPolicyTypeOverridden ? mContentPolicyType : @@ -84,6 +85,7 @@ InternalRequest::InternalRequest(const InternalRequest& aOther) , mContentPolicyType(aOther.mContentPolicyType) , mReferrer(aOther.mReferrer) , mReferrerPolicy(aOther.mReferrerPolicy) + , mEnvironmentReferrerPolicy(aOther.mEnvironmentReferrerPolicy) , mMode(aOther.mMode) , mCredentialsMode(aOther.mCredentialsMode) , mResponseTainting(aOther.mResponseTainting) diff --git a/dom/fetch/InternalRequest.h b/dom/fetch/InternalRequest.h index 9f985e8aca19..e2f8b2a316be 100644 --- a/dom/fetch/InternalRequest.h +++ b/dom/fetch/InternalRequest.h @@ -11,6 +11,7 @@ #include "mozilla/dom/InternalHeaders.h" #include "mozilla/dom/RequestBinding.h" #include "mozilla/LoadTainting.h" +#include "mozilla/net/ReferrerPolicy.h" #include "nsIContentPolicy.h" #include "nsIInputStream.h" @@ -95,6 +96,7 @@ public: , mContentPolicyType(nsIContentPolicy::TYPE_FETCH) , mReferrer(NS_LITERAL_STRING(kFETCH_CLIENT_REFERRER_STR)) , mReferrerPolicy(ReferrerPolicy::_empty) + , mEnvironmentReferrerPolicy(net::RP_Default) , mMode(RequestMode::No_cors) , mCredentialsMode(RequestCredentials::Omit) , mResponseTainting(LoadTainting::Basic) @@ -132,6 +134,7 @@ public: , mContentPolicyType(aContentPolicyType) , mReferrer(aReferrer) , mReferrerPolicy(aReferrerPolicy) + , mEnvironmentReferrerPolicy(net::RP_Default) , mMode(aMode) , mCredentialsMode(aRequestCredentials) , mResponseTainting(LoadTainting::Basic) @@ -266,6 +269,18 @@ public: mReferrerPolicy = aReferrerPolicy; } + net::ReferrerPolicy + GetEnvironmentReferrerPolicy() const + { + return mEnvironmentReferrerPolicy; + } + + void + SetEnvironmentReferrerPolicy(net::ReferrerPolicy aReferrerPolicy) + { + mEnvironmentReferrerPolicy = aReferrerPolicy; + } + bool SkipServiceWorker() const { @@ -492,6 +507,13 @@ private: nsString mReferrer; ReferrerPolicy mReferrerPolicy; + // This will be used for request created from Window or Worker contexts + // In case there's no Referrer Policy in Request, this will be passed to + // channel. + // The Environment Referrer Policy should be net::ReferrerPolicy so that it + // could be associated with nsIHttpChannel. + net::ReferrerPolicy mEnvironmentReferrerPolicy; + RequestMode mMode; RequestCredentials mCredentialsMode; MOZ_INIT_OUTSIDE_CTOR LoadTainting mResponseTainting; diff --git a/dom/fetch/Request.cpp b/dom/fetch/Request.cpp index 5e2d6abf96b0..596b213011c2 100644 --- a/dom/fetch/Request.cpp +++ b/dom/fetch/Request.cpp @@ -413,6 +413,23 @@ Request::Constructor(const GlobalObject& aGlobal, request->SetReferrerPolicy(aInit.mReferrerPolicy.Value()); } + if (NS_IsMainThread()) { + nsCOMPtr window = do_QueryInterface(global); + if (window) { + nsCOMPtr doc; + doc = window->GetExtantDoc(); + if (doc) { + request->SetEnvironmentReferrerPolicy(doc->GetReferrerPolicy()); + } + } + } else { + workers::WorkerPrivate* worker = workers::GetCurrentThreadWorkerPrivate(); + if (worker) { + worker->AssertIsOnWorkerThread(); + request->SetEnvironmentReferrerPolicy(worker->GetReferrerPolicy()); + } + } + if (mode != RequestMode::EndGuard_) { request->ClearCreatedByFetchEvent(); request->SetMode(mode);