Skip to content

Commit

Permalink
Bug 1525245 - Stabilize cookiePolicy/cookiePermission for live docume…
Browse files Browse the repository at this point in the history
…nts - part 12 - nsICookieSettings for the channel creation, r=ckerschb,asuth,Ehsan

Differential Revision: https://phabricator.services.mozilla.com/D21538

--HG--
extra : moz-landing-system : lando
  • Loading branch information
bakulf committed Mar 6, 2019
1 parent cc423cd commit 4fabb4a
Show file tree
Hide file tree
Showing 46 changed files with 320 additions and 109 deletions.
36 changes: 31 additions & 5 deletions dom/base/EventSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ class EventSourceImpl final : public nsIObserver,
NS_DECL_NSIEVENTTARGET_FULL
NS_DECL_NSITHREADRETARGETABLESTREAMLISTENER

explicit EventSourceImpl(EventSource* aEventSource);
EventSourceImpl(EventSource* aEventSource,
nsICookieSettings* aCookieSettings);

enum { CONNECTING = 0U, OPEN = 1U, CLOSED = 2U };

Expand Down Expand Up @@ -291,6 +292,8 @@ class EventSourceImpl final : public nsIObserver,
uint64_t mInnerWindowID;

private:
nsCOMPtr<nsICookieSettings> mCookieSettings;

// Pointer to the target thread for checking whether we are
// on the target thread. This is intentionally a non-owning
// pointer in order not to affect the thread destruction
Expand All @@ -316,7 +319,8 @@ NS_IMPL_ISUPPORTS(EventSourceImpl, nsIObserver, nsIStreamListener,
nsIInterfaceRequestor, nsISupportsWeakReference,
nsIEventTarget, nsIThreadRetargetableStreamListener)

