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 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 } |
80 | 80 |
81 void FeaturePolicy::Whitelist::add(RefPtr<SecurityOrigin> origin) { | 81 void FeaturePolicy::Whitelist::add(RefPtr<SecurityOrigin> origin) { |
82 m_origins.append(std::move(origin)); | 82 m_origins.push_back(std::move(origin)); |
83 } | 83 } |
84 | 84 |
85 bool FeaturePolicy::Whitelist::contains(const SecurityOrigin& origin) const { | 85 bool FeaturePolicy::Whitelist::contains(const SecurityOrigin& origin) const { |
86 if (m_matchesAllOrigins) | 86 if (m_matchesAllOrigins) |
87 return true; | 87 return true; |
88 for (const auto& targetOrigin : m_origins) { | 88 for (const auto& targetOrigin : m_origins) { |
89 if (targetOrigin->isSameSchemeHostPortAndSuborigin(&origin)) | 89 if (targetOrigin->isSameSchemeHostPortAndSuborigin(&origin)) |
90 return true; | 90 return true; |
91 } | 91 } |
92 return false; | 92 return false; |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
153 RefPtr<SecurityOrigin> origin, | 153 RefPtr<SecurityOrigin> origin, |
154 Vector<String>* messages) { | 154 Vector<String>* messages) { |
155 Vector<WebFeaturePolicy::ParsedWhitelist> whitelists; | 155 Vector<WebFeaturePolicy::ParsedWhitelist> 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->append("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->append("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->append("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 WebFeaturePolicy::ParsedWhitelist 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.append(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.append(targetOrigin); | 200 origins.push_back(targetOrigin); |
201 } | 201 } |
202 } else { | 202 } else { |
203 if (messages) | 203 if (messages) |
204 messages->append("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.append(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(const WebParsedFeaturePolicy& policy) { |
215 DCHECK(m_headerWhitelists.isEmpty()); | 215 DCHECK(m_headerWhitelists.isEmpty()); |
216 for (const WebFeaturePolicy::ParsedWhitelist& parsedWhitelist : policy) { | 216 for (const WebFeaturePolicy::ParsedWhitelist& parsedWhitelist : policy) { |
217 const FeaturePolicy::Feature* feature = | 217 const FeaturePolicy::Feature* feature = |
218 featureForName(parsedWhitelist.featureName, m_features); | 218 featureForName(parsedWhitelist.featureName, m_features); |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
269 sb.append(" "); | 269 sb.append(" "); |
270 sb.append(whitelist.key->featureName); | 270 sb.append(whitelist.key->featureName); |
271 sb.append(": "); | 271 sb.append(": "); |
272 sb.append(whitelist.value->toString()); | 272 sb.append(whitelist.value->toString()); |
273 sb.append("\n"); | 273 sb.append("\n"); |
274 } | 274 } |
275 return sb.toString(); | 275 return sb.toString(); |
276 } | 276 } |
277 | 277 |
278 } // namespace blink | 278 } // namespace blink |
OLD | NEW |