mirror of
https://github.com/mozilla/gecko-dev.git
synced 2025-01-26 23:23:33 +00:00
Bug 1349512 - Move OriginAttributes class in separate files, r=qdot
--HG-- rename : caps/BasePrincipal.cpp => caps/OriginAttributes.cpp rename : caps/BasePrincipal.h => caps/OriginAttributes.h
This commit is contained in:
parent
07103b0a43
commit
8d7c2746ea
@ -12,7 +12,6 @@
|
||||
#endif
|
||||
#include "nsIAddonPolicyService.h"
|
||||
#include "nsIContentSecurityPolicy.h"
|
||||
#include "nsIEffectiveTLDService.h"
|
||||
#include "nsIObjectInputStream.h"
|
||||
#include "nsIObjectOutputStream.h"
|
||||
|
||||
@ -25,279 +24,10 @@
|
||||
|
||||
#include "mozilla/dom/ChromeUtils.h"
|
||||
#include "mozilla/dom/CSPDictionariesBinding.h"
|
||||
#include "mozilla/dom/quota/QuotaManager.h"
|
||||
#include "mozilla/dom/ToJSValue.h"
|
||||
#include "mozilla/dom/URLSearchParams.h"
|
||||
|
||||
namespace mozilla {
|
||||
|
||||
using dom::URLParams;
|
||||
|
||||
bool OriginAttributes::sFirstPartyIsolation = false;
|
||||
bool OriginAttributes::sRestrictedOpenerAccess = false;
|
||||
|
||||
void
|
||||
OriginAttributes::InitPrefs()
|
||||
{
|
||||
MOZ_ASSERT(NS_IsMainThread());
|
||||
static bool sInited = false;
|
||||
if (!sInited) {
|
||||
sInited = true;
|
||||
Preferences::AddBoolVarCache(&sFirstPartyIsolation,
|
||||
"privacy.firstparty.isolate");
|
||||
Preferences::AddBoolVarCache(&sRestrictedOpenerAccess,
|
||||
"privacy.firstparty.isolate.restrict_opener_access");
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
OriginAttributes::SetFirstPartyDomain(const bool aIsTopLevelDocument,
|
||||
nsIURI* aURI)
|
||||
{
|
||||
bool isFirstPartyEnabled = IsFirstPartyEnabled();
|
||||
|
||||
// If the pref is off or this is not a top level load, bail out.
|
||||
if (!isFirstPartyEnabled || !aIsTopLevelDocument) {
|
||||
return;
|
||||
}
|
||||
|
||||
nsCOMPtr<nsIEffectiveTLDService> tldService =
|
||||
do_GetService(NS_EFFECTIVETLDSERVICE_CONTRACTID);
|
||||
MOZ_ASSERT(tldService);
|
||||
if (!tldService) {
|
||||
return;
|
||||
}
|
||||
|
||||
nsAutoCString baseDomain;
|
||||
nsresult rv = tldService->GetBaseDomain(aURI, 0, baseDomain);
|
||||
if (NS_FAILED(rv)) {
|
||||
nsAutoCString scheme;
|
||||
rv = aURI->GetScheme(scheme);
|
||||
NS_ENSURE_SUCCESS_VOID(rv);
|
||||
if (scheme.EqualsLiteral("about")) {
|
||||
baseDomain.AssignLiteral(ABOUT_URI_FIRST_PARTY_DOMAIN);
|
||||
}
|
||||
}
|
||||
|
||||
mFirstPartyDomain = NS_ConvertUTF8toUTF16(baseDomain);
|
||||
}
|
||||
|
||||
void
|
||||
OriginAttributes::SetFirstPartyDomain(const bool aIsTopLevelDocument,
|
||||
const nsACString& aDomain)
|
||||
{
|
||||
bool isFirstPartyEnabled = IsFirstPartyEnabled();
|
||||
|
||||
// If the pref is off or this is not a top level load, bail out.
|
||||
if (!isFirstPartyEnabled || !aIsTopLevelDocument) {
|
||||
return;
|
||||
}
|
||||
|
||||
mFirstPartyDomain = NS_ConvertUTF8toUTF16(aDomain);
|
||||
}
|
||||
|
||||
void
|
||||
OriginAttributes::CreateSuffix(nsACString& aStr) const
|
||||
{
|
||||
URLParams params;
|
||||
nsAutoString value;
|
||||
|
||||
//
|
||||
// Important: While serializing any string-valued attributes, perform a
|
||||
// release-mode assertion to make sure that they don't contain characters that
|
||||
// will break the quota manager when it uses the serialization for file
|
||||
// naming.
|
||||
//
|
||||
|
||||
if (mAppId != nsIScriptSecurityManager::NO_APP_ID) {
|
||||
value.AppendInt(mAppId);
|
||||
params.Set(NS_LITERAL_STRING("appId"), value);
|
||||
}
|
||||
|
||||
if (mInIsolatedMozBrowser) {
|
||||
params.Set(NS_LITERAL_STRING("inBrowser"), NS_LITERAL_STRING("1"));
|
||||
}
|
||||
|
||||
if (mUserContextId != nsIScriptSecurityManager::DEFAULT_USER_CONTEXT_ID) {
|
||||
value.Truncate();
|
||||
value.AppendInt(mUserContextId);
|
||||
params.Set(NS_LITERAL_STRING("userContextId"), value);
|
||||
}
|
||||
|
||||
|
||||
if (mPrivateBrowsingId) {
|
||||
value.Truncate();
|
||||
value.AppendInt(mPrivateBrowsingId);
|
||||
params.Set(NS_LITERAL_STRING("privateBrowsingId"), value);
|
||||
}
|
||||
|
||||
if (!mFirstPartyDomain.IsEmpty()) {
|
||||
MOZ_RELEASE_ASSERT(mFirstPartyDomain.FindCharInSet(dom::quota::QuotaManager::kReplaceChars) == kNotFound);
|
||||
params.Set(NS_LITERAL_STRING("firstPartyDomain"), mFirstPartyDomain);
|
||||
}
|
||||
|
||||
aStr.Truncate();
|
||||
|
||||
params.Serialize(value);
|
||||
if (!value.IsEmpty()) {
|
||||
aStr.AppendLiteral("^");
|
||||
aStr.Append(NS_ConvertUTF16toUTF8(value));
|
||||
}
|
||||
|
||||
// In debug builds, check the whole string for illegal characters too (just in case).
|
||||
#ifdef DEBUG
|
||||
nsAutoCString str;
|
||||
str.Assign(aStr);
|
||||
MOZ_ASSERT(str.FindCharInSet(dom::quota::QuotaManager::kReplaceChars) == kNotFound);
|
||||
#endif
|
||||
}
|
||||
|
||||
void
|
||||
OriginAttributes::CreateAnonymizedSuffix(nsACString& aStr) const
|
||||
{
|
||||
OriginAttributes attrs = *this;
|
||||
|
||||
if (!attrs.mFirstPartyDomain.IsEmpty()) {
|
||||
attrs.mFirstPartyDomain.AssignLiteral("_anonymizedFirstPartyDomain_");
|
||||
}
|
||||
|
||||
attrs.CreateSuffix(aStr);
|
||||
}
|
||||
|
||||
namespace {
|
||||
|
||||
class MOZ_STACK_CLASS PopulateFromSuffixIterator final
|
||||
: public URLParams::ForEachIterator
|
||||
{
|
||||
public:
|
||||
explicit PopulateFromSuffixIterator(OriginAttributes* aOriginAttributes)
|
||||
: mOriginAttributes(aOriginAttributes)
|
||||
{
|
||||
MOZ_ASSERT(aOriginAttributes);
|
||||
// If mPrivateBrowsingId is passed in as >0 and is not present in the suffix,
|
||||
// then it will remain >0 when it should be 0 according to the suffix. Set to 0 before
|
||||
// iterating to fix this.
|
||||
mOriginAttributes->mPrivateBrowsingId = 0;
|
||||
}
|
||||
|
||||
bool URLParamsIterator(const nsString& aName,
|
||||
const nsString& aValue) override
|
||||
{
|
||||
if (aName.EqualsLiteral("appId")) {
|
||||
nsresult rv;
|
||||
int64_t val = aValue.ToInteger64(&rv);
|
||||
NS_ENSURE_SUCCESS(rv, false);
|
||||
NS_ENSURE_TRUE(val <= UINT32_MAX, false);
|
||||
mOriginAttributes->mAppId = static_cast<uint32_t>(val);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
if (aName.EqualsLiteral("inBrowser")) {
|
||||
if (!aValue.EqualsLiteral("1")) {
|
||||
return false;
|
||||
}
|
||||
|
||||
mOriginAttributes->mInIsolatedMozBrowser = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
if (aName.EqualsLiteral("addonId")) {
|
||||
// No longer supported. Silently ignore so that legacy origin strings
|
||||
// don't cause failures.
|
||||
return true;
|
||||
}
|
||||
|
||||
if (aName.EqualsLiteral("userContextId")) {
|
||||
nsresult rv;
|
||||
int64_t val = aValue.ToInteger64(&rv);
|
||||
NS_ENSURE_SUCCESS(rv, false);
|
||||
NS_ENSURE_TRUE(val <= UINT32_MAX, false);
|
||||
mOriginAttributes->mUserContextId = static_cast<uint32_t>(val);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
if (aName.EqualsLiteral("privateBrowsingId")) {
|
||||
nsresult rv;
|
||||
int64_t val = aValue.ToInteger64(&rv);
|
||||
NS_ENSURE_SUCCESS(rv, false);
|
||||
NS_ENSURE_TRUE(val >= 0 && val <= UINT32_MAX, false);
|
||||
mOriginAttributes->mPrivateBrowsingId = static_cast<uint32_t>(val);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
if (aName.EqualsLiteral("firstPartyDomain")) {
|
||||
MOZ_RELEASE_ASSERT(mOriginAttributes->mFirstPartyDomain.IsEmpty());
|
||||
mOriginAttributes->mFirstPartyDomain.Assign(aValue);
|
||||
return true;
|
||||
}
|
||||
|
||||
// No other attributes are supported.
|
||||
return false;
|
||||
}
|
||||
|
||||
private:
|
||||
OriginAttributes* mOriginAttributes;
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
bool
|
||||
OriginAttributes::PopulateFromSuffix(const nsACString& aStr)
|
||||
{
|
||||
if (aStr.IsEmpty()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (aStr[0] != '^') {
|
||||
return false;
|
||||
}
|
||||
|
||||
URLParams params;
|
||||
params.ParseInput(Substring(aStr, 1, aStr.Length() - 1));
|
||||
|
||||
PopulateFromSuffixIterator iterator(this);
|
||||
return params.ForEach(iterator);
|
||||
}
|
||||
|
||||
bool
|
||||
OriginAttributes::PopulateFromOrigin(const nsACString& aOrigin,
|
||||
nsACString& aOriginNoSuffix)
|
||||
{
|
||||
// RFindChar is only available on nsCString.
|
||||
nsCString origin(aOrigin);
|
||||
int32_t pos = origin.RFindChar('^');
|
||||
|
||||
if (pos == kNotFound) {
|
||||
aOriginNoSuffix = origin;
|
||||
return true;
|
||||
}
|
||||
|
||||
aOriginNoSuffix = Substring(origin, 0, pos);
|
||||
return PopulateFromSuffix(Substring(origin, pos));
|
||||
}
|
||||
|
||||
void
|
||||
OriginAttributes::SyncAttributesWithPrivateBrowsing(bool aInPrivateBrowsing)
|
||||
{
|
||||
mPrivateBrowsingId = aInPrivateBrowsing ? 1 : 0;
|
||||
}
|
||||
|
||||
/* static */
|
||||
bool
|
||||
OriginAttributes::IsPrivateBrowsing(const nsACString& aOrigin)
|
||||
{
|
||||
nsAutoCString dummy;
|
||||
OriginAttributes attrs;
|
||||
if (NS_WARN_IF(!attrs.PopulateFromOrigin(aOrigin, dummy))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return !!attrs.mPrivateBrowsingId;
|
||||
}
|
||||
|
||||
BasePrincipal::BasePrincipal(PrincipalKind aKind)
|
||||
: mKind(aKind)
|
||||
, mDomainSet(false)
|
||||
|
@ -10,9 +10,7 @@
|
||||
#include "nsJSPrincipals.h"
|
||||
|
||||
#include "mozilla/Attributes.h"
|
||||
#include "mozilla/dom/ChromeUtils.h"
|
||||
#include "mozilla/dom/ChromeUtilsBinding.h"
|
||||
#include "nsIScriptSecurityManager.h"
|
||||
#include "mozilla/OriginAttributes.h"
|
||||
|
||||
class nsIContentSecurityPolicy;
|
||||
class nsIObjectOutputStream;
|
||||
@ -23,174 +21,6 @@ class ExpandedPrincipal;
|
||||
|
||||
namespace mozilla {
|
||||
|
||||
// Base OriginAttributes class. This has several subclass flavors, and is not
|
||||
// directly constructable itself.
|
||||
class OriginAttributes : public dom::OriginAttributesDictionary
|
||||
{
|
||||
public:
|
||||
OriginAttributes() {}
|
||||
|
||||
OriginAttributes(uint32_t aAppId, bool aInIsolatedMozBrowser)
|
||||
{
|
||||
mAppId = aAppId;
|
||||
mInIsolatedMozBrowser = aInIsolatedMozBrowser;
|
||||
}
|
||||
|
||||
explicit OriginAttributes(const OriginAttributesDictionary& aOther)
|
||||
: OriginAttributesDictionary(aOther)
|
||||
{}
|
||||
|
||||
void SetFirstPartyDomain(const bool aIsTopLevelDocument, nsIURI* aURI);
|
||||
void SetFirstPartyDomain(const bool aIsTopLevelDocument, const nsACString& aDomain);
|
||||
|
||||
enum {
|
||||
STRIP_FIRST_PARTY_DOMAIN = 0x01,
|
||||
STRIP_USER_CONTEXT_ID = 0x02,
|
||||
};
|
||||
|
||||
inline void StripAttributes(uint32_t aFlags)
|
||||
{
|
||||
if (aFlags & STRIP_FIRST_PARTY_DOMAIN) {
|
||||
mFirstPartyDomain.Truncate();
|
||||
}
|
||||
|
||||
if (aFlags & STRIP_USER_CONTEXT_ID) {
|
||||
mUserContextId = nsIScriptSecurityManager::DEFAULT_USER_CONTEXT_ID;
|
||||
}
|
||||
}
|
||||
|
||||
bool operator==(const OriginAttributes& aOther) const
|
||||
{
|
||||
return mAppId == aOther.mAppId &&
|
||||
mInIsolatedMozBrowser == aOther.mInIsolatedMozBrowser &&
|
||||
mUserContextId == aOther.mUserContextId &&
|
||||
mPrivateBrowsingId == aOther.mPrivateBrowsingId &&
|
||||
mFirstPartyDomain == aOther.mFirstPartyDomain;
|
||||
}
|
||||
|
||||
bool operator!=(const OriginAttributes& aOther) const
|
||||
{
|
||||
return !(*this == aOther);
|
||||
}
|
||||
|
||||
// Serializes/Deserializes non-default values into the suffix format, i.e.
|
||||
// |!key1=value1&key2=value2|. If there are no non-default attributes, this
|
||||
// returns an empty string.
|
||||
void CreateSuffix(nsACString& aStr) const;
|
||||
|
||||
// Don't use this method for anything else than debugging!
|
||||
void CreateAnonymizedSuffix(nsACString& aStr) const;
|
||||
|
||||
MOZ_MUST_USE bool PopulateFromSuffix(const nsACString& aStr);
|
||||
|
||||
// Populates the attributes from a string like
|
||||
// |uri!key1=value1&key2=value2| and returns the uri without the suffix.
|
||||
MOZ_MUST_USE bool PopulateFromOrigin(const nsACString& aOrigin,
|
||||
nsACString& aOriginNoSuffix);
|
||||
|
||||
// Helper function to match mIsPrivateBrowsing to existing private browsing
|
||||
// flags. Once all other flags are removed, this can be removed too.
|
||||
void SyncAttributesWithPrivateBrowsing(bool aInPrivateBrowsing);
|
||||
|
||||
// check if "privacy.firstparty.isolate" is enabled.
|
||||
static inline bool IsFirstPartyEnabled()
|
||||
{
|
||||
return sFirstPartyIsolation;
|
||||
}
|
||||
|
||||
// check if the access of window.opener across different FPDs is restricted.
|
||||
// We only restrict the access of window.opener when first party isolation
|
||||
// is enabled and "privacy.firstparty.isolate.restrict_opener_access" is on.
|
||||
static inline bool IsRestrictOpenerAccessForFPI()
|
||||
{
|
||||
// We always want to restrict window.opener if first party isolation is
|
||||
// disabled.
|
||||
return !sFirstPartyIsolation || sRestrictedOpenerAccess;
|
||||
}
|
||||
|
||||
// returns true if the originAttributes suffix has mPrivateBrowsingId value
|
||||
// different than 0.
|
||||
static bool IsPrivateBrowsing(const nsACString& aOrigin);
|
||||
|
||||
static void InitPrefs();
|
||||
|
||||
private:
|
||||
static bool sFirstPartyIsolation;
|
||||
static bool sRestrictedOpenerAccess;
|
||||
};
|
||||
|
||||
class OriginAttributesPattern : public dom::OriginAttributesPatternDictionary
|
||||
{
|
||||
public:
|
||||
// To convert a JSON string to an OriginAttributesPattern, do the following:
|
||||
//
|
||||
// OriginAttributesPattern pattern;
|
||||
// if (!pattern.Init(aJSONString)) {
|
||||
// ... // handle failure.
|
||||
// }
|
||||
OriginAttributesPattern() {}
|
||||
|
||||
explicit OriginAttributesPattern(const OriginAttributesPatternDictionary& aOther)
|
||||
: OriginAttributesPatternDictionary(aOther) {}
|
||||
|
||||
// Performs a match of |aAttrs| against this pattern.
|
||||
bool Matches(const OriginAttributes& aAttrs) const
|
||||
{
|
||||
if (mAppId.WasPassed() && mAppId.Value() != aAttrs.mAppId) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (mInIsolatedMozBrowser.WasPassed() && mInIsolatedMozBrowser.Value() != aAttrs.mInIsolatedMozBrowser) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (mUserContextId.WasPassed() && mUserContextId.Value() != aAttrs.mUserContextId) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (mPrivateBrowsingId.WasPassed() && mPrivateBrowsingId.Value() != aAttrs.mPrivateBrowsingId) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (mFirstPartyDomain.WasPassed() && mFirstPartyDomain.Value() != aAttrs.mFirstPartyDomain) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Overlaps(const OriginAttributesPattern& aOther) const
|
||||
{
|
||||
if (mAppId.WasPassed() && aOther.mAppId.WasPassed() &&
|
||||
mAppId.Value() != aOther.mAppId.Value()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (mInIsolatedMozBrowser.WasPassed() &&
|
||||
aOther.mInIsolatedMozBrowser.WasPassed() &&
|
||||
mInIsolatedMozBrowser.Value() != aOther.mInIsolatedMozBrowser.Value()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (mUserContextId.WasPassed() && aOther.mUserContextId.WasPassed() &&
|
||||
mUserContextId.Value() != aOther.mUserContextId.Value()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (mPrivateBrowsingId.WasPassed() && aOther.mPrivateBrowsingId.WasPassed() &&
|
||||
mPrivateBrowsingId.Value() != aOther.mPrivateBrowsingId.Value()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (mFirstPartyDomain.WasPassed() && aOther.mFirstPartyDomain.WasPassed() &&
|
||||
mFirstPartyDomain.Value() != aOther.mFirstPartyDomain.Value()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
/*
|
||||
* Base class from which all nsIPrincipal implementations inherit. Use this for
|
||||
* default implementations and other commonalities between principal
|
||||
|
283
caps/OriginAttributes.cpp
Normal file
283
caps/OriginAttributes.cpp
Normal file
@ -0,0 +1,283 @@
|
||||
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
||||
/* vim: set ts=2 sw=2 et tw=80: */
|
||||
/* 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 "mozilla/OriginAttributes.h"
|
||||
#include "mozilla/Preferences.h"
|
||||
#include "mozilla/dom/URLSearchParams.h"
|
||||
#include "mozilla/dom/quota/QuotaManager.h"
|
||||
#include "nsIEffectiveTLDService.h"
|
||||
#include "nsIURI.h"
|
||||
|
||||
namespace mozilla {
|
||||
|
||||
using dom::URLParams;
|
||||
|
||||
bool OriginAttributes::sFirstPartyIsolation = false;
|
||||
bool OriginAttributes::sRestrictedOpenerAccess = false;
|
||||
|
||||
void
|
||||
OriginAttributes::InitPrefs()
|
||||
{
|
||||
MOZ_ASSERT(NS_IsMainThread());
|
||||
static bool sInited = false;
|
||||
if (!sInited) {
|
||||
sInited = true;
|
||||
Preferences::AddBoolVarCache(&sFirstPartyIsolation,
|
||||
"privacy.firstparty.isolate");
|
||||
Preferences::AddBoolVarCache(&sRestrictedOpenerAccess,
|
||||
"privacy.firstparty.isolate.restrict_opener_access");
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
OriginAttributes::SetFirstPartyDomain(const bool aIsTopLevelDocument,
|
||||
nsIURI* aURI)
|
||||
{
|
||||
bool isFirstPartyEnabled = IsFirstPartyEnabled();
|
||||
|
||||
// If the pref is off or this is not a top level load, bail out.
|
||||
if (!isFirstPartyEnabled || !aIsTopLevelDocument) {
|
||||
return;
|
||||
}
|
||||
|
||||
nsCOMPtr<nsIEffectiveTLDService> tldService =
|
||||
do_GetService(NS_EFFECTIVETLDSERVICE_CONTRACTID);
|
||||
MOZ_ASSERT(tldService);
|
||||
if (!tldService) {
|
||||
return;
|
||||
}
|
||||
|
||||
nsAutoCString baseDomain;
|
||||
nsresult rv = tldService->GetBaseDomain(aURI, 0, baseDomain);
|
||||
if (NS_FAILED(rv)) {
|
||||
nsAutoCString scheme;
|
||||
rv = aURI->GetScheme(scheme);
|
||||
NS_ENSURE_SUCCESS_VOID(rv);
|
||||
if (scheme.EqualsLiteral("about")) {
|
||||
baseDomain.AssignLiteral(ABOUT_URI_FIRST_PARTY_DOMAIN);
|
||||
}
|
||||
}
|
||||
|
||||
mFirstPartyDomain = NS_ConvertUTF8toUTF16(baseDomain);
|
||||
}
|
||||
|
||||
void
|
||||
OriginAttributes::SetFirstPartyDomain(const bool aIsTopLevelDocument,
|
||||
const nsACString& aDomain)
|
||||
{
|
||||
bool isFirstPartyEnabled = IsFirstPartyEnabled();
|
||||
|
||||
// If the pref is off or this is not a top level load, bail out.
|
||||
if (!isFirstPartyEnabled || !aIsTopLevelDocument) {
|
||||
return;
|
||||
}
|
||||
|
||||
mFirstPartyDomain = NS_ConvertUTF8toUTF16(aDomain);
|
||||
}
|
||||
|
||||
void
|
||||
OriginAttributes::CreateSuffix(nsACString& aStr) const
|
||||
{
|
||||
URLParams params;
|
||||
nsAutoString value;
|
||||
|
||||
//
|
||||
// Important: While serializing any string-valued attributes, perform a
|
||||
// release-mode assertion to make sure that they don't contain characters that
|
||||
// will break the quota manager when it uses the serialization for file
|
||||
// naming.
|
||||
//
|
||||
|
||||
if (mAppId != nsIScriptSecurityManager::NO_APP_ID) {
|
||||
value.AppendInt(mAppId);
|
||||
params.Set(NS_LITERAL_STRING("appId"), value);
|
||||
}
|
||||
|
||||
if (mInIsolatedMozBrowser) {
|
||||
params.Set(NS_LITERAL_STRING("inBrowser"), NS_LITERAL_STRING("1"));
|
||||
}
|
||||
|
||||
if (mUserContextId != nsIScriptSecurityManager::DEFAULT_USER_CONTEXT_ID) {
|
||||
value.Truncate();
|
||||
value.AppendInt(mUserContextId);
|
||||
params.Set(NS_LITERAL_STRING("userContextId"), value);
|
||||
}
|
||||
|
||||
|
||||
if (mPrivateBrowsingId) {
|
||||
value.Truncate();
|
||||
value.AppendInt(mPrivateBrowsingId);
|
||||
params.Set(NS_LITERAL_STRING("privateBrowsingId"), value);
|
||||
}
|
||||
|
||||
if (!mFirstPartyDomain.IsEmpty()) {
|
||||
MOZ_RELEASE_ASSERT(mFirstPartyDomain.FindCharInSet(dom::quota::QuotaManager::kReplaceChars) == kNotFound);
|
||||
params.Set(NS_LITERAL_STRING("firstPartyDomain"), mFirstPartyDomain);
|
||||
}
|
||||
|
||||
aStr.Truncate();
|
||||
|
||||
params.Serialize(value);
|
||||
if (!value.IsEmpty()) {
|
||||
aStr.AppendLiteral("^");
|
||||
aStr.Append(NS_ConvertUTF16toUTF8(value));
|
||||
}
|
||||
|
||||
// In debug builds, check the whole string for illegal characters too (just in case).
|
||||
#ifdef DEBUG
|
||||
nsAutoCString str;
|
||||
str.Assign(aStr);
|
||||
MOZ_ASSERT(str.FindCharInSet(dom::quota::QuotaManager::kReplaceChars) == kNotFound);
|
||||
#endif
|
||||
}
|
||||
|
||||
void
|
||||
OriginAttributes::CreateAnonymizedSuffix(nsACString& aStr) const
|
||||
{
|
||||
OriginAttributes attrs = *this;
|
||||
|
||||
if (!attrs.mFirstPartyDomain.IsEmpty()) {
|
||||
attrs.mFirstPartyDomain.AssignLiteral("_anonymizedFirstPartyDomain_");
|
||||
}
|
||||
|
||||
attrs.CreateSuffix(aStr);
|
||||
}
|
||||
|
||||
namespace {
|
||||
|
||||
class MOZ_STACK_CLASS PopulateFromSuffixIterator final
|
||||
: public URLParams::ForEachIterator
|
||||
{
|
||||
public:
|
||||
explicit PopulateFromSuffixIterator(OriginAttributes* aOriginAttributes)
|
||||
: mOriginAttributes(aOriginAttributes)
|
||||
{
|
||||
MOZ_ASSERT(aOriginAttributes);
|
||||
// If mPrivateBrowsingId is passed in as >0 and is not present in the suffix,
|
||||
// then it will remain >0 when it should be 0 according to the suffix. Set to 0 before
|
||||
// iterating to fix this.
|
||||
mOriginAttributes->mPrivateBrowsingId = 0;
|
||||
}
|
||||
|
||||
bool URLParamsIterator(const nsString& aName,
|
||||
const nsString& aValue) override
|
||||
{
|
||||
if (aName.EqualsLiteral("appId")) {
|
||||
nsresult rv;
|
||||
int64_t val = aValue.ToInteger64(&rv);
|
||||
NS_ENSURE_SUCCESS(rv, false);
|
||||
NS_ENSURE_TRUE(val <= UINT32_MAX, false);
|
||||
mOriginAttributes->mAppId = static_cast<uint32_t>(val);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
if (aName.EqualsLiteral("inBrowser")) {
|
||||
if (!aValue.EqualsLiteral("1")) {
|
||||
return false;
|
||||
}
|
||||
|
||||
mOriginAttributes->mInIsolatedMozBrowser = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
if (aName.EqualsLiteral("addonId")) {
|
||||
// No longer supported. Silently ignore so that legacy origin strings
|
||||
// don't cause failures.
|
||||
return true;
|
||||
}
|
||||
|
||||
if (aName.EqualsLiteral("userContextId")) {
|
||||
nsresult rv;
|
||||
int64_t val = aValue.ToInteger64(&rv);
|
||||
NS_ENSURE_SUCCESS(rv, false);
|
||||
NS_ENSURE_TRUE(val <= UINT32_MAX, false);
|
||||
mOriginAttributes->mUserContextId = static_cast<uint32_t>(val);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
if (aName.EqualsLiteral("privateBrowsingId")) {
|
||||
nsresult rv;
|
||||
int64_t val = aValue.ToInteger64(&rv);
|
||||
NS_ENSURE_SUCCESS(rv, false);
|
||||
NS_ENSURE_TRUE(val >= 0 && val <= UINT32_MAX, false);
|
||||
mOriginAttributes->mPrivateBrowsingId = static_cast<uint32_t>(val);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
if (aName.EqualsLiteral("firstPartyDomain")) {
|
||||
MOZ_RELEASE_ASSERT(mOriginAttributes->mFirstPartyDomain.IsEmpty());
|
||||
mOriginAttributes->mFirstPartyDomain.Assign(aValue);
|
||||
return true;
|
||||
}
|
||||
|
||||
// No other attributes are supported.
|
||||
return false;
|
||||
}
|
||||
|
||||
private:
|
||||
OriginAttributes* mOriginAttributes;
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
bool
|
||||
OriginAttributes::PopulateFromSuffix(const nsACString& aStr)
|
||||
{
|
||||
if (aStr.IsEmpty()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (aStr[0] != '^') {
|
||||
return false;
|
||||
}
|
||||
|
||||
URLParams params;
|
||||
params.ParseInput(Substring(aStr, 1, aStr.Length() - 1));
|
||||
|
||||
PopulateFromSuffixIterator iterator(this);
|
||||
return params.ForEach(iterator);
|
||||
}
|
||||
|
||||
bool
|
||||
OriginAttributes::PopulateFromOrigin(const nsACString& aOrigin,
|
||||
nsACString& aOriginNoSuffix)
|
||||
{
|
||||
// RFindChar is only available on nsCString.
|
||||
nsCString origin(aOrigin);
|
||||
int32_t pos = origin.RFindChar('^');
|
||||
|
||||
if (pos == kNotFound) {
|
||||
aOriginNoSuffix = origin;
|
||||
return true;
|
||||
}
|
||||
|
||||
aOriginNoSuffix = Substring(origin, 0, pos);
|
||||
return PopulateFromSuffix(Substring(origin, pos));
|
||||
}
|
||||
|
||||
void
|
||||
OriginAttributes::SyncAttributesWithPrivateBrowsing(bool aInPrivateBrowsing)
|
||||
{
|
||||
mPrivateBrowsingId = aInPrivateBrowsing ? 1 : 0;
|
||||
}
|
||||
|
||||
/* static */
|
||||
bool
|
||||
OriginAttributes::IsPrivateBrowsing(const nsACString& aOrigin)
|
||||
{
|
||||
nsAutoCString dummy;
|
||||
OriginAttributes attrs;
|
||||
if (NS_WARN_IF(!attrs.PopulateFromOrigin(aOrigin, dummy))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return !!attrs.mPrivateBrowsingId;
|
||||
}
|
||||
|
||||
} // namespace mozilla
|
184
caps/OriginAttributes.h
Normal file
184
caps/OriginAttributes.h
Normal file
@ -0,0 +1,184 @@
|
||||
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
||||
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
||||
/* 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/. */
|
||||
|
||||
#ifndef mozilla_OriginAttributes_h
|
||||
#define mozilla_OriginAttributes_h
|
||||
|
||||
#include "mozilla/dom/ChromeUtils.h"
|
||||
#include "mozilla/dom/ChromeUtilsBinding.h"
|
||||
#include "nsIScriptSecurityManager.h"
|
||||
|
||||
namespace mozilla {
|
||||
|
||||
class OriginAttributes : public dom::OriginAttributesDictionary
|
||||
{
|
||||
public:
|
||||
OriginAttributes() {}
|
||||
|
||||
OriginAttributes(uint32_t aAppId, bool aInIsolatedMozBrowser)
|
||||
{
|
||||
mAppId = aAppId;
|
||||
mInIsolatedMozBrowser = aInIsolatedMozBrowser;
|
||||
}
|
||||
|
||||
explicit OriginAttributes(const OriginAttributesDictionary& aOther)
|
||||
: OriginAttributesDictionary(aOther)
|
||||
{}
|
||||
|
||||
void SetFirstPartyDomain(const bool aIsTopLevelDocument, nsIURI* aURI);
|
||||
void SetFirstPartyDomain(const bool aIsTopLevelDocument, const nsACString& aDomain);
|
||||
|
||||
enum {
|
||||
STRIP_FIRST_PARTY_DOMAIN = 0x01,
|
||||
STRIP_USER_CONTEXT_ID = 0x02,
|
||||
};
|
||||
|
||||
inline void StripAttributes(uint32_t aFlags)
|
||||
{
|
||||
if (aFlags & STRIP_FIRST_PARTY_DOMAIN) {
|
||||
mFirstPartyDomain.Truncate();
|
||||
}
|
||||
|
||||
if (aFlags & STRIP_USER_CONTEXT_ID) {
|
||||
mUserContextId = nsIScriptSecurityManager::DEFAULT_USER_CONTEXT_ID;
|
||||
}
|
||||
}
|
||||
|
||||
bool operator==(const OriginAttributes& aOther) const
|
||||
{
|
||||
return mAppId == aOther.mAppId &&
|
||||
mInIsolatedMozBrowser == aOther.mInIsolatedMozBrowser &&
|
||||
mUserContextId == aOther.mUserContextId &&
|
||||
mPrivateBrowsingId == aOther.mPrivateBrowsingId &&
|
||||
mFirstPartyDomain == aOther.mFirstPartyDomain;
|
||||
}
|
||||
|
||||
bool operator!=(const OriginAttributes& aOther) const
|
||||
{
|
||||
return !(*this == aOther);
|
||||
}
|
||||
|
||||
// Serializes/Deserializes non-default values into the suffix format, i.e.
|
||||
// |!key1=value1&key2=value2|. If there are no non-default attributes, this
|
||||
// returns an empty string.
|
||||
void CreateSuffix(nsACString& aStr) const;
|
||||
|
||||
// Don't use this method for anything else than debugging!
|
||||
void CreateAnonymizedSuffix(nsACString& aStr) const;
|
||||
|
||||
MOZ_MUST_USE bool PopulateFromSuffix(const nsACString& aStr);
|
||||
|
||||
// Populates the attributes from a string like
|
||||
// |uri!key1=value1&key2=value2| and returns the uri without the suffix.
|
||||
MOZ_MUST_USE bool PopulateFromOrigin(const nsACString& aOrigin,
|
||||
nsACString& aOriginNoSuffix);
|
||||
|
||||
// Helper function to match mIsPrivateBrowsing to existing private browsing
|
||||
// flags. Once all other flags are removed, this can be removed too.
|
||||
void SyncAttributesWithPrivateBrowsing(bool aInPrivateBrowsing);
|
||||
|
||||
// check if "privacy.firstparty.isolate" is enabled.
|
||||
static inline bool IsFirstPartyEnabled()
|
||||
{
|
||||
return sFirstPartyIsolation;
|
||||
}
|
||||
|
||||
// check if the access of window.opener across different FPDs is restricted.
|
||||
// We only restrict the access of window.opener when first party isolation
|
||||
// is enabled and "privacy.firstparty.isolate.restrict_opener_access" is on.
|
||||
static inline bool IsRestrictOpenerAccessForFPI()
|
||||
{
|
||||
// We always want to restrict window.opener if first party isolation is
|
||||
// disabled.
|
||||
return !sFirstPartyIsolation || sRestrictedOpenerAccess;
|
||||
}
|
||||
|
||||
// returns true if the originAttributes suffix has mPrivateBrowsingId value
|
||||
// different than 0.
|
||||
static bool IsPrivateBrowsing(const nsACString& aOrigin);
|
||||
|
||||
static void InitPrefs();
|
||||
|
||||
private:
|
||||
static bool sFirstPartyIsolation;
|
||||
static bool sRestrictedOpenerAccess;
|
||||
};
|
||||
|
||||
class OriginAttributesPattern : public dom::OriginAttributesPatternDictionary
|
||||
{
|
||||
public:
|
||||
// To convert a JSON string to an OriginAttributesPattern, do the following:
|
||||
//
|
||||
// OriginAttributesPattern pattern;
|
||||
// if (!pattern.Init(aJSONString)) {
|
||||
// ... // handle failure.
|
||||
// }
|
||||
OriginAttributesPattern() {}
|
||||
|
||||
explicit OriginAttributesPattern(const OriginAttributesPatternDictionary& aOther)
|
||||
: OriginAttributesPatternDictionary(aOther) {}
|
||||
|
||||
// Performs a match of |aAttrs| against this pattern.
|
||||
bool Matches(const OriginAttributes& aAttrs) const
|
||||
{
|
||||
if (mAppId.WasPassed() && mAppId.Value() != aAttrs.mAppId) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (mInIsolatedMozBrowser.WasPassed() && mInIsolatedMozBrowser.Value() != aAttrs.mInIsolatedMozBrowser) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (mUserContextId.WasPassed() && mUserContextId.Value() != aAttrs.mUserContextId) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (mPrivateBrowsingId.WasPassed() && mPrivateBrowsingId.Value() != aAttrs.mPrivateBrowsingId) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (mFirstPartyDomain.WasPassed() && mFirstPartyDomain.Value() != aAttrs.mFirstPartyDomain) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Overlaps(const OriginAttributesPattern& aOther) const
|
||||
{
|
||||
if (mAppId.WasPassed() && aOther.mAppId.WasPassed() &&
|
||||
mAppId.Value() != aOther.mAppId.Value()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (mInIsolatedMozBrowser.WasPassed() &&
|
||||
aOther.mInIsolatedMozBrowser.WasPassed() &&
|
||||
mInIsolatedMozBrowser.Value() != aOther.mInIsolatedMozBrowser.Value()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (mUserContextId.WasPassed() && aOther.mUserContextId.WasPassed() &&
|
||||
mUserContextId.Value() != aOther.mUserContextId.Value()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (mPrivateBrowsingId.WasPassed() && aOther.mPrivateBrowsingId.WasPassed() &&
|
||||
mPrivateBrowsingId.Value() != aOther.mPrivateBrowsingId.Value()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (mFirstPartyDomain.WasPassed() && aOther.mFirstPartyDomain.WasPassed() &&
|
||||
mFirstPartyDomain.Value() != aOther.mFirstPartyDomain.Value()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace mozilla
|
||||
|
||||
#endif /* mozilla_OriginAttributes_h */
|
@ -30,7 +30,8 @@ EXPORTS += [
|
||||
]
|
||||
|
||||
EXPORTS.mozilla = [
|
||||
'BasePrincipal.h'
|
||||
'BasePrincipal.h',
|
||||
'OriginAttributes.h',
|
||||
]
|
||||
|
||||
SOURCES += [
|
||||
@ -47,6 +48,7 @@ UNIFIED_SOURCES += [
|
||||
'nsScriptSecurityManager.cpp',
|
||||
'NullPrincipal.cpp',
|
||||
'NullPrincipalURI.cpp',
|
||||
'OriginAttributes.cpp',
|
||||
'SystemPrincipal.cpp',
|
||||
]
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user