OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "platform/feature_policy/FeaturePolicy.h" | 5 #include "platform/feature_policy/FeaturePolicy.h" |
6 | 6 |
7 #include "platform/json/JSONValues.h" | 7 #include "platform/json/JSONValues.h" |
8 #include "platform/network/HTTPParsers.h" | 8 #include "platform/network/HTTPParsers.h" |
9 #include "platform/weborigin/KURL.h" | 9 #include "platform/weborigin/KURL.h" |
10 #include "platform/weborigin/SecurityOrigin.h" | 10 #include "platform/weborigin/SecurityOrigin.h" |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 "sync-xhr", FeaturePolicy::FeatureDefault::EnableForAll}; | 54 "sync-xhr", FeaturePolicy::FeatureDefault::EnableForAll}; |
55 const FeaturePolicy::Feature kUsermedia{ | 55 const FeaturePolicy::Feature kUsermedia{ |
56 "usermedia", FeaturePolicy::FeatureDefault::EnableForAll}; | 56 "usermedia", FeaturePolicy::FeatureDefault::EnableForAll}; |
57 const FeaturePolicy::Feature kVibrateFeature{ | 57 const FeaturePolicy::Feature kVibrateFeature{ |
58 "vibrate", FeaturePolicy::FeatureDefault::EnableForSelf}; | 58 "vibrate", FeaturePolicy::FeatureDefault::EnableForSelf}; |
59 const FeaturePolicy::Feature kWebRTC{ | 59 const FeaturePolicy::Feature kWebRTC{ |
60 "webrtc", FeaturePolicy::FeatureDefault::EnableForAll}; | 60 "webrtc", FeaturePolicy::FeatureDefault::EnableForAll}; |
61 | 61 |
62 // static | 62 // static |
63 std::unique_ptr<FeaturePolicy::Whitelist> FeaturePolicy::Whitelist::from( | 63 std::unique_ptr<FeaturePolicy::Whitelist> FeaturePolicy::Whitelist::from( |
64 const WebFeaturePolicy::ParsedWhitelist& parsedWhitelist) { | 64 const WebParsedFeaturePolicyDeclaration& parsedDeclaration) { |
65 std::unique_ptr<Whitelist> whitelist(new FeaturePolicy::Whitelist); | 65 std::unique_ptr<Whitelist> whitelist(new FeaturePolicy::Whitelist); |
66 if (parsedWhitelist.matchesAllOrigins) { | 66 if (parsedDeclaration.matchesAllOrigins) { |
67 whitelist->addAll(); | 67 whitelist->addAll(); |
68 } else { | 68 } else { |
69 for (const WebSecurityOrigin& origin : parsedWhitelist.origins) | 69 for (const WebSecurityOrigin& origin : parsedDeclaration.origins) |
70 whitelist->add(static_cast<WTF::PassRefPtr<SecurityOrigin>>(origin)); | 70 whitelist->add(static_cast<WTF::PassRefPtr<SecurityOrigin>>(origin)); |
71 } | 71 } |
72 return whitelist; | 72 return whitelist; |
73 } | 73 } |
74 | 74 |
75 FeaturePolicy::Whitelist::Whitelist() : m_matchesAllOrigins(false) {} | 75 FeaturePolicy::Whitelist::Whitelist() : m_matchesAllOrigins(false) {} |
76 | 76 |
77 void FeaturePolicy::Whitelist::addAll() { | 77 void FeaturePolicy::Whitelist::addAll() { |
78 m_matchesAllOrigins = true; | 78 m_matchesAllOrigins = true; |
79 } | 79 } |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
141 | 141 |
142 // static | 142 // static |
143 std::unique_ptr<FeaturePolicy> FeaturePolicy::createFromParentPolicy( | 143 std::unique_ptr<FeaturePolicy> FeaturePolicy::createFromParentPolicy( |
144 const FeaturePolicy* parent, | 144 const FeaturePolicy* parent, |
145 RefPtr<SecurityOrigin> currentOrigin) { | 145 RefPtr<SecurityOrigin> currentOrigin) { |
146 return createFromParentPolicy(parent, std::move(currentOrigin), | 146 return createFromParentPolicy(parent, std::move(currentOrigin), |
147 getDefaultFeatureList()); | 147 getDefaultFeatureList()); |
148 } | 148 } |
149 | 149 |
150 // static | 150 // static |
151 WebParsedFeaturePolicy FeaturePolicy::parseFeaturePolicy( | 151 WebParsedFeaturePolicyHeader FeaturePolicy::parseFeaturePolicy( |
152 const String& policy, | 152 const String& policy, |
153 RefPtr<SecurityOrigin> origin, | 153 RefPtr<SecurityOrigin> origin, |
154 Vector<String>* messages) { | 154 Vector<String>* messages) { |
155 Vector<WebFeaturePolicy::ParsedWhitelist> whitelists; | 155 Vector<WebParsedFeaturePolicyDeclaration> whitelists; |
156 | 156 |
157 // Use a reasonable parse depth limit; the actual maximum depth is only going | 157 // Use a reasonable parse depth limit; the actual maximum depth is only going |
158 // to be 4 for a valid policy, but we'll give the featurePolicyParser a chance | 158 // to be 4 for a valid policy, but we'll give the featurePolicyParser a chance |
159 // to report more specific errors, unless the string is really invalid. | 159 // to report more specific errors, unless the string is really invalid. |
160 std::unique_ptr<JSONArray> policyItems = parseJSONHeader(policy, 50); | 160 std::unique_ptr<JSONArray> policyItems = parseJSONHeader(policy, 50); |
161 if (!policyItems) { | 161 if (!policyItems) { |
162 if (messages) | 162 if (messages) |
163 messages->push_back("Unable to parse header"); | 163 messages->push_back("Unable to parse header"); |
164 return whitelists; | 164 return whitelists; |
165 } | 165 } |
166 | 166 |
167 for (size_t i = 0; i < policyItems->size(); ++i) { | 167 for (size_t i = 0; i < policyItems->size(); ++i) { |
168 JSONObject* item = JSONObject::cast(policyItems->at(i)); | 168 JSONObject* item = JSONObject::cast(policyItems->at(i)); |
169 if (!item) { | 169 if (!item) { |
170 if (messages) | 170 if (messages) |
171 messages->push_back("Policy is not an object"); | 171 messages->push_back("Policy is not an object"); |
172 continue; // Array element is not an object; skip | 172 continue; // Array element is not an object; skip |
173 } | 173 } |
174 | 174 |
175 for (size_t j = 0; j < item->size(); ++j) { | 175 for (size_t j = 0; j < item->size(); ++j) { |
176 JSONObject::Entry entry = item->at(j); | 176 JSONObject::Entry entry = item->at(j); |
177 String featureName = entry.first; | 177 String featureName = entry.first; |
178 JSONArray* targets = JSONArray::cast(entry.second); | 178 JSONArray* targets = JSONArray::cast(entry.second); |
179 if (!targets) { | 179 if (!targets) { |
180 if (messages) | 180 if (messages) |
181 messages->push_back("Whitelist is not an array of strings."); | 181 messages->push_back("Whitelist is not an array of strings."); |
182 continue; | 182 continue; |
183 } | 183 } |
184 | 184 |
185 WebFeaturePolicy::ParsedWhitelist whitelist; | 185 WebParsedFeaturePolicyDeclaration whitelist; |
186 whitelist.featureName = featureName; | 186 whitelist.featureName = featureName; |
187 Vector<WebSecurityOrigin> origins; | 187 Vector<WebSecurityOrigin> origins; |
188 String targetString; | 188 String targetString; |
189 for (size_t j = 0; j < targets->size(); ++j) { | 189 for (size_t j = 0; j < targets->size(); ++j) { |
190 if (targets->at(j)->asString(&targetString)) { | 190 if (targets->at(j)->asString(&targetString)) { |
191 if (equalIgnoringCase(targetString, "self")) { | 191 if (equalIgnoringCase(targetString, "self")) { |
192 if (!origin->isUnique()) | 192 if (!origin->isUnique()) |
193 origins.push_back(origin); | 193 origins.push_back(origin); |
194 } else if (targetString == "*") { | 194 } else if (targetString == "*") { |
195 whitelist.matchesAllOrigins = true; | 195 whitelist.matchesAllOrigins = true; |
196 } else { | 196 } else { |
197 WebSecurityOrigin targetOrigin = | 197 WebSecurityOrigin targetOrigin = |
198 WebSecurityOrigin::createFromString(targetString); | 198 WebSecurityOrigin::createFromString(targetString); |
199 if (!targetOrigin.isNull() && !targetOrigin.isUnique()) | 199 if (!targetOrigin.isNull() && !targetOrigin.isUnique()) |
200 origins.push_back(targetOrigin); | 200 origins.push_back(targetOrigin); |
201 } | 201 } |
202 } else { | 202 } else { |
203 if (messages) | 203 if (messages) |
204 messages->push_back("Whitelist is not an array of strings."); | 204 messages->push_back("Whitelist is not an array of strings."); |
205 } | 205 } |
206 } | 206 } |
207 whitelist.origins = origins; | 207 whitelist.origins = origins; |
208 whitelists.push_back(whitelist); | 208 whitelists.push_back(whitelist); |
209 } | 209 } |
210 } | 210 } |
211 return whitelists; | 211 return whitelists; |
212 } | 212 } |
213 | 213 |
214 void FeaturePolicy::setHeaderPolicy(const WebParsedFeaturePolicy& policy) { | 214 void FeaturePolicy::setHeaderPolicy( |
| 215 const WebParsedFeaturePolicyHeader& policy) { |
215 DCHECK(m_headerWhitelists.isEmpty()); | 216 DCHECK(m_headerWhitelists.isEmpty()); |
216 for (const WebFeaturePolicy::ParsedWhitelist& parsedWhitelist : policy) { | 217 for (const WebParsedFeaturePolicyDeclaration& parsedDeclaration : policy) { |
217 const FeaturePolicy::Feature* feature = | 218 const FeaturePolicy::Feature* feature = |
218 featureForName(parsedWhitelist.featureName, m_features); | 219 featureForName(parsedDeclaration.featureName, m_features); |
219 if (!feature) | 220 if (!feature) |
220 continue; | 221 continue; |
221 m_headerWhitelists.set(feature, Whitelist::from(parsedWhitelist)); | 222 m_headerWhitelists.set(feature, Whitelist::from(parsedDeclaration)); |
222 } | 223 } |
223 } | 224 } |
224 | 225 |
225 bool FeaturePolicy::isFeatureEnabledForOrigin( | 226 bool FeaturePolicy::isFeatureEnabledForOrigin( |
226 const FeaturePolicy::Feature& feature, | 227 const FeaturePolicy::Feature& feature, |
227 const SecurityOrigin& origin) const { | 228 const SecurityOrigin& origin) const { |
228 DCHECK(m_inheritedFeatures.contains(&feature)); | 229 DCHECK(m_inheritedFeatures.contains(&feature)); |
229 if (!m_inheritedFeatures.get(&feature)) { | 230 if (!m_inheritedFeatures.get(&feature)) { |
230 return false; | 231 return false; |
231 } | 232 } |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
269 sb.append(" "); | 270 sb.append(" "); |
270 sb.append(whitelist.key->featureName); | 271 sb.append(whitelist.key->featureName); |
271 sb.append(": "); | 272 sb.append(": "); |
272 sb.append(whitelist.value->toString()); | 273 sb.append(whitelist.value->toString()); |
273 sb.append("\n"); | 274 sb.append("\n"); |
274 } | 275 } |
275 return sb.toString(); | 276 return sb.toString(); |
276 } | 277 } |
277 | 278 |
278 } // namespace blink | 279 } // namespace blink |
OLD | NEW |