/work/obj-fuzz/dist/include/mozilla/OriginAttributes.h
Line | Count | Source (jump to first uncovered line) |
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 | | class OriginAttributes : public dom::OriginAttributesDictionary |
17 | | { |
18 | | public: |
19 | 11.5k | OriginAttributes() {} |
20 | | |
21 | | OriginAttributes(uint32_t aAppId, bool aInIsolatedMozBrowser) |
22 | 0 | { |
23 | 0 | mAppId = aAppId; |
24 | 0 | mInIsolatedMozBrowser = aInIsolatedMozBrowser; |
25 | 0 | } |
26 | | |
27 | | explicit OriginAttributes(const OriginAttributesDictionary& aOther) |
28 | | : 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 | | inline void StripAttributes(uint32_t aFlags) |
40 | 0 | { |
41 | 0 | if (aFlags & STRIP_FIRST_PARTY_DOMAIN) { |
42 | 0 | mFirstPartyDomain.Truncate(); |
43 | 0 | } |
44 | 0 |
|
45 | 0 | if (aFlags & STRIP_USER_CONTEXT_ID) { |
46 | 0 | mUserContextId = nsIScriptSecurityManager::DEFAULT_USER_CONTEXT_ID; |
47 | 0 | } |
48 | 0 | } |
49 | | |
50 | | bool operator==(const OriginAttributes& aOther) const |
51 | 0 | { |
52 | 0 | return mAppId == aOther.mAppId && |
53 | 0 | mInIsolatedMozBrowser == aOther.mInIsolatedMozBrowser && |
54 | 0 | mUserContextId == aOther.mUserContextId && |
55 | 0 | mPrivateBrowsingId == aOther.mPrivateBrowsingId && |
56 | 0 | mFirstPartyDomain == aOther.mFirstPartyDomain; |
57 | 0 | } |
58 | | |
59 | | bool operator!=(const OriginAttributes& aOther) const |
60 | 0 | { |
61 | 0 | return !(*this == aOther); |
62 | 0 | } |
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 | | static inline bool IsFirstPartyEnabled() |
85 | 0 | { |
86 | 0 | return sFirstPartyIsolation; |
87 | 0 | } |
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 | | static inline bool IsRestrictOpenerAccessForFPI() |
93 | 0 | { |
94 | 0 | // We always want to restrict window.opener if first party isolation is |
95 | 0 | // disabled. |
96 | 0 | return !sFirstPartyIsolation || sRestrictedOpenerAccess; |
97 | 0 | } |
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 | | 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 | 0 | OriginAttributesPattern() {} |
120 | | |
121 | | explicit OriginAttributesPattern(const OriginAttributesPatternDictionary& aOther) |
122 | 0 | : OriginAttributesPatternDictionary(aOther) {} |
123 | | |
124 | | // Performs a match of |aAttrs| against this pattern. |
125 | | bool Matches(const OriginAttributes& aAttrs) const |
126 | 0 | { |
127 | 0 | if (mAppId.WasPassed() && mAppId.Value() != aAttrs.mAppId) { |
128 | 0 | return false; |
129 | 0 | } |
130 | 0 | |
131 | 0 | if (mInIsolatedMozBrowser.WasPassed() && mInIsolatedMozBrowser.Value() != aAttrs.mInIsolatedMozBrowser) { |
132 | 0 | return false; |
133 | 0 | } |
134 | 0 | |
135 | 0 | if (mUserContextId.WasPassed() && mUserContextId.Value() != aAttrs.mUserContextId) { |
136 | 0 | return false; |
137 | 0 | } |
138 | 0 | |
139 | 0 | if (mPrivateBrowsingId.WasPassed() && mPrivateBrowsingId.Value() != aAttrs.mPrivateBrowsingId) { |
140 | 0 | return false; |
141 | 0 | } |
142 | 0 | |
143 | 0 | if (mFirstPartyDomain.WasPassed() && mFirstPartyDomain.Value() != aAttrs.mFirstPartyDomain) { |
144 | 0 | return false; |
145 | 0 | } |
146 | 0 | |
147 | 0 | return true; |
148 | 0 | } |
149 | | |
150 | | bool Overlaps(const OriginAttributesPattern& aOther) const |
151 | 0 | { |
152 | 0 | if (mAppId.WasPassed() && aOther.mAppId.WasPassed() && |
153 | 0 | mAppId.Value() != aOther.mAppId.Value()) { |
154 | 0 | return false; |
155 | 0 | } |
156 | 0 | |
157 | 0 | if (mInIsolatedMozBrowser.WasPassed() && |
158 | 0 | aOther.mInIsolatedMozBrowser.WasPassed() && |
159 | 0 | mInIsolatedMozBrowser.Value() != aOther.mInIsolatedMozBrowser.Value()) { |
160 | 0 | return false; |
161 | 0 | } |
162 | 0 | |
163 | 0 | if (mUserContextId.WasPassed() && aOther.mUserContextId.WasPassed() && |
164 | 0 | mUserContextId.Value() != aOther.mUserContextId.Value()) { |
165 | 0 | return false; |
166 | 0 | } |
167 | 0 | |
168 | 0 | if (mPrivateBrowsingId.WasPassed() && aOther.mPrivateBrowsingId.WasPassed() && |
169 | 0 | mPrivateBrowsingId.Value() != aOther.mPrivateBrowsingId.Value()) { |
170 | 0 | return false; |
171 | 0 | } |
172 | 0 | |
173 | 0 | if (mFirstPartyDomain.WasPassed() && aOther.mFirstPartyDomain.WasPassed() && |
174 | 0 | mFirstPartyDomain.Value() != aOther.mFirstPartyDomain.Value()) { |
175 | 0 | return false; |
176 | 0 | } |
177 | 0 | |
178 | 0 | return true; |
179 | 0 | } |
180 | | }; |
181 | | |
182 | | } // namespace mozilla |
183 | | |
184 | | #endif /* mozilla_OriginAttributes_h */ |