1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_OriginAttributes_h
8 #define mozilla_OriginAttributes_h
10 #include "mozilla/dom/ChromeUtils.h"
11 #include "mozilla/dom/ChromeUtilsBinding.h"
12 #include "mozilla/StaticPrefs_privacy.h"
13 #include "nsIScriptSecurityManager.h"
17 class OriginAttributes
: public dom::OriginAttributesDictionary
{
19 OriginAttributes() = default;
21 explicit OriginAttributes(bool aInIsolatedMozBrowser
) {
22 mInIsolatedMozBrowser
= aInIsolatedMozBrowser
;
25 explicit OriginAttributes(const OriginAttributesDictionary
& aOther
)
26 : OriginAttributesDictionary(aOther
) {}
28 void SetFirstPartyDomain(const bool aIsTopLevelDocument
, nsIURI
* aURI
,
29 bool aForced
= false);
30 void SetFirstPartyDomain(const bool aIsTopLevelDocument
,
31 const nsACString
& aDomain
);
32 void SetFirstPartyDomain(const bool aIsTopLevelDocument
,
33 const nsAString
& aDomain
, bool aForced
= false);
35 void SetPartitionKey(nsIURI
* aURI
);
36 void SetPartitionKey(const nsACString
& aDomain
);
37 void SetPartitionKey(const nsAString
& aDomain
);
40 STRIP_FIRST_PARTY_DOMAIN
= 0x01,
41 STRIP_USER_CONTEXT_ID
= 0x02,
42 STRIP_PRIVATE_BROWSING_ID
= 0x04,
43 STRIP_PARITION_KEY
= 0x08,
46 inline void StripAttributes(uint32_t aFlags
) {
47 if (aFlags
& STRIP_FIRST_PARTY_DOMAIN
) {
48 mFirstPartyDomain
.Truncate();
51 if (aFlags
& STRIP_USER_CONTEXT_ID
) {
52 mUserContextId
= nsIScriptSecurityManager::DEFAULT_USER_CONTEXT_ID
;
55 if (aFlags
& STRIP_PRIVATE_BROWSING_ID
) {
57 nsIScriptSecurityManager::DEFAULT_PRIVATE_BROWSING_ID
;
60 if (aFlags
& STRIP_PARITION_KEY
) {
61 mPartitionKey
.Truncate();
65 bool operator==(const OriginAttributes
& aOther
) const {
66 return mInIsolatedMozBrowser
== aOther
.mInIsolatedMozBrowser
&&
67 mUserContextId
== aOther
.mUserContextId
&&
68 mPrivateBrowsingId
== aOther
.mPrivateBrowsingId
&&
69 mFirstPartyDomain
== aOther
.mFirstPartyDomain
&&
70 mGeckoViewSessionContextId
== aOther
.mGeckoViewSessionContextId
&&
71 mPartitionKey
== aOther
.mPartitionKey
;
74 bool operator!=(const OriginAttributes
& aOther
) const {
75 return !(*this == aOther
);
78 MOZ_MUST_USE
bool EqualsIgnoringFPD(const OriginAttributes
& aOther
) const {
79 return mInIsolatedMozBrowser
== aOther
.mInIsolatedMozBrowser
&&
80 mUserContextId
== aOther
.mUserContextId
&&
81 mPrivateBrowsingId
== aOther
.mPrivateBrowsingId
&&
82 mGeckoViewSessionContextId
== aOther
.mGeckoViewSessionContextId
;
85 // Serializes/Deserializes non-default values into the suffix format, i.e.
86 // |!key1=value1&key2=value2|. If there are no non-default attributes, this
87 // returns an empty string.
88 void CreateSuffix(nsACString
& aStr
) const;
90 // Don't use this method for anything else than debugging!
91 void CreateAnonymizedSuffix(nsACString
& aStr
) const;
93 MOZ_MUST_USE
bool PopulateFromSuffix(const nsACString
& aStr
);
95 // Populates the attributes from a string like
96 // |uri!key1=value1&key2=value2| and returns the uri without the suffix.
97 MOZ_MUST_USE
bool PopulateFromOrigin(const nsACString
& aOrigin
,
98 nsACString
& aOriginNoSuffix
);
100 // Helper function to match mIsPrivateBrowsing to existing private browsing
101 // flags. Once all other flags are removed, this can be removed too.
102 void SyncAttributesWithPrivateBrowsing(bool aInPrivateBrowsing
);
104 // check if "privacy.firstparty.isolate" is enabled.
105 static inline bool IsFirstPartyEnabled() {
106 return StaticPrefs::privacy_firstparty_isolate();
109 static inline bool UseSiteForFirstPartyDomain() {
110 if (IsFirstPartyEnabled()) {
111 return StaticPrefs::privacy_firstparty_isolate_use_site();
113 return StaticPrefs::privacy_dynamic_firstparty_use_site();
116 // check if the access of window.opener across different FPDs is restricted.
117 // We only restrict the access of window.opener when first party isolation
118 // is enabled and "privacy.firstparty.isolate.restrict_opener_access" is on.
119 static inline bool IsRestrictOpenerAccessForFPI() {
120 // We always want to restrict window.opener if first party isolation is
122 return !StaticPrefs::privacy_firstparty_isolate() ||
123 StaticPrefs::privacy_firstparty_isolate_restrict_opener_access();
126 // Check whether we block the postMessage across different FPDs when the
127 // targetOrigin is '*'.
128 static inline MOZ_MUST_USE
bool IsBlockPostMessageForFPI() {
129 return StaticPrefs::privacy_firstparty_isolate() &&
130 StaticPrefs::privacy_firstparty_isolate_block_post_message();
133 // returns true if the originAttributes suffix has mPrivateBrowsingId value
135 static bool IsPrivateBrowsing(const nsACString
& aOrigin
);
138 class OriginAttributesPattern
: public dom::OriginAttributesPatternDictionary
{
140 // To convert a JSON string to an OriginAttributesPattern, do the following:
142 // OriginAttributesPattern pattern;
143 // if (!pattern.Init(aJSONString)) {
144 // ... // handle failure.
146 OriginAttributesPattern() = default;
148 explicit OriginAttributesPattern(
149 const OriginAttributesPatternDictionary
& aOther
)
150 : OriginAttributesPatternDictionary(aOther
) {}
152 // Performs a match of |aAttrs| against this pattern.
153 bool Matches(const OriginAttributes
& aAttrs
) const {
154 if (mInIsolatedMozBrowser
.WasPassed() &&
155 mInIsolatedMozBrowser
.Value() != aAttrs
.mInIsolatedMozBrowser
) {
159 if (mUserContextId
.WasPassed() &&
160 mUserContextId
.Value() != aAttrs
.mUserContextId
) {
164 if (mPrivateBrowsingId
.WasPassed() &&
165 mPrivateBrowsingId
.Value() != aAttrs
.mPrivateBrowsingId
) {
169 if (mFirstPartyDomain
.WasPassed() &&
170 mFirstPartyDomain
.Value() != aAttrs
.mFirstPartyDomain
) {
174 if (mGeckoViewSessionContextId
.WasPassed() &&
175 mGeckoViewSessionContextId
.Value() !=
176 aAttrs
.mGeckoViewSessionContextId
) {
180 if (mPartitionKey
.WasPassed() &&
181 mPartitionKey
.Value() != aAttrs
.mPartitionKey
) {
188 bool Overlaps(const OriginAttributesPattern
& aOther
) const {
189 if (mInIsolatedMozBrowser
.WasPassed() &&
190 aOther
.mInIsolatedMozBrowser
.WasPassed() &&
191 mInIsolatedMozBrowser
.Value() != aOther
.mInIsolatedMozBrowser
.Value()) {
195 if (mUserContextId
.WasPassed() && aOther
.mUserContextId
.WasPassed() &&
196 mUserContextId
.Value() != aOther
.mUserContextId
.Value()) {
200 if (mPrivateBrowsingId
.WasPassed() &&
201 aOther
.mPrivateBrowsingId
.WasPassed() &&
202 mPrivateBrowsingId
.Value() != aOther
.mPrivateBrowsingId
.Value()) {
206 if (mFirstPartyDomain
.WasPassed() && aOther
.mFirstPartyDomain
.WasPassed() &&
207 mFirstPartyDomain
.Value() != aOther
.mFirstPartyDomain
.Value()) {
211 if (mGeckoViewSessionContextId
.WasPassed() &&
212 aOther
.mGeckoViewSessionContextId
.WasPassed() &&
213 mGeckoViewSessionContextId
.Value() !=
214 aOther
.mGeckoViewSessionContextId
.Value()) {
218 if (mPartitionKey
.WasPassed() && aOther
.mPartitionKey
.WasPassed() &&
219 mPartitionKey
.Value() != aOther
.mPartitionKey
.Value()) {
227 } // namespace mozilla
229 #endif /* mozilla_OriginAttributes_h */