2017-03-22 20:45:40 +03:00
|
|
|
/* -*- 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"
|
2018-07-24 23:15:57 +03:00
|
|
|
#include "mozilla/dom/BlobURLProtocolHandler.h"
|
2017-03-22 20:45:40 +03:00
|
|
|
#include "mozilla/dom/URLSearchParams.h"
|
|
|
|
#include "mozilla/dom/quota/QuotaManager.h"
|
|
|
|
#include "nsIEffectiveTLDService.h"
|
|
|
|
#include "nsIURI.h"
|
2018-07-13 22:53:15 +03:00
|
|
|
#include "nsURLHelper.h"
|
2017-03-22 20:45:40 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
using dom::URLParams;
|
|
|
|
|
|
|
|
void OriginAttributes::SetFirstPartyDomain(const bool aIsTopLevelDocument,
|
2019-04-12 08:31:32 +03:00
|
|
|
nsIURI* aURI, bool aForced) {
|
2017-03-22 20:45:40 +03:00
|
|
|
bool isFirstPartyEnabled = IsFirstPartyEnabled();
|
|
|
|
|
2019-04-12 08:31:32 +03:00
|
|
|
// If the prefs are off or this is not a top level load, bail out.
|
|
|
|
if ((!isFirstPartyEnabled || !aIsTopLevelDocument) && !aForced) {
|
2017-03-22 20:45:40 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIEffectiveTLDService> tldService =
|
|
|
|
do_GetService(NS_EFFECTIVETLDSERVICE_CONTRACTID);
|
|
|
|
MOZ_ASSERT(tldService);
|
|
|
|
if (!tldService) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoCString baseDomain;
|
|
|
|
nsresult rv = tldService->GetBaseDomain(aURI, 0, baseDomain);
|
2017-04-12 09:47:48 +03:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
mFirstPartyDomain = NS_ConvertUTF8toUTF16(baseDomain);
|
|
|
|
return;
|
2017-03-22 20:45:40 +03:00
|
|
|
}
|
|
|
|
|
2018-07-13 22:53:15 +03:00
|
|
|
if (rv == NS_ERROR_HOST_IS_IP_ADDRESS) {
|
|
|
|
// If the host is an IPv4/IPv6 address, we still accept it as a
|
|
|
|
// valid firstPartyDomain.
|
|
|
|
nsAutoCString ipAddr;
|
|
|
|
rv = aURI->GetHost(ipAddr);
|
|
|
|
NS_ENSURE_SUCCESS_VOID(rv);
|
|
|
|
|
2019-04-08 09:33:04 +03:00
|
|
|
if (net_IsValidIPv6Addr(ipAddr)) {
|
2018-07-13 22:53:15 +03:00
|
|
|
// According to RFC2732, the host of an IPv6 address should be an
|
|
|
|
// IPv6reference. The GetHost() of nsIURI will only return the IPv6
|
|
|
|
// address. So, we need to convert it back to IPv6reference here.
|
|
|
|
mFirstPartyDomain.Truncate();
|
|
|
|
mFirstPartyDomain.AssignLiteral("[");
|
|
|
|
mFirstPartyDomain.Append(NS_ConvertUTF8toUTF16(ipAddr));
|
|
|
|
mFirstPartyDomain.AppendLiteral("]");
|
|
|
|
} else {
|
|
|
|
mFirstPartyDomain = NS_ConvertUTF8toUTF16(ipAddr);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-12 18:44:40 +03:00
|
|
|
// Saving isInsufficientDomainLevels before rv is overwritten.
|
|
|
|
bool isInsufficientDomainLevels = (rv == NS_ERROR_INSUFFICIENT_DOMAIN_LEVELS);
|
2017-04-12 09:47:48 +03:00
|
|
|
nsAutoCString scheme;
|
2019-09-27 17:05:13 +03:00
|
|
|
if (aURI) {
|
|
|
|
rv = aURI->GetScheme(scheme);
|
|
|
|
NS_ENSURE_SUCCESS_VOID(rv);
|
|
|
|
if (scheme.EqualsLiteral("about")) {
|
|
|
|
mFirstPartyDomain.AssignLiteral(ABOUT_URI_FIRST_PARTY_DOMAIN);
|
|
|
|
return;
|
|
|
|
}
|
2018-07-24 23:15:57 +03:00
|
|
|
}
|
|
|
|
|
2019-10-06 18:43:39 +03:00
|
|
|
// Add-on principals should never get any first-party domain
|
|
|
|
// attributes in order to guarantee their storage integrity when switching
|
|
|
|
// FPI on and off.
|
|
|
|
if (scheme.EqualsLiteral("moz-extension")) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-24 23:15:57 +03:00
|
|
|
nsCOMPtr<nsIPrincipal> blobPrincipal;
|
2019-09-27 17:05:13 +03:00
|
|
|
if (aURI && dom::BlobURLProtocolHandler::GetBlobURLPrincipal(
|
|
|
|
aURI, getter_AddRefs(blobPrincipal))) {
|
2018-07-24 23:15:57 +03:00
|
|
|
MOZ_ASSERT(blobPrincipal);
|
|
|
|
mFirstPartyDomain = blobPrincipal->OriginAttributesRef().mFirstPartyDomain;
|
|
|
|
return;
|
2017-04-12 09:47:48 +03:00
|
|
|
}
|
2019-04-12 18:44:40 +03:00
|
|
|
|
|
|
|
if (isInsufficientDomainLevels) {
|
|
|
|
nsAutoCString publicSuffix;
|
|
|
|
rv = tldService->GetPublicSuffix(aURI, publicSuffix);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
mFirstPartyDomain = NS_ConvertUTF8toUTF16(publicSuffix);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2017-03-22 20:45:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void OriginAttributes::SetFirstPartyDomain(const bool aIsTopLevelDocument,
|
|
|
|
const nsACString& aDomain) {
|
2019-10-24 11:51:06 +03:00
|
|
|
SetFirstPartyDomain(aIsTopLevelDocument, NS_ConvertUTF8toUTF16(aDomain));
|
|
|
|
}
|
|
|
|
|
|
|
|
void OriginAttributes::SetFirstPartyDomain(const bool aIsTopLevelDocument,
|
|
|
|
const nsAString& aDomain) {
|
2017-03-22 20:45:40 +03:00
|
|
|
bool isFirstPartyEnabled = IsFirstPartyEnabled();
|
|
|
|
|
|
|
|
// If the pref is off or this is not a top level load, bail out.
|
|
|
|
if (!isFirstPartyEnabled || !aIsTopLevelDocument) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-10-24 11:51:06 +03:00
|
|
|
mFirstPartyDomain = aDomain;
|
2017-03-22 20:45:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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 (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()) {
|
2018-07-03 16:47:45 +03:00
|
|
|
nsAutoString sanitizedFirstPartyDomain(mFirstPartyDomain);
|
2019-11-14 15:59:16 +03:00
|
|
|
sanitizedFirstPartyDomain.ReplaceChar(
|
|
|
|
dom::quota::QuotaManager::kReplaceChars, '+');
|
2018-07-03 16:47:45 +03:00
|
|
|
|
|
|
|
params.Set(NS_LITERAL_STRING("firstPartyDomain"),
|
|
|
|
sanitizedFirstPartyDomain);
|
2017-03-22 20:45:40 +03:00
|
|
|
}
|
|
|
|
|
2019-07-21 20:18:37 +03:00
|
|
|
if (!mGeckoViewSessionContextId.IsEmpty()) {
|
|
|
|
nsAutoString sanitizedGeckoViewUserContextId(mGeckoViewSessionContextId);
|
|
|
|
sanitizedGeckoViewUserContextId.ReplaceChar(
|
2019-11-14 15:59:16 +03:00
|
|
|
dom::quota::QuotaManager::kReplaceChars, '+');
|
2019-07-21 20:18:37 +03:00
|
|
|
|
|
|
|
params.Set(NS_LITERAL_STRING("geckoViewUserContextId"),
|
|
|
|
sanitizedGeckoViewUserContextId);
|
|
|
|
}
|
|
|
|
|
2017-03-22 20:45:40 +03:00
|
|
|
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);
|
2019-10-24 17:18:54 +03:00
|
|
|
// If a non-default mPrivateBrowsingId is passed and is not present in the
|
|
|
|
// suffix, then it will retain the id when it should be default according
|
|
|
|
// to the suffix. Set to default before iterating to fix this.
|
|
|
|
mOriginAttributes->mPrivateBrowsingId =
|
|
|
|
nsIScriptSecurityManager::DEFAULT_PRIVATE_BROWSING_ID;
|
2017-03-22 20:45:40 +03:00
|
|
|
}
|
|
|
|
|
2018-01-18 15:13:51 +03:00
|
|
|
bool URLParamsIterator(const nsAString& aName,
|
|
|
|
const nsAString& aValue) override {
|
2017-03-22 20:45:40 +03:00
|
|
|
if (aName.EqualsLiteral("inBrowser")) {
|
|
|
|
if (!aValue.EqualsLiteral("1")) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
mOriginAttributes->mInIsolatedMozBrowser = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-05-03 20:13:17 +03:00
|
|
|
if (aName.EqualsLiteral("addonId") || aName.EqualsLiteral("appId")) {
|
2017-03-22 20:45:40 +03:00
|
|
|
// 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());
|
2019-11-14 15:59:16 +03:00
|
|
|
mOriginAttributes->mFirstPartyDomain.Assign(aValue);
|
2017-03-22 20:45:40 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-07-21 20:18:37 +03:00
|
|
|
if (aName.EqualsLiteral("geckoViewUserContextId")) {
|
|
|
|
MOZ_RELEASE_ASSERT(
|
|
|
|
mOriginAttributes->mGeckoViewSessionContextId.IsEmpty());
|
|
|
|
mOriginAttributes->mGeckoViewSessionContextId.Assign(aValue);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-03-22 20:45:40 +03:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
PopulateFromSuffixIterator iterator(this);
|
2018-01-18 14:53:35 +03:00
|
|
|
return URLParams::Parse(Substring(aStr, 1, aStr.Length() - 1), iterator);
|
2017-03-22 20:45:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|