2017-03-22 20:45:40 +03:00
|
|
|
/* -*- 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"
|
2019-08-01 09:00:22 +03:00
|
|
|
#include "mozilla/StaticPrefs_privacy.h"
|
2017-03-22 20:45:40 +03:00
|
|
|
#include "nsIScriptSecurityManager.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
class OriginAttributes : public dom::OriginAttributesDictionary {
|
|
|
|
public:
|
|
|
|
OriginAttributes() {}
|
|
|
|
|
2019-05-03 20:13:17 +03:00
|
|
|
explicit OriginAttributes(bool aInIsolatedMozBrowser) {
|
2017-03-22 20:45:40 +03:00
|
|
|
mInIsolatedMozBrowser = aInIsolatedMozBrowser;
|
|
|
|
}
|
|
|
|
|
|
|
|
explicit OriginAttributes(const OriginAttributesDictionary& aOther)
|
|
|
|
: OriginAttributesDictionary(aOther) {}
|
|
|
|
|
2019-04-12 08:31:32 +03:00
|
|
|
void SetFirstPartyDomain(const bool aIsTopLevelDocument, nsIURI* aURI,
|
|
|
|
bool aForced = false);
|
2017-03-22 20:45:40 +03:00
|
|
|
void SetFirstPartyDomain(const bool aIsTopLevelDocument,
|
|
|
|
const nsACString& aDomain);
|
2019-10-24 11:51:06 +03:00
|
|
|
void SetFirstPartyDomain(const bool aIsTopLevelDocument,
|
|
|
|
const nsAString& aDomain);
|
2017-03-22 20:45:40 +03:00
|
|
|
|
|
|
|
enum {
|
|
|
|
STRIP_FIRST_PARTY_DOMAIN = 0x01,
|
|
|
|
STRIP_USER_CONTEXT_ID = 0x02,
|
2019-10-24 17:18:54 +03:00
|
|
|
STRIP_PRIVATE_BROWSING_ID = 0x04,
|
2017-03-22 20:45:40 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2019-10-24 17:18:54 +03:00
|
|
|
|
|
|
|
if (aFlags & STRIP_PRIVATE_BROWSING_ID) {
|
|
|
|
mPrivateBrowsingId =
|
|
|
|
nsIScriptSecurityManager::DEFAULT_PRIVATE_BROWSING_ID;
|
|
|
|
}
|
2017-03-22 20:45:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==(const OriginAttributes& aOther) const {
|
2019-05-03 20:13:17 +03:00
|
|
|
return mInIsolatedMozBrowser == aOther.mInIsolatedMozBrowser &&
|
2017-03-22 20:45:40 +03:00
|
|
|
mUserContextId == aOther.mUserContextId &&
|
|
|
|
mPrivateBrowsingId == aOther.mPrivateBrowsingId &&
|
2019-07-21 20:18:37 +03:00
|
|
|
mFirstPartyDomain == aOther.mFirstPartyDomain &&
|
|
|
|
mGeckoViewSessionContextId == aOther.mGeckoViewSessionContextId;
|
2017-03-22 20:45:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(const OriginAttributes& aOther) const {
|
|
|
|
return !(*this == aOther);
|
|
|
|
}
|
|
|
|
|
2018-10-25 09:47:08 +03:00
|
|
|
MOZ_MUST_USE bool EqualsIgnoringFPD(const OriginAttributes& aOther) const {
|
2019-05-03 20:13:17 +03:00
|
|
|
return mInIsolatedMozBrowser == aOther.mInIsolatedMozBrowser &&
|
2018-10-25 09:47:08 +03:00
|
|
|
mUserContextId == aOther.mUserContextId &&
|
2019-07-21 20:18:37 +03:00
|
|
|
mPrivateBrowsingId == aOther.mPrivateBrowsingId &&
|
|
|
|
mGeckoViewSessionContextId == aOther.mGeckoViewSessionContextId;
|
2018-10-25 09:47:08 +03:00
|
|
|
}
|
|
|
|
|
2017-03-22 20:45:40 +03:00
|
|
|
// 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.
|
2019-08-01 09:00:22 +03:00
|
|
|
static inline bool IsFirstPartyEnabled() {
|
|
|
|
return StaticPrefs::privacy_firstparty_isolate();
|
|
|
|
}
|
2017-03-22 20:45:40 +03:00
|
|
|
|
|
|
|
// 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.
|
2019-08-01 09:00:22 +03:00
|
|
|
return !StaticPrefs::privacy_firstparty_isolate() ||
|
|
|
|
StaticPrefs::privacy_firstparty_isolate_restrict_opener_access();
|
2017-03-22 20:45:40 +03:00
|
|
|
}
|
|
|
|
|
2018-10-25 09:47:08 +03:00
|
|
|
// Check whether we block the postMessage across different FPDs when the
|
|
|
|
// targetOrigin is '*'.
|
|
|
|
static inline MOZ_MUST_USE bool IsBlockPostMessageForFPI() {
|
2019-08-01 09:00:22 +03:00
|
|
|
return StaticPrefs::privacy_firstparty_isolate() &&
|
|
|
|
StaticPrefs::privacy_firstparty_isolate_block_post_message();
|
2018-10-25 09:47:08 +03:00
|
|
|
}
|
|
|
|
|
2017-03-22 20:45:40 +03:00
|
|
|
// returns true if the originAttributes suffix has mPrivateBrowsingId value
|
|
|
|
// different than 0.
|
|
|
|
static bool IsPrivateBrowsing(const nsACString& aOrigin);
|
|
|
|
};
|
|
|
|
|
|
|
|
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 (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;
|
|
|
|
}
|
|
|
|
|
2019-07-21 20:18:37 +03:00
|
|
|
if (mGeckoViewSessionContextId.WasPassed() &&
|
|
|
|
mGeckoViewSessionContextId.Value() !=
|
2019-08-13 10:15:25 +03:00
|
|
|
aAttrs.mGeckoViewSessionContextId) {
|
2019-07-21 20:18:37 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-22 20:45:40 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Overlaps(const OriginAttributesPattern& aOther) const {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-07-21 20:18:37 +03:00
|
|
|
if (mGeckoViewSessionContextId.WasPassed() &&
|
|
|
|
aOther.mGeckoViewSessionContextId.WasPassed() &&
|
|
|
|
mGeckoViewSessionContextId.Value() !=
|
2019-08-13 10:15:25 +03:00
|
|
|
aOther.mGeckoViewSessionContextId.Value()) {
|
2019-07-21 20:18:37 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-22 20:45:40 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif /* mozilla_OriginAttributes_h */
|