2018-11-30 22:52:05 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2018-11-30 18:39:55 +03:00
|
|
|
/* vim: set ts=4 et sw=2 tw=80: */
|
2013-11-13 04:43:35 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "DomainPolicy.h"
|
2015-03-24 17:29:16 +03:00
|
|
|
#include "mozilla/dom/ContentParent.h"
|
|
|
|
#include "mozilla/ipc/URIUtils.h"
|
2016-08-23 07:09:32 +03:00
|
|
|
#include "mozilla/Unused.h"
|
2013-11-13 04:43:35 +04:00
|
|
|
#include "nsScriptSecurityManager.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2015-03-24 17:29:16 +03:00
|
|
|
using namespace ipc;
|
|
|
|
using namespace dom;
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(DomainPolicy, nsIDomainPolicy)
|
2013-11-13 04:43:35 +04:00
|
|
|
|
2015-03-24 17:29:16 +03:00
|
|
|
static nsresult BroadcastDomainSetChange(DomainSetType aSetType,
|
|
|
|
DomainSetChangeType aChangeType,
|
|
|
|
nsIURI* aDomain = nullptr) {
|
2015-07-04 04:29:00 +03:00
|
|
|
MOZ_ASSERT(XRE_IsParentProcess(),
|
2015-03-24 17:29:16 +03:00
|
|
|
"DomainPolicy should only be exposed to the chrome process.");
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-03-24 17:29:16 +03:00
|
|
|
nsTArray<ContentParent*> parents;
|
|
|
|
ContentParent::GetAll(parents);
|
|
|
|
if (!parents.Length()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2019-03-06 17:05:50 +03:00
|
|
|
Maybe<URIParams> uri;
|
2015-03-24 17:29:16 +03:00
|
|
|
SerializeURI(aDomain, uri);
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < parents.Length(); i++) {
|
2015-11-02 08:53:26 +03:00
|
|
|
Unused << parents[i]->SendDomainSetChanged(aSetType, aChangeType, uri);
|
2015-03-24 17:29:16 +03:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2018-10-31 20:56:43 +03:00
|
|
|
DomainPolicy::DomainPolicy()
|
|
|
|
: mBlocklist(new DomainSet(BLOCKLIST)),
|
|
|
|
mSuperBlocklist(new DomainSet(SUPER_BLOCKLIST)),
|
|
|
|
mAllowlist(new DomainSet(ALLOWLIST)),
|
|
|
|
mSuperAllowlist(new DomainSet(SUPER_ALLOWLIST)) {
|
2015-07-04 04:29:00 +03:00
|
|
|
if (XRE_IsParentProcess()) {
|
2015-03-24 17:29:16 +03:00
|
|
|
BroadcastDomainSetChange(NO_TYPE, ACTIVATE_POLICY);
|
|
|
|
}
|
|
|
|
}
|
2013-11-13 04:43:35 +04:00
|
|
|
|
|
|
|
DomainPolicy::~DomainPolicy() {
|
|
|
|
// The SSM holds a strong ref to the DomainPolicy until Deactivate() is
|
|
|
|
// invoked, so we should never hit the destructor until that happens.
|
2018-10-31 20:56:43 +03:00
|
|
|
MOZ_ASSERT(!mBlocklist && !mSuperBlocklist && !mAllowlist &&
|
|
|
|
!mSuperAllowlist);
|
2013-11-13 04:43:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2018-10-31 20:56:43 +03:00
|
|
|
DomainPolicy::GetBlocklist(nsIDomainSet** aSet) {
|
|
|
|
nsCOMPtr<nsIDomainSet> set = mBlocklist.get();
|
2013-11-13 04:43:35 +04:00
|
|
|
set.forget(aSet);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2018-10-31 20:56:43 +03:00
|
|
|
DomainPolicy::GetSuperBlocklist(nsIDomainSet** aSet) {
|
|
|
|
nsCOMPtr<nsIDomainSet> set = mSuperBlocklist.get();
|
2013-11-13 04:43:35 +04:00
|
|
|
set.forget(aSet);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2018-10-31 20:56:43 +03:00
|
|
|
DomainPolicy::GetAllowlist(nsIDomainSet** aSet) {
|
|
|
|
nsCOMPtr<nsIDomainSet> set = mAllowlist.get();
|
2013-11-13 04:43:35 +04:00
|
|
|
set.forget(aSet);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2018-10-31 20:56:43 +03:00
|
|
|
DomainPolicy::GetSuperAllowlist(nsIDomainSet** aSet) {
|
|
|
|
nsCOMPtr<nsIDomainSet> set = mSuperAllowlist.get();
|
2013-11-13 04:43:35 +04:00
|
|
|
set.forget(aSet);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
DomainPolicy::Deactivate() {
|
|
|
|
// Clear the hashtables first to free up memory, since script might
|
|
|
|
// hold the doomed sets alive indefinitely.
|
2018-10-31 20:56:43 +03:00
|
|
|
mBlocklist->Clear();
|
|
|
|
mSuperBlocklist->Clear();
|
|
|
|
mAllowlist->Clear();
|
|
|
|
mSuperAllowlist->Clear();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-11-13 04:43:35 +04:00
|
|
|
// Null them out.
|
2018-10-31 20:56:43 +03:00
|
|
|
mBlocklist = nullptr;
|
|
|
|
mSuperBlocklist = nullptr;
|
|
|
|
mAllowlist = nullptr;
|
|
|
|
mSuperAllowlist = nullptr;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-11-13 04:43:35 +04:00
|
|
|
// Inform the SSM.
|
2015-03-24 17:29:16 +03:00
|
|
|
nsScriptSecurityManager* ssm =
|
|
|
|
nsScriptSecurityManager::GetScriptSecurityManager();
|
|
|
|
if (ssm) {
|
|
|
|
ssm->DeactivateDomainPolicy();
|
|
|
|
}
|
2015-07-04 04:29:00 +03:00
|
|
|
if (XRE_IsParentProcess()) {
|
2015-03-24 17:29:16 +03:00
|
|
|
BroadcastDomainSetChange(NO_TYPE, DEACTIVATE_POLICY);
|
|
|
|
}
|
2013-11-13 04:43:35 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-03-24 17:29:16 +03:00
|
|
|
void DomainPolicy::CloneDomainPolicy(DomainPolicyClone* aClone) {
|
|
|
|
aClone->active() = true;
|
2018-10-31 20:56:43 +03:00
|
|
|
mBlocklist->CloneSet(&aClone->blocklist());
|
|
|
|
mSuperBlocklist->CloneSet(&aClone->superBlocklist());
|
|
|
|
mAllowlist->CloneSet(&aClone->allowlist());
|
|
|
|
mSuperAllowlist->CloneSet(&aClone->superAllowlist());
|
2015-03-24 17:29:16 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2019-07-10 06:24:29 +03:00
|
|
|
static void CopyURIs(const nsTArray<URIParams>& aDomains, nsIDomainSet* aSet) {
|
2015-03-24 17:29:16 +03:00
|
|
|
for (uint32_t i = 0; i < aDomains.Length(); i++) {
|
|
|
|
nsCOMPtr<nsIURI> uri = DeserializeURI(aDomains[i]);
|
|
|
|
aSet->Add(uri);
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2017-02-05 08:52:38 +03:00
|
|
|
void DomainPolicy::ApplyClone(const DomainPolicyClone* aClone) {
|
2018-10-31 20:56:43 +03:00
|
|
|
CopyURIs(aClone->blocklist(), mBlocklist);
|
|
|
|
CopyURIs(aClone->allowlist(), mAllowlist);
|
|
|
|
CopyURIs(aClone->superBlocklist(), mSuperBlocklist);
|
|
|
|
CopyURIs(aClone->superAllowlist(), mSuperAllowlist);
|
2015-03-24 17:29:16 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-11-13 04:43:35 +04:00
|
|
|
static already_AddRefed<nsIURI> GetCanonicalClone(nsIURI* aURI) {
|
|
|
|
nsCOMPtr<nsIURI> clone;
|
2018-02-26 22:43:45 +03:00
|
|
|
nsresult rv = NS_MutateURI(aURI)
|
|
|
|
.SetUserPass(EmptyCString())
|
|
|
|
.SetPathQueryRef(EmptyCString())
|
|
|
|
.Finalize(clone);
|
2013-11-13 04:43:35 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
|
|
|
return clone.forget();
|
|
|
|
}
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(DomainSet, nsIDomainSet)
|
2013-11-13 04:43:35 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
DomainSet::Add(nsIURI* aDomain) {
|
|
|
|
nsCOMPtr<nsIURI> clone = GetCanonicalClone(aDomain);
|
|
|
|
NS_ENSURE_TRUE(clone, NS_ERROR_FAILURE);
|
|
|
|
mHashTable.PutEntry(clone);
|
2015-07-04 04:29:00 +03:00
|
|
|
if (XRE_IsParentProcess())
|
2015-03-24 17:29:16 +03:00
|
|
|
return BroadcastDomainSetChange(mType, ADD_DOMAIN, aDomain);
|
|
|
|
|
2013-11-13 04:43:35 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
DomainSet::Remove(nsIURI* aDomain) {
|
|
|
|
nsCOMPtr<nsIURI> clone = GetCanonicalClone(aDomain);
|
|
|
|
NS_ENSURE_TRUE(clone, NS_ERROR_FAILURE);
|
|
|
|
mHashTable.RemoveEntry(clone);
|
2015-07-04 04:29:00 +03:00
|
|
|
if (XRE_IsParentProcess())
|
2015-03-24 17:29:16 +03:00
|
|
|
return BroadcastDomainSetChange(mType, REMOVE_DOMAIN, aDomain);
|
|
|
|
|
2013-11-13 04:43:35 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
DomainSet::Clear() {
|
|
|
|
mHashTable.Clear();
|
2015-07-04 04:29:00 +03:00
|
|
|
if (XRE_IsParentProcess())
|
2015-03-24 17:29:16 +03:00
|
|
|
return BroadcastDomainSetChange(mType, CLEAR_DOMAINS);
|
|
|
|
|
2013-11-13 04:43:35 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
DomainSet::Contains(nsIURI* aDomain, bool* aContains) {
|
|
|
|
*aContains = false;
|
|
|
|
nsCOMPtr<nsIURI> clone = GetCanonicalClone(aDomain);
|
|
|
|
NS_ENSURE_TRUE(clone, NS_ERROR_FAILURE);
|
|
|
|
*aContains = mHashTable.Contains(clone);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
DomainSet::ContainsSuperDomain(nsIURI* aDomain, bool* aContains) {
|
|
|
|
*aContains = false;
|
|
|
|
nsCOMPtr<nsIURI> clone = GetCanonicalClone(aDomain);
|
|
|
|
NS_ENSURE_TRUE(clone, NS_ERROR_FAILURE);
|
|
|
|
nsAutoCString domain;
|
|
|
|
nsresult rv = clone->GetHost(domain);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
while (true) {
|
|
|
|
// Check the current domain.
|
|
|
|
if (mHashTable.Contains(clone)) {
|
|
|
|
*aContains = true;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Chop off everything before the first dot, or break if there are no
|
|
|
|
// dots left.
|
|
|
|
int32_t index = domain.Find(".");
|
|
|
|
if (index == kNotFound) break;
|
|
|
|
domain.Assign(Substring(domain, index + 1));
|
|
|
|
rv = NS_MutateURI(clone).SetHost(domain).Finalize(clone);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2013-11-13 04:43:35 +04:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
// No match.
|
2013-11-13 04:43:35 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2019-07-10 06:24:29 +03:00
|
|
|
void DomainSet::CloneSet(nsTArray<URIParams>* aDomains) {
|
2015-07-14 14:58:00 +03:00
|
|
|
for (auto iter = mHashTable.Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
nsIURI* key = iter.Get()->GetKey();
|
|
|
|
|
|
|
|
URIParams uri;
|
|
|
|
SerializeURI(key, uri);
|
|
|
|
|
|
|
|
aDomains->AppendElement(uri);
|
|
|
|
}
|
2015-03-24 17:29:16 +03:00
|
|
|
}
|
|
|
|
|
2013-11-13 04:43:35 +04:00
|
|
|
} /* namespace mozilla */
|