OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "components/update_client/utils.h" | 5 #include "components/update_client/utils.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <algorithm> | 10 #include <algorithm> |
(...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
232 bool IsValidBrand(const std::string& brand) { | 232 bool IsValidBrand(const std::string& brand) { |
233 const size_t kMaxBrandSize = 4; | 233 const size_t kMaxBrandSize = 4; |
234 if (!brand.empty() && brand.size() != kMaxBrandSize) | 234 if (!brand.empty() && brand.size() != kMaxBrandSize) |
235 return false; | 235 return false; |
236 | 236 |
237 return std::find_if_not(brand.begin(), brand.end(), [](char ch) { | 237 return std::find_if_not(brand.begin(), brand.end(), [](char ch) { |
238 return base::IsAsciiAlpha(ch); | 238 return base::IsAsciiAlpha(ch); |
239 }) == brand.end(); | 239 }) == brand.end(); |
240 } | 240 } |
241 | 241 |
242 bool IsValidAp(const std::string& ap) { | 242 // Helper function. |
243 const size_t kMaxApSize = 256; | 243 // Returns true if |part| matches the expression |
244 if (ap.size() > kMaxApSize) | 244 // ^[<special_chars>a-zA-Z0-9]{min_length,max_length}$ |
| 245 bool IsValidInstallerAttributePart(const std::string& part, |
| 246 const std::string& special_chars, |
| 247 size_t min_length, |
| 248 size_t max_length) { |
| 249 if (part.size() < min_length || part.size() > max_length) |
245 return false; | 250 return false; |
246 | 251 |
247 return std::find_if_not(ap.begin(), ap.end(), [](char ch) { | 252 return std::find_if_not(part.begin(), part.end(), [&special_chars](char ch) { |
248 if (base::IsAsciiAlpha(ch) || base::IsAsciiDigit(ch)) | 253 if (base::IsAsciiAlpha(ch) || base::IsAsciiDigit(ch)) |
249 return true; | 254 return true; |
250 | 255 |
251 const char kSpecialChars[] = "+-_="; | 256 for (auto c : special_chars) { |
252 for (auto c : kSpecialChars) { | |
253 if (c == ch) | 257 if (c == ch) |
254 return true; | 258 return true; |
255 } | 259 } |
256 | 260 |
257 return false; | 261 return false; |
258 }) == ap.end(); | 262 }) == part.end(); |
| 263 } |
| 264 |
| 265 // Returns true if the |name| parameter matches ^[-_a-zA-Z0-9]{1,256}$ . |
| 266 bool IsValidInstallerAttributeName(const std::string& name) { |
| 267 return IsValidInstallerAttributePart(name, "-_", 1, 256); |
| 268 } |
| 269 |
| 270 // Returns true if the |value| parameter matches ^[-.,;+_=a-zA-Z0-9]{0,256}$ . |
| 271 bool IsValidInstallerAttributeValue(const std::string& value) { |
| 272 return IsValidInstallerAttributePart(value, "-.,;+_=", 0, 256); |
| 273 } |
| 274 |
| 275 bool IsValidInstallerAttribute(const InstallerAttribute& attr) { |
| 276 return IsValidInstallerAttributeName(attr.first) && |
| 277 IsValidInstallerAttributeValue(attr.second); |
259 } | 278 } |
260 | 279 |
261 void RemoveUnsecureUrls(std::vector<GURL>* urls) { | 280 void RemoveUnsecureUrls(std::vector<GURL>* urls) { |
262 DCHECK(urls); | 281 DCHECK(urls); |
263 urls->erase(std::remove_if( | 282 urls->erase(std::remove_if( |
264 urls->begin(), urls->end(), | 283 urls->begin(), urls->end(), |
265 [](const GURL& url) { return !url.SchemeIsCryptographic(); }), | 284 [](const GURL& url) { return !url.SchemeIsCryptographic(); }), |
266 urls->end()); | 285 urls->end()); |
267 } | 286 } |
268 | 287 |
269 } // namespace update_client | 288 } // namespace update_client |
OLD | NEW |