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 11 matching lines...) Expand all Loading... |
22 FeaturePolicy::FeatureList& features) { | 22 FeaturePolicy::FeatureList& features) { |
23 for (const FeaturePolicy::Feature* feature : features) { | 23 for (const FeaturePolicy::Feature* feature : features) { |
24 if (featureName == feature->featureName) | 24 if (featureName == feature->featureName) |
25 return feature; | 25 return feature; |
26 } | 26 } |
27 return nullptr; | 27 return nullptr; |
28 } | 28 } |
29 | 29 |
30 // Converts a list of JSON feature policy items into a mapping of features to | 30 // Converts a list of JSON feature policy items into a mapping of features to |
31 // whitelists. For future compatibility, unrecognized features are simply | 31 // whitelists. For future compatibility, unrecognized features are simply |
32 // ignored, as are unparseable origins. Any errors in the input will cause an | 32 // ignored, as are unparseable origins. If |messages| is not null, then any |
33 // error message appended to |messages|. | 33 // errors in the input will cause an error message to be appended to it. |
34 HashMap<const FeaturePolicy::Feature*, | 34 HashMap<const FeaturePolicy::Feature*, |
35 std::unique_ptr<FeaturePolicy::Whitelist>> | 35 std::unique_ptr<FeaturePolicy::Whitelist>> |
36 parseFeaturePolicyFromJson(std::unique_ptr<JSONArray> policyItems, | 36 parseFeaturePolicyFromJson(std::unique_ptr<JSONArray> policyItems, |
37 RefPtr<SecurityOrigin> origin, | 37 RefPtr<SecurityOrigin> origin, |
38 FeaturePolicy::FeatureList& features, | 38 FeaturePolicy::FeatureList& features, |
39 Vector<String>& messages) { | 39 Vector<String>* messages) { |
40 HashMap<const FeaturePolicy::Feature*, | 40 HashMap<const FeaturePolicy::Feature*, |
41 std::unique_ptr<FeaturePolicy::Whitelist>> | 41 std::unique_ptr<FeaturePolicy::Whitelist>> |
42 whitelists; | 42 whitelists; |
43 | 43 |
44 for (size_t i = 0; i < policyItems->size(); ++i) { | 44 for (size_t i = 0; i < policyItems->size(); ++i) { |
45 JSONObject* item = JSONObject::cast(policyItems->at(i)); | 45 JSONObject* item = JSONObject::cast(policyItems->at(i)); |
46 if (!item) { | 46 if (!item) { |
47 messages.append("Policy is not an object"); | 47 if (messages) |
| 48 messages->append("Policy is not an object"); |
48 continue; // Array element is not an object; skip | 49 continue; // Array element is not an object; skip |
49 } | 50 } |
50 | 51 |
51 for (size_t j = 0; j < item->size(); ++j) { | 52 for (size_t j = 0; j < item->size(); ++j) { |
52 JSONObject::Entry entry = item->at(j); | 53 JSONObject::Entry entry = item->at(j); |
53 String featureName = entry.first; | 54 String featureName = entry.first; |
54 JSONArray* targets = JSONArray::cast(entry.second); | 55 JSONArray* targets = JSONArray::cast(entry.second); |
55 if (!targets) { | 56 if (!targets) { |
56 messages.append("Whitelist is not an array of strings."); | 57 if (messages) |
| 58 messages->append("Whitelist is not an array of strings."); |
57 continue; | 59 continue; |
58 } | 60 } |
59 | 61 |
60 const FeaturePolicy::Feature* feature = | 62 const FeaturePolicy::Feature* feature = |
61 featureForName(featureName, features); | 63 featureForName(featureName, features); |
62 if (!feature) | 64 if (!feature) |
63 continue; // Feature is not recognized; skip | 65 continue; // Feature is not recognized; skip |
64 | 66 |
65 std::unique_ptr<FeaturePolicy::Whitelist> whitelist( | 67 std::unique_ptr<FeaturePolicy::Whitelist> whitelist( |
66 new FeaturePolicy::Whitelist); | 68 new FeaturePolicy::Whitelist); |
67 String targetString; | 69 String targetString; |
68 for (size_t j = 0; j < targets->size(); ++j) { | 70 for (size_t j = 0; j < targets->size(); ++j) { |
69 if (targets->at(j)->asString(&targetString)) { | 71 if (targets->at(j)->asString(&targetString)) { |
70 if (equalIgnoringCase(targetString, "self")) { | 72 if (equalIgnoringCase(targetString, "self")) { |
71 whitelist->add(origin); | 73 whitelist->add(origin); |
72 } else if (targetString == "*") { | 74 } else if (targetString == "*") { |
73 whitelist->addAll(); | 75 whitelist->addAll(); |
74 } else { | 76 } else { |
75 KURL originUrl = KURL(KURL(), targetString); | 77 KURL originUrl = KURL(KURL(), targetString); |
76 if (originUrl.isValid()) { | 78 if (originUrl.isValid()) { |
77 whitelist->add(SecurityOrigin::create(originUrl)); | 79 whitelist->add(SecurityOrigin::create(originUrl)); |
78 } | 80 } |
79 } | 81 } |
80 } else { | 82 } else { |
81 messages.append("Whitelist is not an array of strings."); | 83 if (messages) |
| 84 messages->append("Whitelist is not an array of strings."); |
82 } | 85 } |
83 } | 86 } |
84 whitelists.set(feature, std::move(whitelist)); | 87 whitelists.set(feature, std::move(whitelist)); |
85 } | 88 } |
86 } | 89 } |
87 return whitelists; | 90 return whitelists; |
88 } | 91 } |
89 | 92 |
90 } // namespace | 93 } // namespace |
91 | 94 |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
186 | 189 |
187 // static | 190 // static |
188 std::unique_ptr<FeaturePolicy> FeaturePolicy::createFromParentPolicy( | 191 std::unique_ptr<FeaturePolicy> FeaturePolicy::createFromParentPolicy( |
189 const FeaturePolicy* parent, | 192 const FeaturePolicy* parent, |
190 RefPtr<SecurityOrigin> currentOrigin) { | 193 RefPtr<SecurityOrigin> currentOrigin) { |
191 return createFromParentPolicy(parent, std::move(currentOrigin), | 194 return createFromParentPolicy(parent, std::move(currentOrigin), |
192 getDefaultFeatureList()); | 195 getDefaultFeatureList()); |
193 } | 196 } |
194 | 197 |
195 void FeaturePolicy::setHeaderPolicy(const String& policy, | 198 void FeaturePolicy::setHeaderPolicy(const String& policy, |
196 Vector<String>& messages) { | 199 Vector<String>* messages) { |
197 DCHECK(m_headerWhitelists.isEmpty()); | 200 DCHECK(m_headerWhitelists.isEmpty()); |
198 std::unique_ptr<JSONArray> policyJSON = parseJSONHeader(policy); | 201 std::unique_ptr<JSONArray> policyJSON = parseJSONHeader(policy); |
199 if (!policyJSON) { | 202 if (!policyJSON) { |
200 messages.append("Unable to parse header"); | 203 if (messages) |
| 204 messages->append("Unable to parse header"); |
201 return; | 205 return; |
202 } | 206 } |
203 m_headerWhitelists = parseFeaturePolicyFromJson( | 207 m_headerWhitelists = parseFeaturePolicyFromJson( |
204 std::move(policyJSON), m_origin, m_features, messages); | 208 std::move(policyJSON), m_origin, m_features, messages); |
205 } | 209 } |
206 | 210 |
207 bool FeaturePolicy::isFeatureEnabledForOrigin( | 211 bool FeaturePolicy::isFeatureEnabledForOrigin( |
208 const FeaturePolicy::Feature& feature, | 212 const FeaturePolicy::Feature& feature, |
209 const SecurityOrigin& origin) const { | 213 const SecurityOrigin& origin) const { |
210 DCHECK(m_inheritedFeatures.contains(&feature)); | 214 DCHECK(m_inheritedFeatures.contains(&feature)); |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
251 sb.append(" "); | 255 sb.append(" "); |
252 sb.append(whitelist.key->featureName); | 256 sb.append(whitelist.key->featureName); |
253 sb.append(": "); | 257 sb.append(": "); |
254 sb.append(whitelist.value->toString()); | 258 sb.append(whitelist.value->toString()); |
255 sb.append("\n"); | 259 sb.append("\n"); |
256 } | 260 } |
257 return sb.toString(); | 261 return sb.toString(); |
258 } | 262 } |
259 | 263 |
260 } // namespace blink | 264 } // namespace blink |
OLD | NEW |