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