Skip to content

Commit

Permalink
Bug 1572240 - Part 3: Introduce nsIHttpChannelInternal.contentBlockin…
Browse files Browse the repository at this point in the history
…gAllowListPrincipal; r=michal

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

--HG--
extra : moz-landing-system : lando
  • Loading branch information
ehsan committed Aug 19, 2019
1 parent e06a6e7 commit 575c480
Show file tree
Hide file tree
Showing 23 changed files with 260 additions and 44 deletions.
11 changes: 11 additions & 0 deletions dom/base/Document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15970,5 +15970,16 @@ bool Document::HasRecentlyStartedForegroundLoads() {
return false;
}

already_AddRefed<nsIPrincipal>
Document::RecomputeContentBlockingAllowListPrincipal(
nsIURI* aURIBeingLoaded, const OriginAttributes& aAttrs) {
AntiTrackingCommon::RecomputeContentBlockingAllowListPrincipal(
aURIBeingLoaded, aAttrs,
getter_AddRefs(mContentBlockingAllowListPrincipal));

nsCOMPtr<nsIPrincipal> copy = mContentBlockingAllowListPrincipal;
return copy.forget();
}

} // namespace dom
} // namespace mozilla
3 changes: 3 additions & 0 deletions dom/base/Document.h
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,9 @@ class Document : public nsINode,
return mContentBlockingAllowListPrincipal;
}

already_AddRefed<nsIPrincipal> RecomputeContentBlockingAllowListPrincipal(
nsIURI* aURIBeingLoaded, const OriginAttributes& aAttrs);

// EventTarget
void GetEventTargetParent(EventChainPreVisitor& aVisitor) override;
EventListenerManager* GetOrCreateListenerManager() override;
Expand Down
38 changes: 38 additions & 0 deletions dom/base/ThirdPartyUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "ThirdPartyUtil.h"
#include "nsDocShell.h"
#include "nsGlobalWindowOuter.h"
#include "nsNetCID.h"
#include "nsNetUtil.h"
Expand Down Expand Up @@ -139,6 +140,43 @@ ThirdPartyUtil::GetURIFromWindow(mozIDOMWindowProxy* aWin, nsIURI** result) {
return rv;
}

NS_IMETHODIMP
ThirdPartyUtil::GetContentBlockingAllowListPrincipalFromWindow(
mozIDOMWindowProxy* aWin, nsIURI* aURIBeingLoaded, nsIPrincipal** result) {
nsPIDOMWindowOuter* outerWindow = nsPIDOMWindowOuter::From(aWin);
nsPIDOMWindowInner* innerWindow = outerWindow->GetCurrentInnerWindow();
Document* doc = innerWindow ? innerWindow->GetExtantDoc() : nullptr;
if (!doc) {
return GetPrincipalFromWindow(aWin, result);
}

nsCOMPtr<nsIPrincipal> principal =
doc->GetContentBlockingAllowListPrincipal();
if (aURIBeingLoaded && principal && principal->GetIsNullPrincipal()) {
// If we have an initial principal during navigation, recompute it to get
// the real content blocking allow list principal.
nsIDocShell* docShell = doc->GetDocShell();
OriginAttributes attrs =
docShell ? nsDocShell::Cast(docShell)->GetOriginAttributes()
: OriginAttributes();
principal =
doc->RecomputeContentBlockingAllowListPrincipal(aURIBeingLoaded, attrs);
}

if (!principal || !principal->GetIsContentPrincipal()) {
// This is for compatibility with GetURIFromWindow. Null principals are
// explicitly special cased there. GetURI returns nullptr for
// SystemPrincipal and ExpandedPrincipal.
LOG(
("ThirdPartyUtil::GetContentBlockingAllowListPrincipalFromWindow can't "
"use null principal\n"));
return NS_ERROR_INVALID_ARG;
}

principal.forget(result);
return NS_OK;
}

// Determine if aFirstURI is third party with respect to aSecondURI. See docs
// for mozIThirdPartyUtil.
NS_IMETHODIMP
Expand Down
9 changes: 9 additions & 0 deletions netwerk/base/mozIThirdPartyUtil.idl
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,15 @@ interface mozIThirdPartyUtil : nsISupports
*/
nsIPrincipal getPrincipalFromWindow(in mozIDOMWindowProxy aWindow);