EventSourceImpl::EventSourceImpl(EventSource* aEventSource)
EventSourceImpl::EventSourceImpl(EventSource* aEventSource,
nsICookieSettings* aCookieSettings)
: mEventSource(aEventSource),
mReconnectionTime(0),
mStatus(PARSE_STATE_OFF),
Expand All @@ -328,6 +332,7 @@ EventSourceImpl::EventSourceImpl(EventSource* aEventSource)
mScriptLine(0),
mScriptColumn(0),
mInnerWindowID(0),
mCookieSettings(aCookieSettings),
mTargetThread(NS_GetCurrentThread()) {
MOZ_ASSERT(mEventSource);
if (!mIsMainThread) {
Expand Down Expand Up @@ -971,6 +976,8 @@ nsresult EventSourceImpl::InitChannelAndRequestEventSource() {
nsCOMPtr<nsIChannel> channel;
// If we have the document, use it
if (doc) {
MOZ_ASSERT(mCookieSettings == doc->CookieSettings());

nsCOMPtr<nsILoadGroup> loadGroup = doc->GetDocumentLoadGroup();
rv = NS_NewChannel(getter_AddRefs(channel), mSrc, doc, securityFlags,
nsIContentPolicy::TYPE_INTERNAL_EVENTSOURCE,
Expand All @@ -982,6 +989,7 @@ nsresult EventSourceImpl::InitChannelAndRequestEventSource() {
// otherwise use the principal
rv = NS_NewChannel(getter_AddRefs(channel), mSrc, mPrincipal, securityFlags,
nsIContentPolicy::TYPE_INTERNAL_EVENTSOURCE,
mCookieSettings,
nullptr, // aPerformanceStorage
nullptr, // loadGroup
nullptr, // aCallbacks
Expand Down Expand Up @@ -1777,12 +1785,14 @@ EventSourceImpl::CheckListenerChain() {
////////////////////////////////////////////////////////////////////////////////

EventSource::EventSource(nsPIDOMWindowInner* aOwnerWindow,
nsICookieSettings* aCookieSettings,
bool aWithCredentials)
: DOMEventTargetHelper(aOwnerWindow),
mWithCredentials(aWithCredentials),
mIsMainThread(true),
mKeepingAlive(false) {
mImpl = new EventSourceImpl(this);
MOZ_ASSERT(aCookieSettings);
mImpl = new EventSourceImpl(this, aCookieSettings);
}

EventSource::~EventSource() {}
Expand All @@ -1806,8 +1816,24 @@ already_AddRefed<EventSource> EventSource::Constructor(

MOZ_ASSERT(!NS_IsMainThread() || ownerWindow);

RefPtr<EventSource> eventSource =
new EventSource(ownerWindow, aEventSourceInitDict.mWithCredentials);
nsCOMPtr<nsICookieSettings> cookieSettings;
if (ownerWindow) {
Document* doc = ownerWindow->GetExtantDoc();
if (NS_WARN_IF(!doc)) {
aRv.Throw(NS_ERROR_FAILURE);
return nullptr;
}

cookieSettings = doc->CookieSettings();
} else {
// Worker side.
WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate();
MOZ_ASSERT(workerPrivate);
cookieSettings = workerPrivate->CookieSettings();
}

RefPtr<EventSource> eventSource = new EventSource(
ownerWindow, cookieSettings, aEventSourceInitDict.mWithCredentials);
RefPtr<EventSourceImpl> eventSourceImp = eventSource->mImpl;

if (NS_IsMainThread()) {
Expand Down
4 changes: 3 additions & 1 deletion dom/base/EventSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "nsDeque.h"

class nsPIDOMWindowInner;
class nsICookieSettings;

namespace mozilla {

Expand Down Expand Up @@ -80,7 +81,8 @@ class EventSource final : public DOMEventTargetHelper {
void Close();

private:
EventSource(nsPIDOMWindowInner* aOwnerWindow, bool aWithCredentials);
EventSource(nsPIDOMWindowInner* aOwnerWindow,
nsICookieSettings* aCookieSettings, bool aWithCredentials);
virtual ~EventSource();
// prevent bad usage
EventSource(const EventSource& x) = delete;
Expand Down
13 changes: 7 additions & 6 deletions dom/base/nsSyncLoadService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,13 +279,14 @@ nsSyncLoader::GetInterface(const nsIID &aIID, void **aResult) {
nsresult nsSyncLoadService::LoadDocument(
nsIURI *aURI, nsContentPolicyType aContentPolicyType,
nsIPrincipal *aLoaderPrincipal, nsSecurityFlags aSecurityFlags,
nsILoadGroup *aLoadGroup, bool aForceToXML, ReferrerPolicy aReferrerPolicy,
Document **aResult) {
nsILoadGroup *aLoadGroup, nsICookieSettings *aCookieSettings,
bool aForceToXML, ReferrerPolicy aReferrerPolicy, Document **aResult) {
nsCOMPtr<nsIChannel> channel;
nsresult rv = NS_NewChannel(getter_AddRefs(channel), aURI, aLoaderPrincipal,
aSecurityFlags, aContentPolicyType,
nullptr, // PerformanceStorage
aLoadGroup);
nsresult rv =
NS_NewChannel(getter_AddRefs(channel), aURI, aLoaderPrincipal,
aSecurityFlags, aContentPolicyType, aCookieSettings,
nullptr, // PerformanceStorage
aLoadGroup);
NS_ENSURE_SUCCESS(rv, rv);

if (!aForceToXML) {
Expand Down
14 changes: 7 additions & 7 deletions dom/base/nsSyncLoadService.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "nscore.h"
#include "mozilla/net/ReferrerPolicy.h"

class nsICookieSettings;
class nsIInputStream;
class nsILoadGroup;
class nsIStreamListener;
Expand Down Expand Up @@ -44,13 +45,12 @@ class nsSyncLoadService {
* @param referrerPolicy Referrer policy.
* @param aResult [out] The document loaded from the URI.
*/
static nsresult LoadDocument(nsIURI* aURI,
nsContentPolicyType aContentPolicyType,
nsIPrincipal* aLoaderPrincipal,
nsSecurityFlags aSecurityFlags,
nsILoadGroup* aLoadGroup, bool aForceToXML,
mozilla::net::ReferrerPolicy aReferrerPolicy,
mozilla::dom::Document** aResult);
static nsresult LoadDocument(
nsIURI* aURI, nsContentPolicyType aContentPolicyType,
nsIPrincipal* aLoaderPrincipal, nsSecurityFlags aSecurityFlags,
nsILoadGroup* aLoadGroup, nsICookieSettings* aCookieSettings,
bool aForceToXML, mozilla::net::ReferrerPolicy aReferrerPolicy,
mozilla::dom::Document** aResult);

/**
* Read input stream aIn in chunks and deliver synchronously to aListener.
Expand Down
8 changes: 7 additions & 1 deletion dom/fetch/Fetch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "mozilla/dom/Response.h"
#include "mozilla/dom/ScriptSettings.h"
#include "mozilla/dom/URLSearchParams.h"
#include "mozilla/net/CookieSettings.h"
#include "mozilla/Telemetry.h"

#include "BodyExtractor.h"
Expand Down Expand Up @@ -389,6 +390,7 @@ class MainThreadFetchRunnable : public Runnable {
// so pass false as the last argument to FetchDriver().
fetch = new FetchDriver(mRequest, principal, loadGroup,
workerPrivate->MainThreadEventTarget(),
workerPrivate->CookieSettings(),
workerPrivate->GetPerformanceStorage(), false);
nsAutoCString spec;
if (proxy->GetWorkerPrivate()->GetBaseURI()) {
Expand Down Expand Up @@ -462,6 +464,7 @@ already_AddRefed<Promise> FetchRequest(nsIGlobalObject* aGlobal,
nsCOMPtr<nsPIDOMWindowInner> window = do_QueryInterface(aGlobal);
nsCOMPtr<Document> doc;
nsCOMPtr<nsILoadGroup> loadGroup;
nsCOMPtr<nsICookieSettings> cookieSettings;
nsIPrincipal* principal;
bool isTrackingFetch = false;
if (window) {
Expand All @@ -472,6 +475,7 @@ already_AddRefed<Promise> FetchRequest(nsIGlobalObject* aGlobal,
}
principal = doc->NodePrincipal();
loadGroup = doc->GetDocumentLoadGroup();
cookieSettings = doc->CookieSettings();

nsAutoCString fileNameString;
if (nsJSUtils::GetCallingLocation(cx, fileNameString)) {
Expand All @@ -488,6 +492,8 @@ already_AddRefed<Promise> FetchRequest(nsIGlobalObject* aGlobal,
aRv.Throw(rv);
return nullptr;
}

cookieSettings = mozilla::net::CookieSettings::Create();
}

Telemetry::Accumulate(Telemetry::FETCH_IS_MAINTHREAD, 1);
Expand All @@ -496,7 +502,7 @@ already_AddRefed<Promise> FetchRequest(nsIGlobalObject* aGlobal,
p, observer, signalImpl, request->MozErrors());
RefPtr<FetchDriver> fetch = new FetchDriver(
r, principal, loadGroup, aGlobal->EventTargetFor(TaskCategory::Other),
nullptr, // PerformanceStorage
cookieSettings, nullptr, // PerformanceStorage
isTrackingFetch);
fetch->SetDocument(doc);
resolver->SetLoadGroup(loadGroup);
Expand Down
18 changes: 11 additions & 7 deletions dom/fetch/FetchDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -323,12 +323,14 @@ NS_IMPL_ISUPPORTS(FetchDriver, nsIStreamListener, nsIChannelEventSink,
FetchDriver::FetchDriver(InternalRequest* aRequest, nsIPrincipal* aPrincipal,
nsILoadGroup* aLoadGroup,
nsIEventTarget* aMainThreadEventTarget,
nsICookieSettings* aCookieSettings,
PerformanceStorage* aPerformanceStorage,
bool aIsTrackingFetch)
: mPrincipal(aPrincipal),
mLoadGroup(aLoadGroup),
mRequest(aRequest),
mMainThreadEventTarget(aMainThreadEventTarget),
mCookieSettings(aCookieSettings),
mPerformanceStorage(aPerformanceStorage),
mNeedToObserveOnDataAvailable(false),
mIsTrackingFetch(aIsTrackingFetch),
Expand Down Expand Up @@ -509,22 +511,24 @@ nsresult FetchDriver::HttpFetch(
nsIRequest::LOAD_BACKGROUND | bypassFlag | nsIChannel::LOAD_CLASSIFY_URI;
if (mDocument) {
MOZ_ASSERT(mDocument->NodePrincipal() == mPrincipal);
MOZ_ASSERT(mDocument->CookieSettings() == mCookieSettings);
rv = NS_NewChannel(getter_AddRefs(chan), uri, mDocument, secFlags,
mRequest->ContentPolicyType(),
nullptr, /* aPerformanceStorage */
mLoadGroup, nullptr, /* aCallbacks */
loadFlags, ios);
} else if (mClientInfo.isSome()) {
rv = NS_NewChannel(getter_AddRefs(chan), uri, mPrincipal, mClientInfo.ref(),
mController, secFlags, mRequest->ContentPolicyType(),
mCookieSettings, mPerformanceStorage, mLoadGroup,
nullptr, /* aCallbacks */
loadFlags, ios);
} else {
rv =
NS_NewChannel(getter_AddRefs(chan), uri, mPrincipal, mClientInfo.ref(),
mController, secFlags, mRequest->ContentPolicyType(),
NS_NewChannel(getter_AddRefs(chan), uri, mPrincipal, secFlags,
mRequest->ContentPolicyType(), mCookieSettings,
mPerformanceStorage, mLoadGroup, nullptr, /* aCallbacks */
loadFlags, ios);
} else {
rv = NS_NewChannel(getter_AddRefs(chan), uri, mPrincipal, secFlags,
mRequest->ContentPolicyType(), mPerformanceStorage,
mLoadGroup, nullptr, /* aCallbacks */
loadFlags, ios);
}
NS_ENSURE_SUCCESS(rv, rv);

Expand Down
4 changes: 4 additions & 0 deletions dom/fetch/FetchDriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "mozilla/net/ReferrerPolicy.h"

class nsIConsoleReportCollector;
class nsICookieSettings;
class nsICSPEventListener;
class nsIEventTarget;
class nsIOutputStream;
Expand Down Expand Up @@ -101,6 +102,7 @@ class FetchDriver final : public nsIStreamListener,

FetchDriver(InternalRequest* aRequest, nsIPrincipal* aPrincipal,
nsILoadGroup* aLoadGroup, nsIEventTarget* aMainThreadEventTarget,
nsICookieSettings* aCookieSettings,
PerformanceStorage* aPerformanceStorage, bool aIsTrackingFetch);

nsresult Fetch(AbortSignalImpl* aSignalImpl, FetchDriverObserver* aObserver);
Expand Down Expand Up @@ -136,6 +138,8 @@ class FetchDriver final : public nsIStreamListener,
nsAutoPtr<SRICheckDataVerifier> mSRIDataVerifier;
nsCOMPtr<nsIEventTarget> mMainThreadEventTarget;

nsCOMPtr<nsICookieSettings> mCookieSettings;

// This is set only when Fetch is used in workers.
RefPtr<PerformanceStorage> mPerformanceStorage;

Expand Down
1 change: 1 addition & 0 deletions dom/security/nsCSPContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1016,6 +1016,7 @@ nsresult nsCSPContext::SendReports(
mLoadingPrincipal,
nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_DATA_IS_NULL,
nsIContentPolicy::TYPE_CSP_REPORT,
nullptr, // nsICookieSettings
nullptr, // PerformanceStorage
nullptr, // aLoadGroup
nullptr, // aCallbacks
Expand Down
5 changes: 5 additions & 0 deletions dom/serviceworkers/ServiceWorkerPrivate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include "mozilla/dom/WorkerRunnable.h"
#include "mozilla/dom/WorkerScope.h"
#include "mozilla/dom/ipc/StructuredCloneData.h"
#include "mozilla/net/CookieSettings.h"
#include "mozilla/net/NeckoChannelParams.h"
#include "mozilla/StaticPrefs.h"
#include "mozilla/Unused.h"
Expand Down Expand Up @@ -1727,6 +1728,10 @@ nsresult ServiceWorkerPrivate::SpawnWorkerIfNeeded(WakeUpReason aWhy,
nsContentUtils::StorageAllowedForServiceWorker(info.mPrincipal);
info.mStorageAllowed =
access > nsContentUtils::StorageAccess::ePrivateBrowsing;

info.mCookieSettings = mozilla::net::CookieSettings::Create();
MOZ_ASSERT(info.mCookieSettings);

info.mOriginAttributes = mInfo->GetOriginAttributes();

// Verify that we don't have any CSP on pristine principal.
Expand Down
10 changes: 8 additions & 2 deletions dom/serviceworkers/ServiceWorkerScriptCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "mozilla/dom/WorkerCommon.h"
#include "mozilla/ipc/BackgroundUtils.h"
#include "mozilla/ipc/PBackgroundSharedTypes.h"
#include "mozilla/net/CookieSettings.h"
#include "nsICacheInfoChannel.h"
#include "nsIHttpChannelInternal.h"
#include "nsIStreamLoader.h"
Expand Down Expand Up @@ -657,12 +658,17 @@ nsresult CompareNetwork::Initialize(nsIPrincipal* aPrincipal,
mIsMainScript ? nsIContentPolicy::TYPE_INTERNAL_SERVICE_WORKER
: nsIContentPolicy::TYPE_INTERNAL_WORKER_IMPORT_SCRIPTS;

// Create a new cookieSettings.
nsCOMPtr<nsICookieSettings> cookieSettings =
mozilla::net::CookieSettings::Create();

// Note that because there is no "serviceworker" RequestContext type, we can
// use the TYPE_INTERNAL_SCRIPT content policy types when loading a service
// worker.
rv = NS_NewChannel(getter_AddRefs(mChannel), uri, aPrincipal, secFlags,
contentPolicyType, nullptr, /* aPerformanceStorage */
loadGroup, nullptr /* aCallbacks */, mLoadFlags);
contentPolicyType, cookieSettings,
nullptr /* aPerformanceStorage */, loadGroup,
nullptr /* aCallbacks */, mLoadFlags);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
Expand Down
8 changes: 7 additions & 1 deletion dom/webbrowserpersist/nsWebBrowserPersist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
#include "nsIMIMEInfo.h"
#include "mozilla/dom/HTMLInputElement.h"
#include "mozilla/dom/HTMLSharedElement.h"
#include "mozilla/net/CookieSettings.h"
#include "mozilla/Printf.h"

using namespace mozilla;
Expand Down Expand Up @@ -1234,11 +1235,16 @@ nsresult nsWebBrowserPersist::SaveURIInternal(
loadFlags |= nsIRequest::LOAD_FROM_CACHE;
}

// Create a new cookieSettings for this download in order to send cookies
// based on the current state of the prefs/permissions.
nsCOMPtr<nsICookieSettings> cookieSettings =
mozilla::net::CookieSettings::Create();

// Open a channel to the URI
nsCOMPtr<nsIChannel> inputChannel;
rv = NS_NewChannel(getter_AddRefs(inputChannel), aURI, aTriggeringPrincipal,
nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_DATA_IS_NULL,
nsIContentPolicy::TYPE_SAVEAS_DOWNLOAD,
nsIContentPolicy::TYPE_SAVEAS_DOWNLOAD, cookieSettings,
nullptr, // aPerformanceStorage
nullptr, // aLoadGroup
static_cast<nsIInterfaceRequestor *>(this), loadFlags);
Expand Down
Loading

0 comments on commit 4fabb4a

Please sign in to comment.