Avi Drissman | 8ba1bad | 2022-09-13 19:22:36 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors |
[email protected] | dc1e47142 | 2012-04-18 19:54:31 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 716c016 | 2013-12-13 20:36:53 | [diff] [blame] | 5 | #include "components/url_matcher/url_matcher_factory.h" |
[email protected] | dc1e47142 | 2012-04-18 19:54:31 | [diff] [blame] | 6 | |
Peter Kasting | ccea0983 | 2025-01-27 18:38:22 | [diff] [blame] | 7 | #include <algorithm> |
Gyuyoung Kim | 8b084c0 | 2018-01-23 13:34:37 | [diff] [blame] | 8 | #include <memory> |
dcheng | 51ace48a | 2015-12-26 22:45:17 | [diff] [blame] | 9 | #include <utility> |
[email protected] | ffbb60a | 2013-01-23 08:24:52 | [diff] [blame] | 10 | |
Hans Wennborg | df87046c | 2020-04-28 11:06:24 | [diff] [blame] | 11 | #include "base/check.h" |
[email protected] | dc1e47142 | 2012-04-18 19:54:31 | [diff] [blame] | 12 | #include "base/lazy_instance.h" |
[email protected] | f439096 | 2013-06-11 07:29:22 | [diff] [blame] | 13 | #include "base/strings/stringprintf.h" |
[email protected] | dc1e47142 | 2012-04-18 19:54:31 | [diff] [blame] | 14 | #include "base/values.h" |
[email protected] | 716c016 | 2013-12-13 20:36:53 | [diff] [blame] | 15 | #include "components/url_matcher/url_matcher_constants.h" |
Yann Dago | e65b7ee | 2022-01-04 19:01:35 | [diff] [blame] | 16 | #include "components/url_matcher/url_util.h" |
battre | 0739c64 | 2015-12-21 17:45:27 | [diff] [blame] | 17 | #include "third_party/re2/src/re2/re2.h" |
[email protected] | dc1e47142 | 2012-04-18 19:54:31 | [diff] [blame] | 18 | |
[email protected] | 716c016 | 2013-12-13 20:36:53 | [diff] [blame] | 19 | namespace url_matcher { |
| 20 | |
[email protected] | 716c016 | 2013-12-13 20:36:53 | [diff] [blame] | 21 | namespace keys = url_matcher_constants; |
[email protected] | dc1e47142 | 2012-04-18 19:54:31 | [diff] [blame] | 22 | |
| 23 | namespace { |
[email protected] | 716c016 | 2013-12-13 20:36:53 | [diff] [blame] | 24 | |
[email protected] | dc1e47142 | 2012-04-18 19:54:31 | [diff] [blame] | 25 | // Error messages: |
| 26 | const char kInvalidPortRanges[] = "Invalid port ranges in UrlFilter."; |
| 27 | const char kVectorOfStringsExpected[] = |
[email protected] | 716c016 | 2013-12-13 20:36:53 | [diff] [blame] | 28 | "UrlFilter attribute '%s' expected a vector of strings as parameter."; |
[email protected] | dc1e47142 | 2012-04-18 19:54:31 | [diff] [blame] | 29 | const char kUnknownURLFilterAttribute[] = |
[email protected] | 716c016 | 2013-12-13 20:36:53 | [diff] [blame] | 30 | "Unknown attribute '%s' in UrlFilter."; |
[email protected] | dc1e47142 | 2012-04-18 19:54:31 | [diff] [blame] | 31 | const char kAttributeExpectedString[] = |
[email protected] | 716c016 | 2013-12-13 20:36:53 | [diff] [blame] | 32 | "UrlFilter attribute '%s' expected a string value."; |
[email protected] | 7a80ce8 | 2012-10-15 20:37:21 | [diff] [blame] | 33 | const char kUnparseableRegexString[] = |
[email protected] | 716c016 | 2013-12-13 20:36:53 | [diff] [blame] | 34 | "Could not parse regular expression '%s': %s"; |
| 35 | const char kLowerCaseExpected[] = "%s values need to be in lower case."; |
Chen Mor-Yosef | 281dee84 | 2024-02-06 18:37:40 | [diff] [blame] | 36 | const char kInvalidCidrBlocks[] = "Invalid CIDR blocks in UrlFilter."; |
[email protected] | dc1e47142 | 2012-04-18 19:54:31 | [diff] [blame] | 37 | |
[email protected] | 716c016 | 2013-12-13 20:36:53 | [diff] [blame] | 38 | // Registry for all factory methods of URLMatcherConditionFactory |
[email protected] | dc1e47142 | 2012-04-18 19:54:31 | [diff] [blame] | 39 | // that allows translating string literals from the extension API into |
| 40 | // the corresponding factory method to be called. |
| 41 | class URLMatcherConditionFactoryMethods { |
| 42 | public: |
| 43 | URLMatcherConditionFactoryMethods() { |
[email protected] | 716c016 | 2013-12-13 20:36:53 | [diff] [blame] | 44 | typedef URLMatcherConditionFactory F; |
[email protected] | dc1e47142 | 2012-04-18 19:54:31 | [diff] [blame] | 45 | factory_methods_[keys::kHostContainsKey] = &F::CreateHostContainsCondition; |
| 46 | factory_methods_[keys::kHostEqualsKey] = &F::CreateHostEqualsCondition; |
| 47 | factory_methods_[keys::kHostPrefixKey] = &F::CreateHostPrefixCondition; |
| 48 | factory_methods_[keys::kHostSuffixKey] = &F::CreateHostSuffixCondition; |
[email protected] | 2280dc8 | 2013-04-11 20:04:01 | [diff] [blame] | 49 | factory_methods_[keys::kOriginAndPathMatchesKey] = |
| 50 | &F::CreateOriginAndPathMatchesCondition; |
[email protected] | dc1e47142 | 2012-04-18 19:54:31 | [diff] [blame] | 51 | factory_methods_[keys::kPathContainsKey] = &F::CreatePathContainsCondition; |
| 52 | factory_methods_[keys::kPathEqualsKey] = &F::CreatePathEqualsCondition; |
| 53 | factory_methods_[keys::kPathPrefixKey] = &F::CreatePathPrefixCondition; |
| 54 | factory_methods_[keys::kPathSuffixKey] = &F::CreatePathSuffixCondition; |
| 55 | factory_methods_[keys::kQueryContainsKey] = |
| 56 | &F::CreateQueryContainsCondition; |
| 57 | factory_methods_[keys::kQueryEqualsKey] = &F::CreateQueryEqualsCondition; |
| 58 | factory_methods_[keys::kQueryPrefixKey] = &F::CreateQueryPrefixCondition; |
| 59 | factory_methods_[keys::kQuerySuffixKey] = &F::CreateQuerySuffixCondition; |
| 60 | factory_methods_[keys::kURLContainsKey] = &F::CreateURLContainsCondition; |
| 61 | factory_methods_[keys::kURLEqualsKey] = &F::CreateURLEqualsCondition; |
| 62 | factory_methods_[keys::kURLPrefixKey] = &F::CreateURLPrefixCondition; |
| 63 | factory_methods_[keys::kURLSuffixKey] = &F::CreateURLSuffixCondition; |
[email protected] | 5bcf3b7 | 2012-09-14 00:20:28 | [diff] [blame] | 64 | factory_methods_[keys::kURLMatchesKey] = &F::CreateURLMatchesCondition; |
[email protected] | dc1e47142 | 2012-04-18 19:54:31 | [diff] [blame] | 65 | } |
| 66 | |
Peter Boström | 9f667c38 | 2021-10-01 20:09:31 | [diff] [blame] | 67 | URLMatcherConditionFactoryMethods(const URLMatcherConditionFactoryMethods&) = |
| 68 | delete; |
| 69 | URLMatcherConditionFactoryMethods& operator=( |
| 70 | const URLMatcherConditionFactoryMethods&) = delete; |
| 71 | |
[email protected] | dc1e47142 | 2012-04-18 19:54:31 | [diff] [blame] | 72 | // Returns whether a factory method for the specified |pattern_type| (e.g. |
| 73 | // "host_suffix") is known. |
| 74 | bool Contains(const std::string& pattern_type) const { |
| 75 | return factory_methods_.find(pattern_type) != factory_methods_.end(); |
| 76 | } |
| 77 | |
| 78 | // Creates a URLMatcherCondition instance from |url_matcher_condition_factory| |
| 79 | // of the given |pattern_type| (e.g. "host_suffix") for the given |
| 80 | // |pattern_value| (e.g. "example.com"). |
| 81 | // The |pattern_type| needs to be known to this class (see Contains()) or |
| 82 | // a CHECK is triggered. |
[email protected] | 716c016 | 2013-12-13 20:36:53 | [diff] [blame] | 83 | URLMatcherCondition Call( |
| 84 | URLMatcherConditionFactory* url_matcher_condition_factory, |
[email protected] | dc1e47142 | 2012-04-18 19:54:31 | [diff] [blame] | 85 | const std::string& pattern_type, |
| 86 | const std::string& pattern_value) const { |
jdoerrie | 3feb185 | 2018-10-05 12:16:44 | [diff] [blame] | 87 | auto i = factory_methods_.find(pattern_type); |
[email protected] | dc1e47142 | 2012-04-18 19:54:31 | [diff] [blame] | 88 | CHECK(i != factory_methods_.end()); |
| 89 | const FactoryMethod& method = i->second; |
| 90 | return (url_matcher_condition_factory->*method)(pattern_value); |
| 91 | } |
| 92 | |
| 93 | private: |
[email protected] | 716c016 | 2013-12-13 20:36:53 | [diff] [blame] | 94 | typedef URLMatcherCondition |
| 95 | (URLMatcherConditionFactory::* FactoryMethod) |
[email protected] | dc1e47142 | 2012-04-18 19:54:31 | [diff] [blame] | 96 | (const std::string& prefix); |
| 97 | typedef std::map<std::string, FactoryMethod> FactoryMethods; |
| 98 | |
| 99 | FactoryMethods factory_methods_; |
[email protected] | dc1e47142 | 2012-04-18 19:54:31 | [diff] [blame] | 100 | }; |
| 101 | |
scottmg | 5e65e3a | 2017-03-08 08:48:46 | [diff] [blame] | 102 | static base::LazyInstance<URLMatcherConditionFactoryMethods>::DestructorAtExit |
[email protected] | dc1e47142 | 2012-04-18 19:54:31 | [diff] [blame] | 103 | g_url_matcher_condition_factory_methods = LAZY_INSTANCE_INITIALIZER; |
| 104 | |
| 105 | } // namespace |
| 106 | |
[email protected] | dc1e47142 | 2012-04-18 19:54:31 | [diff] [blame] | 107 | // static |
| 108 | scoped_refptr<URLMatcherConditionSet> |
| 109 | URLMatcherFactory::CreateFromURLFilterDictionary( |
| 110 | URLMatcherConditionFactory* url_matcher_condition_factory, |
Dan Sanders | ab2a4716 | 2022-04-27 20:12:00 | [diff] [blame] | 111 | const base::Value::Dict& url_filter_dict, |
Peter Kasting | 78549f3 | 2022-05-31 18:20:20 | [diff] [blame] | 112 | base::MatcherStringPattern::ID id, |
[email protected] | dc1e47142 | 2012-04-18 19:54:31 | [diff] [blame] | 113 | std::string* error) { |
dcheng | 3f767dc3 | 2016-04-25 22:54:22 | [diff] [blame] | 114 | std::unique_ptr<URLMatcherSchemeFilter> url_matcher_schema_filter; |
| 115 | std::unique_ptr<URLMatcherPortFilter> url_matcher_port_filter; |
Chen Mor-Yosef | 281dee84 | 2024-02-06 18:37:40 | [diff] [blame] | 116 | std::unique_ptr<URLMatcherCidrBlockFilter> url_matcher_cidr_block_filter; |
[email protected] | dc1e47142 | 2012-04-18 19:54:31 | [diff] [blame] | 117 | URLMatcherConditionSet::Conditions url_matcher_conditions; |
| 118 | |
Dan Sanders | ab2a4716 | 2022-04-27 20:12:00 | [diff] [blame] | 119 | for (const auto iter : url_filter_dict) { |
| 120 | const std::string& condition_attribute_name = iter.first; |
| 121 | const base::Value& condition_attribute_value = iter.second; |
[email protected] | dc1e47142 | 2012-04-18 19:54:31 | [diff] [blame] | 122 | if (IsURLMatcherConditionAttribute(condition_attribute_name)) { |
| 123 | // Handle {host, path, ...}{Prefix, Suffix, Contains, Equals}. |
| 124 | URLMatcherCondition url_matcher_condition = |
| 125 | CreateURLMatcherCondition( |
| 126 | url_matcher_condition_factory, |
| 127 | condition_attribute_name, |
| 128 | &condition_attribute_value, |
| 129 | error); |
| 130 | if (!error->empty()) |
Ivan Kotenkov | 75b1c3a | 2017-10-24 14:47:24 | [diff] [blame] | 131 | return scoped_refptr<URLMatcherConditionSet>(nullptr); |
[email protected] | dc1e47142 | 2012-04-18 19:54:31 | [diff] [blame] | 132 | url_matcher_conditions.insert(url_matcher_condition); |
| 133 | } else if (condition_attribute_name == keys::kSchemesKey) { |
| 134 | // Handle scheme. |
| 135 | url_matcher_schema_filter = CreateURLMatcherScheme( |
| 136 | &condition_attribute_value, error); |
| 137 | if (!error->empty()) |
Ivan Kotenkov | 75b1c3a | 2017-10-24 14:47:24 | [diff] [blame] | 138 | return scoped_refptr<URLMatcherConditionSet>(nullptr); |
[email protected] | dc1e47142 | 2012-04-18 19:54:31 | [diff] [blame] | 139 | } else if (condition_attribute_name == keys::kPortsKey) { |
| 140 | // Handle ports. |
| 141 | url_matcher_port_filter = CreateURLMatcherPorts( |
| 142 | &condition_attribute_value, error); |
| 143 | if (!error->empty()) |
Ivan Kotenkov | 75b1c3a | 2017-10-24 14:47:24 | [diff] [blame] | 144 | return scoped_refptr<URLMatcherConditionSet>(nullptr); |
Chen Mor-Yosef | 281dee84 | 2024-02-06 18:37:40 | [diff] [blame] | 145 | } else if (condition_attribute_name == keys::kCidrBlocksKey) { |
| 146 | // Handle CIDR blocks. |
| 147 | url_matcher_cidr_block_filter = |
| 148 | CreateURLMatcherCidrBlocks(&condition_attribute_value, error); |
| 149 | if (!error->empty()) { |
| 150 | return scoped_refptr<URLMatcherConditionSet>(nullptr); |
| 151 | } |
[email protected] | dc1e47142 | 2012-04-18 19:54:31 | [diff] [blame] | 152 | } else { |
| 153 | // Handle unknown attributes. |
[email protected] | 716c016 | 2013-12-13 20:36:53 | [diff] [blame] | 154 | *error = base::StringPrintf(kUnknownURLFilterAttribute, |
| 155 | condition_attribute_name.c_str()); |
Ivan Kotenkov | 75b1c3a | 2017-10-24 14:47:24 | [diff] [blame] | 156 | return scoped_refptr<URLMatcherConditionSet>(nullptr); |
[email protected] | dc1e47142 | 2012-04-18 19:54:31 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | |
| 160 | // As the URL is the preliminary matching criterion that triggers the tests |
| 161 | // for the remaining condition attributes, we insert an empty URL match if |
| 162 | // no other url match conditions were specified. Such an empty URL is always |
| 163 | // matched. |
| 164 | if (url_matcher_conditions.empty()) { |
| 165 | url_matcher_conditions.insert( |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 166 | url_matcher_condition_factory->CreateHostPrefixCondition( |
| 167 | std::string())); |
[email protected] | dc1e47142 | 2012-04-18 19:54:31 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | scoped_refptr<URLMatcherConditionSet> url_matcher_condition_set( |
| 171 | new URLMatcherConditionSet(id, url_matcher_conditions, |
dcheng | 51ace48a | 2015-12-26 22:45:17 | [diff] [blame] | 172 | std::move(url_matcher_schema_filter), |
Chen Mor-Yosef | 281dee84 | 2024-02-06 18:37:40 | [diff] [blame] | 173 | std::move(url_matcher_port_filter), |
| 174 | std::move(url_matcher_cidr_block_filter))); |
[email protected] | dc1e47142 | 2012-04-18 19:54:31 | [diff] [blame] | 175 | return url_matcher_condition_set; |
| 176 | } |
| 177 | |
| 178 | // static |
| 179 | bool URLMatcherFactory::IsURLMatcherConditionAttribute( |
| 180 | const std::string& condition_attribute_name) { |
| 181 | return g_url_matcher_condition_factory_methods.Get().Contains( |
| 182 | condition_attribute_name); |
| 183 | } |
| 184 | |
[email protected] | ffbb60a | 2013-01-23 08:24:52 | [diff] [blame] | 185 | namespace { |
| 186 | |
| 187 | // Returns true if some alphabetic characters in this string are upper case. |
| 188 | bool ContainsUpperCase(const std::string& str) { |
Peter Kasting | ccea0983 | 2025-01-27 18:38:22 | [diff] [blame] | 189 | return std::ranges::any_of(str, ::isupper); |
[email protected] | ffbb60a | 2013-01-23 08:24:52 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | } // namespace |
| 193 | |
[email protected] | dc1e47142 | 2012-04-18 19:54:31 | [diff] [blame] | 194 | // static |
| 195 | URLMatcherCondition URLMatcherFactory::CreateURLMatcherCondition( |
| 196 | URLMatcherConditionFactory* url_matcher_condition_factory, |
| 197 | const std::string& condition_attribute_name, |
| 198 | const base::Value* value, |
| 199 | std::string* error) { |
tom | 4970e29 | 2021-07-16 14:27:52 | [diff] [blame] | 200 | if (!value->is_string()) { |
[email protected] | 716c016 | 2013-12-13 20:36:53 | [diff] [blame] | 201 | *error = base::StringPrintf(kAttributeExpectedString, |
| 202 | condition_attribute_name.c_str()); |
[email protected] | dc1e47142 | 2012-04-18 19:54:31 | [diff] [blame] | 203 | return URLMatcherCondition(); |
| 204 | } |
tom | 4970e29 | 2021-07-16 14:27:52 | [diff] [blame] | 205 | const std::string& str_value = value->GetString(); |
[email protected] | ffbb60a | 2013-01-23 08:24:52 | [diff] [blame] | 206 | if (condition_attribute_name == keys::kHostContainsKey || |
| 207 | condition_attribute_name == keys::kHostPrefixKey || |
| 208 | condition_attribute_name == keys::kHostSuffixKey || |
| 209 | condition_attribute_name == keys::kHostEqualsKey) { |
| 210 | if (ContainsUpperCase(str_value)) { |
[email protected] | 716c016 | 2013-12-13 20:36:53 | [diff] [blame] | 211 | *error = base::StringPrintf(kLowerCaseExpected, "Host"); |
[email protected] | ffbb60a | 2013-01-23 08:24:52 | [diff] [blame] | 212 | return URLMatcherCondition(); |
| 213 | } |
| 214 | } |
| 215 | |
[email protected] | 7a80ce8 | 2012-10-15 20:37:21 | [diff] [blame] | 216 | // Test regular expressions for validity. |
[email protected] | 1a3a021 | 2013-04-29 13:14:27 | [diff] [blame] | 217 | if (condition_attribute_name == keys::kURLMatchesKey || |
| 218 | condition_attribute_name == keys::kOriginAndPathMatchesKey) { |
[email protected] | 7a80ce8 | 2012-10-15 20:37:21 | [diff] [blame] | 219 | re2::RE2 regex(str_value); |
| 220 | if (!regex.ok()) { |
[email protected] | 716c016 | 2013-12-13 20:36:53 | [diff] [blame] | 221 | *error = base::StringPrintf( |
| 222 | kUnparseableRegexString, str_value.c_str(), regex.error().c_str()); |
[email protected] | 7a80ce8 | 2012-10-15 20:37:21 | [diff] [blame] | 223 | return URLMatcherCondition(); |
| 224 | } |
| 225 | } |
[email protected] | dc1e47142 | 2012-04-18 19:54:31 | [diff] [blame] | 226 | return g_url_matcher_condition_factory_methods.Get().Call( |
| 227 | url_matcher_condition_factory, condition_attribute_name, str_value); |
| 228 | } |
| 229 | |
| 230 | // static |
dcheng | 3f767dc3 | 2016-04-25 22:54:22 | [diff] [blame] | 231 | std::unique_ptr<URLMatcherSchemeFilter> |
| 232 | URLMatcherFactory::CreateURLMatcherScheme(const base::Value* value, |
| 233 | std::string* error) { |
[email protected] | dc1e47142 | 2012-04-18 19:54:31 | [diff] [blame] | 234 | std::vector<std::string> schemas; |
Yann Dago | e65b7ee | 2022-01-04 19:01:35 | [diff] [blame] | 235 | if (!util::GetAsStringVector(value, &schemas)) { |
[email protected] | 716c016 | 2013-12-13 20:36:53 | [diff] [blame] | 236 | *error = base::StringPrintf(kVectorOfStringsExpected, keys::kSchemesKey); |
dcheng | 3f767dc3 | 2016-04-25 22:54:22 | [diff] [blame] | 237 | return nullptr; |
[email protected] | dc1e47142 | 2012-04-18 19:54:31 | [diff] [blame] | 238 | } |
[email protected] | ffbb60a | 2013-01-23 08:24:52 | [diff] [blame] | 239 | for (std::vector<std::string>::const_iterator it = schemas.begin(); |
| 240 | it != schemas.end(); ++it) { |
| 241 | if (ContainsUpperCase(*it)) { |
[email protected] | 716c016 | 2013-12-13 20:36:53 | [diff] [blame] | 242 | *error = base::StringPrintf(kLowerCaseExpected, "Scheme"); |
dcheng | 3f767dc3 | 2016-04-25 22:54:22 | [diff] [blame] | 243 | return nullptr; |
[email protected] | ffbb60a | 2013-01-23 08:24:52 | [diff] [blame] | 244 | } |
| 245 | } |
Gyuyoung Kim | 8b084c0 | 2018-01-23 13:34:37 | [diff] [blame] | 246 | return std::make_unique<URLMatcherSchemeFilter>(schemas); |
[email protected] | dc1e47142 | 2012-04-18 19:54:31 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | // static |
dcheng | 3f767dc3 | 2016-04-25 22:54:22 | [diff] [blame] | 250 | std::unique_ptr<URLMatcherPortFilter> URLMatcherFactory::CreateURLMatcherPorts( |
[email protected] | dc1e47142 | 2012-04-18 19:54:31 | [diff] [blame] | 251 | const base::Value* value, |
| 252 | std::string* error) { |
| 253 | std::vector<URLMatcherPortFilter::Range> ranges; |
Alex Cooper | f017a03 | 2021-07-14 07:58:16 | [diff] [blame] | 254 | if (!value->is_list()) { |
[email protected] | dc1e47142 | 2012-04-18 19:54:31 | [diff] [blame] | 255 | *error = kInvalidPortRanges; |
dcheng | 3f767dc3 | 2016-04-25 22:54:22 | [diff] [blame] | 256 | return nullptr; |
[email protected] | dc1e47142 | 2012-04-18 19:54:31 | [diff] [blame] | 257 | } |
David Bertoni | 0c66746 | 2022-09-21 08:00:34 | [diff] [blame] | 258 | const base::Value::List& value_list = value->GetList(); |
[email protected] | dc1e47142 | 2012-04-18 19:54:31 | [diff] [blame] | 259 | |
Alex Cooper | f017a03 | 2021-07-14 07:58:16 | [diff] [blame] | 260 | for (const auto& entry : value_list) { |
Minoru Chikamune | 2ab98e9 | 2021-04-17 02:21:18 | [diff] [blame] | 261 | if (entry.is_int()) { |
| 262 | ranges.push_back(URLMatcherPortFilter::CreateRange(entry.GetInt())); |
Alex Cooper | f017a03 | 2021-07-14 07:58:16 | [diff] [blame] | 263 | } else if (entry.is_list()) { |
David Bertoni | 0c66746 | 2022-09-21 08:00:34 | [diff] [blame] | 264 | const base::Value::List& entry_list = entry.GetList(); |
Alex Cooper | f017a03 | 2021-07-14 07:58:16 | [diff] [blame] | 265 | if (entry_list.size() != 2u || !entry_list[0].is_int() || |
| 266 | !entry_list[1].is_int()) { |
[email protected] | dc1e47142 | 2012-04-18 19:54:31 | [diff] [blame] | 267 | *error = kInvalidPortRanges; |
dcheng | 3f767dc3 | 2016-04-25 22:54:22 | [diff] [blame] | 268 | return nullptr; |
[email protected] | dc1e47142 | 2012-04-18 19:54:31 | [diff] [blame] | 269 | } |
Alex Cooper | f017a03 | 2021-07-14 07:58:16 | [diff] [blame] | 270 | int from = entry_list[0].GetInt(); |
| 271 | int to = entry_list[1].GetInt(); |
[email protected] | dc1e47142 | 2012-04-18 19:54:31 | [diff] [blame] | 272 | ranges.push_back(URLMatcherPortFilter::CreateRange(from, to)); |
| 273 | } else { |
| 274 | *error = kInvalidPortRanges; |
dcheng | 3f767dc3 | 2016-04-25 22:54:22 | [diff] [blame] | 275 | return nullptr; |
[email protected] | dc1e47142 | 2012-04-18 19:54:31 | [diff] [blame] | 276 | } |
| 277 | } |
| 278 | |
Gyuyoung Kim | 8b084c0 | 2018-01-23 13:34:37 | [diff] [blame] | 279 | return std::make_unique<URLMatcherPortFilter>(ranges); |
[email protected] | dc1e47142 | 2012-04-18 19:54:31 | [diff] [blame] | 280 | } |
| 281 | |
Chen Mor-Yosef | 281dee84 | 2024-02-06 18:37:40 | [diff] [blame] | 282 | // static |
| 283 | std::unique_ptr<URLMatcherCidrBlockFilter> |
| 284 | URLMatcherFactory::CreateURLMatcherCidrBlocks(const base::Value* value, |
| 285 | std::string* error) { |
| 286 | std::vector<URLMatcherCidrBlockFilter::CidrBlock> cidr_blocks; |
| 287 | if (!value->is_list()) { |
| 288 | *error = kInvalidCidrBlocks; |
| 289 | return nullptr; |
| 290 | } |
| 291 | |
| 292 | cidr_blocks.reserve(value->GetList().size()); |
| 293 | for (const auto& entry : value->GetList()) { |
| 294 | if (!entry.is_string()) { |
| 295 | *error = kInvalidCidrBlocks; |
| 296 | return nullptr; |
| 297 | } |
| 298 | |
| 299 | base::expected<URLMatcherCidrBlockFilter::CidrBlock, std::string> |
| 300 | cidr_block = |
| 301 | URLMatcherCidrBlockFilter::CreateCidrBlock(entry.GetString()); |
| 302 | if (!cidr_block.has_value()) { |
| 303 | *error = cidr_block.error(); |
| 304 | return nullptr; |
| 305 | } |
| 306 | |
| 307 | cidr_blocks.push_back(std::move(*cidr_block)); |
| 308 | } |
| 309 | |
| 310 | return std::make_unique<URLMatcherCidrBlockFilter>(std::move(cidr_blocks)); |
| 311 | } |
| 312 | |
[email protected] | 716c016 | 2013-12-13 20:36:53 | [diff] [blame] | 313 | } // namespace url_matcher |