Source code

Revision control

Other Tools

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/. */
6
7
#ifndef mozilla_OriginAttributes_h
8
#define mozilla_OriginAttributes_h
9
10
#include "mozilla/dom/ChromeUtils.h"
11
#include "mozilla/dom/ChromeUtilsBinding.h"
12
#include "mozilla/StaticPrefs_privacy.h"
13
#include "nsIScriptSecurityManager.h"
14
15
namespace mozilla {
16
17
class OriginAttributes : public dom::OriginAttributesDictionary {
18
public:
19
OriginAttributes() = default;
20
21
explicit OriginAttributes(bool aInIsolatedMozBrowser) {
22
mInIsolatedMozBrowser = aInIsolatedMozBrowser;
23
}
24
25
explicit OriginAttributes(const OriginAttributesDictionary& aOther)
26
: OriginAttributesDictionary(aOther) {}
27
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);
34
35
enum {
36
STRIP_FIRST_PARTY_DOMAIN = 0x01,
37
STRIP_USER_CONTEXT_ID = 0x02,
38
STRIP_PRIVATE_BROWSING_ID = 0x04,
39
};
40
41
inline void StripAttributes(uint32_t aFlags) {
42
if (aFlags & STRIP_FIRST_PARTY_DOMAIN) {
43
mFirstPartyDomain.Truncate();
44
}
45
46
if (aFlags & STRIP_USER_CONTEXT_ID) {
47
mUserContextId = nsIScriptSecurityManager::DEFAULT_USER_CONTEXT_ID;
48
}
49
50
if (aFlags & STRIP_PRIVATE_BROWSING_ID) {
51
mPrivateBrowsingId =
52
nsIScriptSecurityManager::DEFAULT_PRIVATE_BROWSING_ID;
53
}
54
}
55
56
bool operator==(const OriginAttributes& aOther) const {
57
return mInIsolatedMozBrowser == aOther.mInIsolatedMozBrowser &&
58
mUserContextId == aOther.mUserContextId &&
59
mPrivateBrowsingId == aOther.mPrivateBrowsingId &&
60
mFirstPartyDomain == aOther.mFirstPartyDomain &&
61
mGeckoViewSessionContextId == aOther.mGeckoViewSessionContextId;
62
}
63
64
bool operator!=(const OriginAttributes& aOther) const {
65
return !(*this == aOther);
66
}
67
68
MOZ_MUST_USE bool EqualsIgnoringFPD(const OriginAttributes& aOther) const {
69
return mInIsolatedMozBrowser == aOther.mInIsolatedMozBrowser &&
70
mUserContextId == aOther.mUserContextId &&
71
mPrivateBrowsingId == aOther.mPrivateBrowsingId &&
72
mGeckoViewSessionContextId == aOther.mGeckoViewSessionContextId;
73
}
74
75
// Serializes/Deserializes non-default values into the suffix format, i.e.
76
// |!key1=value1&key2=value2|. If there are no non-default attributes, this
77
// returns an empty string.
78
void CreateSuffix(nsACString& aStr) const;
79
80
// Don't use this method for anything else than debugging!
81
void CreateAnonymizedSuffix(nsACString& aStr) const;
82
83
MOZ_MUST_USE bool PopulateFromSuffix(const nsACString& aStr);
84
85
// Populates the attributes from a string like
86
// |uri!key1=value1&key2=value2| and returns the uri without the suffix.
87
MOZ_MUST_USE bool PopulateFromOrigin(const nsACString& aOrigin,
88
nsACString& aOriginNoSuffix);
89
90
// Helper function to match mIsPrivateBrowsing to existing private browsing
91
// flags. Once all other flags are removed, this can be removed too.
92
void SyncAttributesWithPrivateBrowsing(bool aInPrivateBrowsing);
93
94
// check if "privacy.firstparty.isolate" is enabled.
95
static inline bool IsFirstPartyEnabled() {
96
return StaticPrefs::privacy_firstparty_isolate();
97
}
98
99
// check if the access of window.opener across different FPDs is restricted.
100
// We only restrict the access of window.opener when first party isolation
101
// is enabled and "privacy.firstparty.isolate.restrict_opener_access" is on.
102
static inline bool IsRestrictOpenerAccessForFPI() {
103
// We always want to restrict window.opener if first party isolation is
104
// disabled.
105
return !StaticPrefs::privacy_firstparty_isolate() ||
106
StaticPrefs::privacy_firstparty_isolate_restrict_opener_access();
107
}
108
109
// Check whether we block the postMessage across different FPDs when the
110
// targetOrigin is '*'.
111
static inline MOZ_MUST_USE bool IsBlockPostMessageForFPI() {
112
return StaticPrefs::privacy_firstparty_isolate() &&
113
StaticPrefs::privacy_firstparty_isolate_block_post_message();
114
}
115
116
// returns true if the originAttributes suffix has mPrivateBrowsingId value
117
// different than 0.
118
static bool IsPrivateBrowsing(const nsACString& aOrigin);
119
};
120
121
class OriginAttributesPattern : public dom::OriginAttributesPatternDictionary {
122
public:
123
// To convert a JSON string to an OriginAttributesPattern, do the following:
124
//
125
// OriginAttributesPattern pattern;
126
// if (!pattern.Init(aJSONString)) {
127
// ... // handle failure.
128
// }
129
OriginAttributesPattern() = default;
130
131
explicit OriginAttributesPattern(
132
const OriginAttributesPatternDictionary& aOther)
133
: OriginAttributesPatternDictionary(aOther) {}
134
135
// Performs a match of |aAttrs| against this pattern.
136
bool Matches(const OriginAttributes& aAttrs) const {
137
if (mInIsolatedMozBrowser.WasPassed() &&
138
mInIsolatedMozBrowser.Value() != aAttrs.mInIsolatedMozBrowser) {
139
return false;
140
}
141
142
if (mUserContextId.WasPassed() &&
143
mUserContextId.Value() != aAttrs.mUserContextId) {
144
return false;
145
}
146
147
if (mPrivateBrowsingId.WasPassed() &&
148
mPrivateBrowsingId.Value() != aAttrs.mPrivateBrowsingId) {
149
return false;
150
}
151
152
if (mFirstPartyDomain.WasPassed() &&
153
mFirstPartyDomain.Value() != aAttrs.mFirstPartyDomain) {
154
return false;
155
}
156
157
if (mGeckoViewSessionContextId.WasPassed() &&
158
mGeckoViewSessionContextId.Value() !=
159
aAttrs.mGeckoViewSessionContextId) {
160
return false;
161
}
162
163
return true;
164
}
165
166
bool Overlaps(const OriginAttributesPattern& aOther) const {
167
if (mInIsolatedMozBrowser.WasPassed() &&
168
aOther.mInIsolatedMozBrowser.WasPassed() &&
169
mInIsolatedMozBrowser.Value() != aOther.mInIsolatedMozBrowser.Value()) {
170
return false;
171
}
172
173
if (mUserContextId.WasPassed() && aOther.mUserContextId.WasPassed() &&
174
mUserContextId.Value() != aOther.mUserContextId.Value()) {
175
return false;
176
}
177
178
if (mPrivateBrowsingId.WasPassed() &&
179
aOther.mPrivateBrowsingId.WasPassed() &&
180
mPrivateBrowsingId.Value() != aOther.mPrivateBrowsingId.Value()) {
181
return false;
182
}
183
184
if (mFirstPartyDomain.WasPassed() && aOther.mFirstPartyDomain.WasPassed() &&
185
mFirstPartyDomain.Value() != aOther.mFirstPartyDomain.Value()) {
186
return false;
187
}
188
189
if (mGeckoViewSessionContextId.WasPassed() &&
190
aOther.mGeckoViewSessionContextId.WasPassed() &&
191
mGeckoViewSessionContextId.Value() !=
192
aOther.mGeckoViewSessionContextId.Value()) {
193
return false;
194
}
195
196
return true;
197
}
198
};
199
200
} // namespace mozilla
201
202
#endif /* mozilla_OriginAttributes_h */