OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 // NB: Modelled after Mozilla's code (originally written by Pamela Greene, | 5 // NB: Modelled after Mozilla's code (originally written by Pamela Greene, |
6 // later modified by others), but almost entirely rewritten for Chrome. | 6 // later modified by others), but almost entirely rewritten for Chrome. |
7 // (netwerk/dns/src/nsEffectiveTLDService.cpp) | 7 // (netwerk/dns/src/nsEffectiveTLDService.cpp) |
8 /* ***** BEGIN LICENSE BLOCK ***** | 8 /* ***** BEGIN LICENSE BLOCK ***** |
9 * Version: MPL 1.1/GPL 2.0/LGPL 2.1 | 9 * Version: MPL 1.1/GPL 2.0/LGPL 2.1 |
10 * | 10 * |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
46 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" | 46 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
47 | 47 |
48 #include "base/logging.h" | 48 #include "base/logging.h" |
49 #include "base/strings/string_util.h" | 49 #include "base/strings/string_util.h" |
50 #include "base/strings/utf_string_conversions.h" | 50 #include "base/strings/utf_string_conversions.h" |
51 #include "net/base/net_module.h" | 51 #include "net/base/net_module.h" |
52 #include "net/base/net_util.h" | 52 #include "net/base/net_util.h" |
53 #include "url/gurl.h" | 53 #include "url/gurl.h" |
54 #include "url/url_parse.h" | 54 #include "url/url_parse.h" |
55 | 55 |
| 56 #include "effective_tld_names.cc" |
| 57 |
56 namespace net { | 58 namespace net { |
57 namespace registry_controlled_domains { | 59 namespace registry_controlled_domains { |
58 | 60 |
59 namespace { | 61 namespace { |
60 #include "effective_tld_names-inc.cc" | |
61 | 62 |
62 // See make_dafsa.py for documentation of the generated dafsa byte array. | |
63 | |
64 const unsigned char* g_graph = kDafsa; | |
65 size_t g_graph_length = sizeof(kDafsa); | |
66 | |
67 const int kNotFound = -1; | |
68 const int kExceptionRule = 1; | 63 const int kExceptionRule = 1; |
69 const int kWildcardRule = 2; | 64 const int kWildcardRule = 2; |
70 const int kPrivateRule = 4; | 65 const int kPrivateRule = 4; |
71 | 66 |
72 // Read next offset from pos. | 67 const FindDomainPtr kDefaultFindDomainFunction = Perfect_Hash::FindDomain; |
73 // Returns true if an offset could be read, false otherwise. | |
74 bool GetNextOffset(const unsigned char** pos, const unsigned char* end, | |
75 const unsigned char** offset) { | |
76 if (*pos == end) | |
77 return false; | |
78 | 68 |
79 // When reading an offset the byte array must always contain at least | 69 // 'stringpool' is defined as a macro by the gperf-generated |
80 // three more bytes to consume. First the offset to read, then a node | 70 // "effective_tld_names.cc". Provide a real constant value for it instead. |
81 // to skip over and finally a destination node. No object can be smaller | 71 const char* const kDefaultStringPool = stringpool; |
82 // than one byte. | 72 #undef stringpool |
83 CHECK_LT(*pos + 2, end); | |
84 size_t bytes_consumed; | |
85 switch (**pos & 0x60) { | |
86 case 0x60: // Read three byte offset | |
87 *offset += (((*pos)[0] & 0x1F) << 16) | ((*pos)[1] << 8) | (*pos)[2]; | |
88 bytes_consumed = 3; | |
89 break; | |
90 case 0x40: // Read two byte offset | |
91 *offset += (((*pos)[0] & 0x1F) << 8) | (*pos)[1]; | |
92 bytes_consumed = 2; | |
93 break; | |
94 default: | |
95 *offset += (*pos)[0] & 0x3F; | |
96 bytes_consumed = 1; | |
97 } | |
98 if ((**pos & 0x80) != 0) { | |
99 *pos = end; | |
100 } else { | |
101 *pos += bytes_consumed; | |
102 } | |
103 return true; | |
104 } | |
105 | 73 |
106 // Check if byte at offset is last in label. | 74 FindDomainPtr g_find_domain_function = kDefaultFindDomainFunction; |
107 bool IsEOL(const unsigned char* offset, const unsigned char* end) { | 75 const char* g_stringpool = kDefaultStringPool; |
108 CHECK_LT(offset, end); | |
109 return (*offset & 0x80) != 0; | |
110 } | |
111 | |
112 // Check if byte at offset matches first character in key. | |
113 // This version matches characters not last in label. | |
114 bool IsMatch(const unsigned char* offset, const unsigned char* end, | |
115 const char* key) { | |
116 CHECK_LT(offset, end); | |
117 return *offset == *key; | |
118 } | |
119 | |
120 // Check if byte at offset matches first character in key. | |
121 // This version matches characters last in label. | |
122 bool IsEndCharMatch(const unsigned char* offset, const unsigned char* end, | |
123 const char* key) { | |
124 CHECK_LT(offset, end); | |
125 return *offset == (*key | 0x80); | |
126 } | |
127 | |
128 // Read return value at offset. | |
129 // Returns true if a return value could be read, false otherwise. | |
130 bool GetReturnValue(const unsigned char* offset, const unsigned char* end, | |
131 int* return_value) { | |
132 CHECK_LT(offset, end); | |
133 if ((*offset & 0xE0) == 0x80) { | |
134 *return_value = *offset & 0x0F; | |
135 return true; | |
136 } | |
137 return false; | |
138 } | |
139 | |
140 // Lookup a domain key in a byte array generated by make_dafsa.py. | |
141 // The rule type is returned if key is found, otherwise kNotFound is returned. | |
142 int LookupString(const unsigned char* graph, size_t length, const char* key, | |
143 size_t key_length) { | |
144 const unsigned char* pos = graph; | |
145 const unsigned char* end = graph + length; | |
146 const unsigned char* offset = pos; | |
147 const char* key_end = key + key_length; | |
148 while (GetNextOffset(&pos, end, &offset)) { | |
149 // char <char>+ end_char offsets | |
150 // char <char>+ return value | |
151 // char end_char offsets | |
152 // char return value | |
153 // end_char offsets | |
154 // return_value | |
155 bool did_consume = false; | |
156 if (key != key_end && !IsEOL(offset, end)) { | |
157 // Leading <char> is not a match. Don't dive into this child | |
158 if (!IsMatch(offset, end, key)) | |
159 continue; | |
160 did_consume = true; | |
161 ++offset; | |
162 ++key; | |
163 // Possible matches at this point: | |
164 // <char>+ end_char offsets | |
165 // <char>+ return value | |
166 // end_char offsets | |
167 // return value | |
168 // Remove all remaining <char> nodes possible | |
169 while (!IsEOL(offset, end) && key != key_end) { | |
170 if (!IsMatch(offset, end, key)) | |
171 return kNotFound; | |
172 ++key; | |
173 ++offset; | |
174 } | |
175 } | |
176 // Possible matches at this point: | |
177 // end_char offsets | |
178 // return_value | |
179 // If one or more <char> elements were consumed, a failure | |
180 // to match is terminal. Otherwise, try the next node. | |
181 if (key == key_end) { | |
182 int return_value; | |
183 if (GetReturnValue(offset, end, &return_value)) | |
184 return return_value; | |
185 // The DAFSA guarantees that if the first char is a match, all | |
186 // remaining char elements MUST match if the key is truly present. | |
187 if (did_consume) | |
188 return kNotFound; | |
189 continue; | |
190 } | |
191 if (!IsEndCharMatch(offset, end, key)) { | |
192 if (did_consume) | |
193 return kNotFound; // Unexpected | |
194 continue; | |
195 } | |
196 ++key; | |
197 pos = ++offset; // Dive into child | |
198 } | |
199 return kNotFound; // No match | |
200 } | |
201 | 76 |
202 size_t GetRegistryLengthImpl( | 77 size_t GetRegistryLengthImpl( |
203 const std::string& host, | 78 const std::string& host, |
204 UnknownRegistryFilter unknown_filter, | 79 UnknownRegistryFilter unknown_filter, |
205 PrivateRegistryFilter private_filter) { | 80 PrivateRegistryFilter private_filter) { |
206 DCHECK(!host.empty()); | 81 DCHECK(!host.empty()); |
207 | 82 |
208 // Skip leading dots. | 83 // Skip leading dots. |
209 const size_t host_check_begin = host.find_first_not_of('.'); | 84 const size_t host_check_begin = host.find_first_not_of('.'); |
210 if (host_check_begin == std::string::npos) | 85 if (host_check_begin == std::string::npos) |
(...skipping 12 matching lines...) Expand all Loading... |
223 | 98 |
224 // Walk up the domain tree, most specific to least specific, | 99 // Walk up the domain tree, most specific to least specific, |
225 // looking for matches at each level. | 100 // looking for matches at each level. |
226 size_t prev_start = std::string::npos; | 101 size_t prev_start = std::string::npos; |
227 size_t curr_start = host_check_begin; | 102 size_t curr_start = host_check_begin; |
228 size_t next_dot = host.find('.', curr_start); | 103 size_t next_dot = host.find('.', curr_start); |
229 if (next_dot >= host_check_len) // Catches std::string::npos as well. | 104 if (next_dot >= host_check_len) // Catches std::string::npos as well. |
230 return 0; // This can't have a registry + domain. | 105 return 0; // This can't have a registry + domain. |
231 while (1) { | 106 while (1) { |
232 const char* domain_str = host.data() + curr_start; | 107 const char* domain_str = host.data() + curr_start; |
233 size_t domain_length = host_check_len - curr_start; | 108 int domain_length = host_check_len - curr_start; |
234 int type = LookupString(g_graph, g_graph_length, domain_str, domain_length); | 109 const DomainRule* rule = g_find_domain_function(domain_str, domain_length); |
235 bool do_check = | |
236 type != kNotFound && (!(type & kPrivateRule) || | |
237 private_filter == INCLUDE_PRIVATE_REGISTRIES); | |
238 | 110 |
239 // If the apparent match is a private registry and we're not including | 111 // We need to compare the string after finding a match because the |
240 // those, it can't be an actual match. | 112 // no-collisions of perfect hashing only refers to items in the set. Since |
241 if (do_check) { | 113 // we're searching for arbitrary domains, there could be collisions. |
242 // Exception rules override wildcard rules when the domain is an exact | 114 // Furthermore, if the apparent match is a private registry and we're not |
243 // match, but wildcards take precedence when there's a subdomain. | 115 // including those, it can't be an actual match. |
244 if (type & kWildcardRule && (prev_start != std::string::npos)) { | 116 if (rule) { |
245 // If prev_start == host_check_begin, then the host is the registry | 117 bool do_check = !(rule->type & kPrivateRule) || |
| 118 private_filter == INCLUDE_PRIVATE_REGISTRIES; |
| 119 if (do_check && base::strncasecmp(domain_str, |
| 120 g_stringpool + rule->name_offset, |
| 121 domain_length) == 0) { |
| 122 // Exception rules override wildcard rules when the domain is an exact |
| 123 // match, but wildcards take precedence when there's a subdomain. |
| 124 if (rule->type & kWildcardRule && (prev_start != std::string::npos)) { |
| 125 // If prev_start == host_check_begin, then the host is the registry |
| 126 // itself, so return 0. |
| 127 return (prev_start == host_check_begin) ? |
| 128 0 : (host.length() - prev_start); |
| 129 } |
| 130 |
| 131 if (rule->type & kExceptionRule) { |
| 132 if (next_dot == std::string::npos) { |
| 133 // If we get here, we had an exception rule with no dots (e.g. |
| 134 // "!foo"). This would only be valid if we had a corresponding |
| 135 // wildcard rule, which would have to be "*". But we explicitly |
| 136 // disallow that case, so this kind of rule is invalid. |
| 137 NOTREACHED() << "Invalid exception rule"; |
| 138 return 0; |
| 139 } |
| 140 return host.length() - next_dot - 1; |
| 141 } |
| 142 |
| 143 // If curr_start == host_check_begin, then the host is the registry |
246 // itself, so return 0. | 144 // itself, so return 0. |
247 return (prev_start == host_check_begin) ? 0 | 145 return (curr_start == host_check_begin) ? |
248 : (host.length() - prev_start); | 146 0 : (host.length() - curr_start); |
249 } | 147 } |
250 | |
251 if (type & kExceptionRule) { | |
252 if (next_dot == std::string::npos) { | |
253 // If we get here, we had an exception rule with no dots (e.g. | |
254 // "!foo"). This would only be valid if we had a corresponding | |
255 // wildcard rule, which would have to be "*". But we explicitly | |
256 // disallow that case, so this kind of rule is invalid. | |
257 NOTREACHED() << "Invalid exception rule"; | |
258 return 0; | |
259 } | |
260 return host.length() - next_dot - 1; | |
261 } | |
262 | |
263 // If curr_start == host_check_begin, then the host is the registry | |
264 // itself, so return 0. | |
265 return (curr_start == host_check_begin) ? 0 | |
266 : (host.length() - curr_start); | |
267 } | 148 } |
268 | 149 |
269 if (next_dot >= host_check_len) // Catches std::string::npos as well. | 150 if (next_dot >= host_check_len) // Catches std::string::npos as well. |
270 break; | 151 break; |
271 | 152 |
272 prev_start = curr_start; | 153 prev_start = curr_start; |
273 curr_start = next_dot + 1; | 154 curr_start = next_dot + 1; |
274 next_dot = host.find('.', curr_start); | 155 next_dot = host.find('.', curr_start); |
275 } | 156 } |
276 | 157 |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
372 PrivateRegistryFilter private_filter) { | 253 PrivateRegistryFilter private_filter) { |
373 url::CanonHostInfo host_info; | 254 url::CanonHostInfo host_info; |
374 const std::string canon_host(CanonicalizeHost(host, &host_info)); | 255 const std::string canon_host(CanonicalizeHost(host, &host_info)); |
375 if (canon_host.empty()) | 256 if (canon_host.empty()) |
376 return std::string::npos; | 257 return std::string::npos; |
377 if (host_info.IsIPAddress()) | 258 if (host_info.IsIPAddress()) |
378 return 0; | 259 return 0; |
379 return GetRegistryLengthImpl(canon_host, unknown_filter, private_filter); | 260 return GetRegistryLengthImpl(canon_host, unknown_filter, private_filter); |
380 } | 261 } |
381 | 262 |
382 void SetFindDomainGraph() { | 263 void SetFindDomainFunctionAndStringPoolForTesting(FindDomainPtr function, |
383 g_graph = kDafsa; | 264 const char* stringpool) { |
384 g_graph_length = sizeof(kDafsa); | 265 g_find_domain_function = function ? function : kDefaultFindDomainFunction; |
385 } | 266 g_stringpool = stringpool ? stringpool : kDefaultStringPool; |
386 | |
387 void SetFindDomainGraph(const unsigned char* domains, size_t length) { | |
388 CHECK(domains); | |
389 CHECK_NE(length, 0U); | |
390 g_graph = domains; | |
391 g_graph_length = length; | |
392 } | 267 } |
393 | 268 |
394 } // namespace registry_controlled_domains | 269 } // namespace registry_controlled_domains |
395 } // namespace net | 270 } // namespace net |
OLD | NEW |