/**
* getContentBlockingAllowListPrincipalFromWindow
*
* Returns the content blocking allow list principal for the window.
*/
[noscript]
nsIPrincipal getContentBlockingAllowListPrincipalFromWindow(in mozIDOMWindowProxy aWindow,
[optional] in nsIURI aURIBeingLoaded);

/**
* getTopWindowForChannel
*
Expand Down
1 change: 1 addition & 0 deletions netwerk/ipc/NeckoChannelParams.ipdlh
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ struct HttpChannelOpenArgs
nsIReferrerInfo referrerInfo;
URIParams? apiRedirectTo;
URIParams? topWindowURI;
OptionalPrincipalInfo contentBlockingAllowListPrincipal;
uint32_t loadFlags;
RequestHeaderTuples requestHeaders;
nsCString requestMethod;
Expand Down
5 changes: 3 additions & 2 deletions netwerk/ipc/NeckoChild.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -418,8 +418,9 @@ mozilla::ipc::IPCResult NeckoChild::RecvNetworkChangeNotification(
}

PClassifierDummyChannelChild* NeckoChild::AllocPClassifierDummyChannelChild(
nsIURI* aURI, nsIURI* aTopWindowURI, const nsresult& aTopWindowURIResult,
const Maybe<LoadInfoArgs>& aLoadInfo) {
nsIURI* aURI, nsIURI* aTopWindowURI,
nsIPrincipal* aContentBlockingAllowListPrincipal,
const nsresult& aTopWindowURIResult, const Maybe<LoadInfoArgs>& aLoadInfo) {
return new ClassifierDummyChannelChild();
}

Expand Down
4 changes: 3 additions & 1 deletion netwerk/ipc/NeckoChild.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ class NeckoChild : public PNeckoChild {
mozilla::ipc::IPCResult RecvNetworkChangeNotification(nsCString const& type);

PClassifierDummyChannelChild* AllocPClassifierDummyChannelChild(
nsIURI* aURI, nsIURI* aTopWindowURI, const nsresult& aTopWindowURIResult,
nsIURI* aURI, nsIURI* aTopWindowURI,
nsIPrincipal* aContentBlockingAllowListPrincipal,
const nsresult& aTopWindowURIResult,
const Maybe<LoadInfoArgs>& aLoadInfo);

bool DeallocPClassifierDummyChannelChild(
Expand Down
9 changes: 6 additions & 3 deletions netwerk/ipc/NeckoParent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -905,14 +905,16 @@ mozilla::ipc::IPCResult NeckoParent::RecvGetExtensionFD(
}

PClassifierDummyChannelParent* NeckoParent::AllocPClassifierDummyChannelParent(
nsIURI* aURI, nsIURI* aTopWindowURI, const nsresult& aTopWindowURIResult,
const Maybe<LoadInfoArgs>& aLoadInfo) {
nsIURI* aURI, nsIURI* aTopWindowURI,
nsIPrincipal* aContentBlockingAllowListPrincipal,
const nsresult& aTopWindowURIResult, const Maybe<LoadInfoArgs>& aLoadInfo) {
RefPtr<ClassifierDummyChannelParent> c = new ClassifierDummyChannelParent();
return c.forget().take();
}

mozilla::ipc::IPCResult NeckoParent::RecvPClassifierDummyChannelConstructor(
PClassifierDummyChannelParent* aActor, nsIURI* aURI, nsIURI* aTopWindowURI,
nsIPrincipal* aContentBlockingAllowListPrincipal,
const nsresult& aTopWindowURIResult, const Maybe<LoadInfoArgs>& aLoadInfo) {
ClassifierDummyChannelParent* p =
static_cast<ClassifierDummyChannelParent*>(aActor);
Expand All @@ -927,7 +929,8 @@ mozilla::ipc::IPCResult NeckoParent::RecvPClassifierDummyChannelConstructor(
return IPC_FAIL_NO_REASON(this);
}

p->Init(aURI, aTopWindowURI, aTopWindowURIResult, loadInfo);
p->Init(aURI, aTopWindowURI, aContentBlockingAllowListPrincipal,
aTopWindowURIResult, loadInfo);
return IPC_OK();
}

Expand Down
7 changes: 5 additions & 2 deletions netwerk/ipc/NeckoParent.h
Original file line number Diff line number Diff line change
Expand Up @@ -224,15 +224,18 @@ class NeckoParent : public PNeckoParent {
GetExtensionFDResolver&& aResolve);

PClassifierDummyChannelParent* AllocPClassifierDummyChannelParent(
nsIURI* aURI, nsIURI* aTopWindowURI, const nsresult& aTopWindowURIResult,
nsIURI* aURI, nsIURI* aTopWindowURI,
nsIPrincipal* aContentBlockingAllowListPrincipal,
const nsresult& aTopWindowURIResult,
const Maybe<LoadInfoArgs>& aLoadInfo);

bool DeallocPClassifierDummyChannelParent(
PClassifierDummyChannelParent* aParent);

virtual mozilla::ipc::IPCResult RecvPClassifierDummyChannelConstructor(
PClassifierDummyChannelParent* aActor, nsIURI* aURI,
nsIURI* aTopWindowURI, const nsresult& aTopWindowURIResult,
nsIURI* aTopWindowURI, nsIPrincipal* aContentBlockingAllowListPrincipal,
const nsresult& aTopWindowURIResult,
const Maybe<LoadInfoArgs>& aLoadInfo) override;

mozilla::ipc::IPCResult RecvInitSocketProcessBridge(
Expand Down
1 change: 1 addition & 0 deletions netwerk/ipc/PNecko.ipdl
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ parent:
async PChannelDiverter(ChannelDiverterArgs channel);

async PClassifierDummyChannel(nsIURI uri, nsIURI aTopWindowURI,
nsIPrincipal contentBlockingAllowListPrincipal,
nsresult aTopWindowURIResult,
LoadInfoArgs? loadInfo);

Expand Down
20 changes: 16 additions & 4 deletions netwerk/protocol/http/ClassifierDummyChannel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,12 @@ NS_INTERFACE_MAP_BEGIN(ClassifierDummyChannel)
NS_INTERFACE_MAP_ENTRY_CONCRETE(ClassifierDummyChannel)
NS_INTERFACE_MAP_END

ClassifierDummyChannel::ClassifierDummyChannel(nsIURI* aURI,
nsIURI* aTopWindowURI,
nsresult aTopWindowURIResult,
nsILoadInfo* aLoadInfo)
ClassifierDummyChannel::ClassifierDummyChannel(
nsIURI* aURI, nsIURI* aTopWindowURI,
nsIPrincipal* aContentBlockingAllowListPrincipal,
nsresult aTopWindowURIResult, nsILoadInfo* aLoadInfo)
: mTopWindowURI(aTopWindowURI),
mContentBlockingAllowListPrincipal(aContentBlockingAllowListPrincipal),
mTopWindowURIResult(aTopWindowURIResult),
mClassificationFlags(0) {
MOZ_ASSERT(XRE_IsParentProcess());
Expand All @@ -97,6 +98,9 @@ ClassifierDummyChannel::~ClassifierDummyChannel() {
mURI.forget());
NS_ReleaseOnMainThreadSystemGroup("ClassifierDummyChannel::mTopWindowURI",
mTopWindowURI.forget());
NS_ReleaseOnMainThreadSystemGroup(
"ClassifierDummyChannel::mContentBlockingAllowListPrincipal",
mContentBlockingAllowListPrincipal.forget());
}

uint32_t ClassifierDummyChannel::ClassificationFlags() const {
Expand Down Expand Up @@ -555,6 +559,14 @@ ClassifierDummyChannel::GetTopWindowURI(nsIURI** aTopWindowURI) {
return mTopWindowURIResult;
}

NS_IMETHODIMP
ClassifierDummyChannel::GetContentBlockingAllowListPrincipal(
nsIPrincipal** aPrincipal) {
nsCOMPtr<nsIPrincipal> copy = mContentBlockingAllowListPrincipal;
copy.forget(aPrincipal);
return NS_OK;
}

NS_IMETHODIMP
ClassifierDummyChannel::SetTopWindowURIIfUnknown(nsIURI* aTopWindowURI) {
return NS_ERROR_NOT_IMPLEMENTED;
Expand Down
3 changes: 3 additions & 0 deletions netwerk/protocol/http/ClassifierDummyChannel.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
}

class nsIChannel;
class nsIPrincipal;

namespace mozilla {
namespace net {
Expand Down Expand Up @@ -64,6 +65,7 @@ class ClassifierDummyChannel final : public nsIChannel,
nsIChannel* aChannel, const std::function<void(bool)>& aCallback);

ClassifierDummyChannel(nsIURI* aURI, nsIURI* aTopWindowURI,
nsIPrincipal* aContentBlockingAllowListPrincipal,
nsresult aTopWindowURIResult, nsILoadInfo* aLoadInfo);

uint32_t ClassificationFlags() const;
Expand All @@ -76,6 +78,7 @@ class ClassifierDummyChannel final : public nsIChannel,
nsCOMPtr<nsILoadInfo> mLoadInfo;
nsCOMPtr<nsIURI> mURI;
nsCOMPtr<nsIURI> mTopWindowURI;
nsCOMPtr<nsIPrincipal> mContentBlockingAllowListPrincipal;
nsresult mTopWindowURIResult;

uint32_t mClassificationFlags;
Expand Down
7 changes: 6 additions & 1 deletion netwerk/protocol/http/ClassifierDummyChannelChild.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,18 @@ bool ClassifierDummyChannelChild::Create(
nsresult topWindowURIResult =
httpChannelInternal->GetTopWindowURI(getter_AddRefs(topWindowURI));

nsCOMPtr<nsIPrincipal> principal;
nsresult rv = httpChannelInternal->GetContentBlockingAllowListPrincipal(
getter_AddRefs(principal));
MOZ_ALWAYS_SUCCEEDS(rv);

nsCOMPtr<nsILoadInfo> loadInfo = aChannel->LoadInfo();
Maybe<LoadInfoArgs> loadInfoArgs;
mozilla::ipc::LoadInfoToLoadInfoArgs(loadInfo, &loadInfoArgs);

PClassifierDummyChannelChild* actor =
gNeckoChild->SendPClassifierDummyChannelConstructor(
aURI, topWindowURI, topWindowURIResult, loadInfoArgs);
aURI, topWindowURI, principal, topWindowURIResult, loadInfoArgs);
if (!actor) {
return false;
}
Expand Down
10 changes: 6 additions & 4 deletions netwerk/protocol/http/ClassifierDummyChannelParent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ ClassifierDummyChannelParent::ClassifierDummyChannelParent()

ClassifierDummyChannelParent::~ClassifierDummyChannelParent() = default;

void ClassifierDummyChannelParent::Init(nsIURI* aURI, nsIURI* aTopWindowURI,
nsresult aTopWindowURIResult,
nsILoadInfo* aLoadInfo) {
void ClassifierDummyChannelParent::Init(
nsIURI* aURI, nsIURI* aTopWindowURI,
nsIPrincipal* aContentBlockingAllowListPrincipal,
nsresult aTopWindowURIResult, nsILoadInfo* aLoadInfo) {
MOZ_ASSERT(mIPCActive);

RefPtr<ClassifierDummyChannelParent> self = this;
Expand All @@ -33,7 +34,8 @@ void ClassifierDummyChannelParent::Init(nsIURI* aURI, nsIURI* aTopWindowURI,
}

RefPtr<ClassifierDummyChannel> channel = new ClassifierDummyChannel(
aURI, aTopWindowURI, aTopWindowURIResult, aLoadInfo);
aURI, aTopWindowURI, aContentBlockingAllowListPrincipal,
aTopWindowURIResult, aLoadInfo);

bool willCallback = NS_SUCCEEDED(AsyncUrlChannelClassifier::CheckChannel(
channel, [self = std::move(self), channel]() {
Expand Down
5 changes: 3 additions & 2 deletions netwerk/protocol/http/ClassifierDummyChannelParent.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ class ClassifierDummyChannelParent final

ClassifierDummyChannelParent();

void Init(nsIURI* aURI, nsIURI* aTopWindowURI, nsresult aTopWindowURIResult,
nsILoadInfo* aLoadInfo);
void Init(nsIURI* aURI, nsIURI* aTopWindowURI,
nsIPrincipal* aContentBlockingAllowListPrincipal,
nsresult aTopWindowURIResult, nsILoadInfo* aLoadInfo);

private:
~ClassifierDummyChannelParent();
Expand Down
28 changes: 28 additions & 0 deletions netwerk/protocol/http/HttpBaseChannel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ void HttpBaseChannel::ReleaseMainThreadOnlyReferences() {
arrayToRelease.AppendElement(mProxyURI.forget());
arrayToRelease.AppendElement(mPrincipal.forget());
arrayToRelease.AppendElement(mTopWindowURI.forget());
arrayToRelease.AppendElement(mContentBlockingAllowListPrincipal.forget());
arrayToRelease.AppendElement(mListener.forget());
arrayToRelease.AppendElement(mCompressListener.forget());

Expand Down Expand Up @@ -2052,6 +2053,12 @@ nsresult HttpBaseChannel::GetTopWindowURI(nsIURI* aURIBeingLoaded,
}
}
#endif

if (!mContentBlockingAllowListPrincipal) {
Unused << util->GetContentBlockingAllowListPrincipalFromWindow(
win, aURIBeingLoaded,
getter_AddRefs(mContentBlockingAllowListPrincipal));
}
}
}
NS_IF_ADDREF(*aTopWindowURI = mTopWindowURI);
Expand All @@ -2066,6 +2073,27 @@ HttpBaseChannel::GetDocumentURI(nsIURI** aDocumentURI) {
return NS_OK;
}

NS_IMETHODIMP
HttpBaseChannel::GetContentBlockingAllowListPrincipal(
nsIPrincipal** aPrincipal) {
NS_ENSURE_ARG_POINTER(aPrincipal);
if (!mContentBlockingAllowListPrincipal) {
if (!mTopWindowURI) {
// If mTopWindowURI is null, it's possible that these two fields haven't
// been initialized yet. GetTopWindowURI will lazily initilize both
// fields for us.
nsCOMPtr<nsIURI> throwAway;
Unused << GetTopWindowURI(getter_AddRefs(throwAway));
} else {
// Otherwise, the content blocking allow list principal is null (which is
// possible), so just return what we have...
}
}
nsCOMPtr<nsIPrincipal> copy = mContentBlockingAllowListPrincipal;
copy.forget(aPrincipal);
return NS_OK;
}

NS_IMETHODIMP
HttpBaseChannel::SetDocumentURI(nsIURI* aDocumentURI) {
ENSURE_CALLED_BEFORE_CONNECT();
Expand Down
7 changes: 7 additions & 0 deletions netwerk/protocol/http/HttpBaseChannel.h
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,8 @@ class HttpBaseChannel : public nsHashPropertyBag,
NS_IMETHOD GetFetchCacheMode(uint32_t* aFetchCacheMode) override;
NS_IMETHOD SetFetchCacheMode(uint32_t aFetchCacheMode) override;
NS_IMETHOD GetTopWindowURI(nsIURI** aTopWindowURI) override;
NS_IMETHOD GetContentBlockingAllowListPrincipal(
nsIPrincipal** aPrincipal) override;
NS_IMETHOD SetTopWindowURIIfUnknown(nsIURI* aTopWindowURI) override;
NS_IMETHOD GetProxyURI(nsIURI** proxyURI) override;
virtual void SetCorsPreflightParameters(
Expand Down Expand Up @@ -464,6 +466,10 @@ class HttpBaseChannel : public nsHashPropertyBag,
return NS_OK;
}

void SetContentBlockingAllowListPrincipal(nsIPrincipal* aPrincipal) {
mContentBlockingAllowListPrincipal = aPrincipal;
}

// Set referrerInfo and compute the referrer header if neccessary.
nsresult SetReferrerInfo(nsIReferrerInfo* aReferrerInfo, bool aClone,
bool aCompute);
Expand Down Expand Up @@ -572,6 +578,7 @@ class HttpBaseChannel : public nsHashPropertyBag,
nsCOMPtr<nsIURI> mProxyURI;
nsCOMPtr<nsIPrincipal> mPrincipal;
nsCOMPtr<nsIURI> mTopWindowURI;
nsCOMPtr<nsIPrincipal> mContentBlockingAllowListPrincipal;
nsCOMPtr<nsIStreamListener> mListener;
// An instance of nsHTTPCompressConv
nsCOMPtr<nsIStreamListener> mCompressListener;
Expand Down
Loading

0 comments on commit 575c480

Please sign in to comment.