blob: 43b53412202bea9b87c8d46a6dccd4a85ceb626d [file] [log] [blame]
rsleevi24f64dc22015-08-07 21:39:211// Copyright 2015 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "components/url_formatter/url_formatter.h"
6
7#include <algorithm>
rsleevi24f64dc22015-08-07 21:39:218#include <utility>
9
10#include "base/lazy_instance.h"
rsleevi24f64dc22015-08-07 21:39:2111#include "base/macros.h"
jshin62a92832016-03-18 18:42:3912#include "base/numerics/safe_conversions.h"
13#include "base/strings/string_piece.h"
rsleevi24f64dc22015-08-07 21:39:2114#include "base/strings/string_util.h"
15#include "base/strings/utf_offset_string_conversions.h"
16#include "base/strings/utf_string_conversions.h"
jshin62a92832016-03-18 18:42:3917#include "base/threading/thread_local_storage.h"
rsleevi24f64dc22015-08-07 21:39:2118#include "third_party/icu/source/common/unicode/uidna.h"
19#include "third_party/icu/source/common/unicode/uniset.h"
20#include "third_party/icu/source/common/unicode/uscript.h"
jshin62a92832016-03-18 18:42:3921#include "third_party/icu/source/common/unicode/uvernum.h"
rsleevi24f64dc22015-08-07 21:39:2122#include "third_party/icu/source/i18n/unicode/regex.h"
jshin62a92832016-03-18 18:42:3923#include "third_party/icu/source/i18n/unicode/uspoof.h"
rsleevi24f64dc22015-08-07 21:39:2124#include "url/gurl.h"
25#include "url/third_party/mozilla/url_parse.h"
26
27namespace url_formatter {
28
29namespace {
30
31base::string16 IDNToUnicodeWithAdjustments(
jshin78809c4d82016-10-06 20:15:4532 base::StringPiece host,
rsleevi24f64dc22015-08-07 21:39:2133 base::OffsetAdjuster::Adjustments* adjustments);
34bool IDNToUnicodeOneComponent(const base::char16* comp,
35 size_t comp_len,
rsleevi24f64dc22015-08-07 21:39:2136 base::string16* out);
37
38class AppendComponentTransform {
39 public:
40 AppendComponentTransform() {}
41 virtual ~AppendComponentTransform() {}
42
43 virtual base::string16 Execute(
44 const std::string& component_text,
45 base::OffsetAdjuster::Adjustments* adjustments) const = 0;
46
47 // NOTE: No DISALLOW_COPY_AND_ASSIGN here, since gcc < 4.3.0 requires an
48 // accessible copy constructor in order to call AppendFormattedComponent()
49 // with an inline temporary (see http://gcc.gnu.org/bugs/#cxx%5Frvalbind ).
50};
51
52class HostComponentTransform : public AppendComponentTransform {
53 public:
jshin62a92832016-03-18 18:42:3954 HostComponentTransform() {}
rsleevi24f64dc22015-08-07 21:39:2155
56 private:
57 base::string16 Execute(
58 const std::string& component_text,
59 base::OffsetAdjuster::Adjustments* adjustments) const override {
jshin62a92832016-03-18 18:42:3960 return IDNToUnicodeWithAdjustments(component_text, adjustments);
rsleevi24f64dc22015-08-07 21:39:2161 }
rsleevi24f64dc22015-08-07 21:39:2162};
63
64class NonHostComponentTransform : public AppendComponentTransform {
65 public:
66 explicit NonHostComponentTransform(net::UnescapeRule::Type unescape_rules)
67 : unescape_rules_(unescape_rules) {}
68
69 private:
70 base::string16 Execute(
71 const std::string& component_text,
72 base::OffsetAdjuster::Adjustments* adjustments) const override {
73 return (unescape_rules_ == net::UnescapeRule::NONE)
74 ? base::UTF8ToUTF16WithAdjustments(component_text, adjustments)
75 : net::UnescapeAndDecodeUTF8URLComponentWithAdjustments(
76 component_text, unescape_rules_, adjustments);
77 }
78
79 const net::UnescapeRule::Type unescape_rules_;
80};
81
82// Transforms the portion of |spec| covered by |original_component| according to
83// |transform|. Appends the result to |output|. If |output_component| is
84// non-NULL, its start and length are set to the transformed component's new
85// start and length. If |adjustments| is non-NULL, appends adjustments (if
86// any) that reflect the transformation the original component underwent to
87// become the transformed value appended to |output|.
88void AppendFormattedComponent(const std::string& spec,
89 const url::Component& original_component,
90 const AppendComponentTransform& transform,
91 base::string16* output,
92 url::Component* output_component,
93 base::OffsetAdjuster::Adjustments* adjustments) {
94 DCHECK(output);
95 if (original_component.is_nonempty()) {
96 size_t original_component_begin =
97 static_cast<size_t>(original_component.begin);
98 size_t output_component_begin = output->length();
99 std::string component_str(spec, original_component_begin,
100 static_cast<size_t>(original_component.len));
101
102 // Transform |component_str| and modify |adjustments| appropriately.
103 base::OffsetAdjuster::Adjustments component_transform_adjustments;
104 output->append(
105 transform.Execute(component_str, &component_transform_adjustments));
106
107 // Shift all the adjustments made for this component so the offsets are
108 // valid for the original string and add them to |adjustments|.
109 for (base::OffsetAdjuster::Adjustments::iterator comp_iter =
110 component_transform_adjustments.begin();
111 comp_iter != component_transform_adjustments.end(); ++comp_iter)
112 comp_iter->original_offset += original_component_begin;
113 if (adjustments) {
114 adjustments->insert(adjustments->end(),
115 component_transform_adjustments.begin(),
116 component_transform_adjustments.end());
117 }
118
119 // Set positions of the parsed component.
120 if (output_component) {
121 output_component->begin = static_cast<int>(output_component_begin);
122 output_component->len =
123 static_cast<int>(output->length() - output_component_begin);
124 }
125 } else if (output_component) {
126 output_component->reset();
127 }
128}
129
130// If |component| is valid, its begin is incremented by |delta|.
131void AdjustComponent(int delta, url::Component* component) {
132 if (!component->is_valid())
133 return;
134
135 DCHECK(delta >= 0 || component->begin >= -delta);
136 component->begin += delta;
137}
138
139// Adjusts all the components of |parsed| by |delta|, except for the scheme.
140void AdjustAllComponentsButScheme(int delta, url::Parsed* parsed) {
141 AdjustComponent(delta, &(parsed->username));
142 AdjustComponent(delta, &(parsed->password));
143 AdjustComponent(delta, &(parsed->host));
144 AdjustComponent(delta, &(parsed->port));
145 AdjustComponent(delta, &(parsed->path));
146 AdjustComponent(delta, &(parsed->query));
147 AdjustComponent(delta, &(parsed->ref));
148}
149
150// Helper for FormatUrlWithOffsets().
151base::string16 FormatViewSourceUrl(
152 const GURL& url,
rsleevi24f64dc22015-08-07 21:39:21153 FormatUrlTypes format_types,
154 net::UnescapeRule::Type unescape_rules,
155 url::Parsed* new_parsed,
156 size_t* prefix_end,
157 base::OffsetAdjuster::Adjustments* adjustments) {
158 DCHECK(new_parsed);
159 const char kViewSource[] = "view-source:";
160 const size_t kViewSourceLength = arraysize(kViewSource) - 1;
161
162 // Format the underlying URL and record adjustments.
163 const std::string& url_str(url.possibly_invalid_spec());
164 adjustments->clear();
165 base::string16 result(
166 base::ASCIIToUTF16(kViewSource) +
167 FormatUrlWithAdjustments(GURL(url_str.substr(kViewSourceLength)),
jshin1fb76462016-04-05 22:13:03168 format_types, unescape_rules, new_parsed,
169 prefix_end, adjustments));
rsleevi24f64dc22015-08-07 21:39:21170 // Revise |adjustments| by shifting to the offsets to prefix that the above
171 // call to FormatUrl didn't get to see.
172 for (base::OffsetAdjuster::Adjustments::iterator it = adjustments->begin();
173 it != adjustments->end(); ++it)
174 it->original_offset += kViewSourceLength;
175
176 // Adjust positions of the parsed components.
177 if (new_parsed->scheme.is_nonempty()) {
178 // Assume "view-source:real-scheme" as a scheme.
179 new_parsed->scheme.len += kViewSourceLength;
180 } else {
181 new_parsed->scheme.begin = 0;
182 new_parsed->scheme.len = kViewSourceLength - 1;
183 }
184 AdjustAllComponentsButScheme(kViewSourceLength, new_parsed);
185
186 if (prefix_end)
187 *prefix_end += kViewSourceLength;
188
189 return result;
190}
191
jshin62a92832016-03-18 18:42:39192// TODO(brettw): We may want to skip this step in the case of file URLs to
193// allow unicode UNC hostnames regardless of encodings.
rsleevi24f64dc22015-08-07 21:39:21194base::string16 IDNToUnicodeWithAdjustments(
jshin78809c4d82016-10-06 20:15:45195 base::StringPiece host, base::OffsetAdjuster::Adjustments* adjustments) {
rsleevi24f64dc22015-08-07 21:39:21196 if (adjustments)
197 adjustments->clear();
198 // Convert the ASCII input to a base::string16 for ICU.
199 base::string16 input16;
200 input16.reserve(host.length());
201 input16.insert(input16.end(), host.begin(), host.end());
202
203 // Do each component of the host separately, since we enforce script matching
204 // on a per-component basis.
205 base::string16 out16;
206 for (size_t component_start = 0, component_end;
207 component_start < input16.length();
208 component_start = component_end + 1) {
209 // Find the end of the component.
210 component_end = input16.find('.', component_start);
211 if (component_end == base::string16::npos)
212 component_end = input16.length(); // For getting the last component.
213 size_t component_length = component_end - component_start;
214 size_t new_component_start = out16.length();
215 bool converted_idn = false;
216 if (component_end > component_start) {
217 // Add the substring that we just found.
218 converted_idn =
219 IDNToUnicodeOneComponent(input16.data() + component_start,
jshin62a92832016-03-18 18:42:39220 component_length, &out16);
rsleevi24f64dc22015-08-07 21:39:21221 }
222 size_t new_component_length = out16.length() - new_component_start;
223
224 if (converted_idn && adjustments) {
225 adjustments->push_back(base::OffsetAdjuster::Adjustment(
226 component_start, component_length, new_component_length));
227 }
228
229 // Need to add the dot we just found (if we found one).
230 if (component_end < input16.length())
231 out16.push_back('.');
232 }
233 return out16;
234}
235
jshin62a92832016-03-18 18:42:39236// A helper class for IDN Spoof checking, used to ensure that no IDN input is
237// spoofable per Chromium's standard of spoofability. For a more thorough
238// explanation of how spoof checking works in Chromium, see
239// http://dev.chromium.org/developers/design-documents/idn-in-google-chrome .
240class IDNSpoofChecker {
rsleevi24f64dc22015-08-07 21:39:21241 public:
jshin62a92832016-03-18 18:42:39242 IDNSpoofChecker();
243
244 // Returns true if |label| is safe to display as Unicode. In the event of
245 // library failure, all IDN inputs will be treated as unsafe.
246 bool Check(base::StringPiece16 label);
rsleevi24f64dc22015-08-07 21:39:21247
248 private:
jshin62a92832016-03-18 18:42:39249 void SetAllowedUnicodeSet(UErrorCode* status);
rsleevi24f64dc22015-08-07 21:39:21250
jshin62a92832016-03-18 18:42:39251 USpoofChecker* checker_;
252 icu::UnicodeSet deviation_characters_;
jshin62a92832016-03-18 18:42:39253 icu::UnicodeSet non_ascii_latin_letters_;
jshinb2b928a2016-11-02 17:16:22254 icu::UnicodeSet kana_letters_exceptions_;
rsleevi24f64dc22015-08-07 21:39:21255
jshin62a92832016-03-18 18:42:39256 DISALLOW_COPY_AND_ASSIGN(IDNSpoofChecker);
rsleevi24f64dc22015-08-07 21:39:21257};
258
jshin62a92832016-03-18 18:42:39259base::LazyInstance<IDNSpoofChecker>::Leaky g_idn_spoof_checker =
rsleevi24f64dc22015-08-07 21:39:21260 LAZY_INSTANCE_INITIALIZER;
jshin62a92832016-03-18 18:42:39261base::ThreadLocalStorage::StaticSlot tls_index = TLS_INITIALIZER;
rsleevi24f64dc22015-08-07 21:39:21262
jshin62a92832016-03-18 18:42:39263void OnThreadTermination(void* regex_matcher) {
264 delete reinterpret_cast<icu::RegexMatcher*>(regex_matcher);
265}
rsleevi24f64dc22015-08-07 21:39:21266
jshin62a92832016-03-18 18:42:39267IDNSpoofChecker::IDNSpoofChecker() {
268 UErrorCode status = U_ZERO_ERROR;
269 checker_ = uspoof_open(&status);
270 if (U_FAILURE(status)) {
271 checker_ = nullptr;
272 return;
rsleevi24f64dc22015-08-07 21:39:21273 }
jshin62a92832016-03-18 18:42:39274
275 // At this point, USpoofChecker has all the checks enabled except
276 // for USPOOF_CHAR_LIMIT (USPOOF_{RESTRICTION_LEVEL, INVISIBLE,
277 // MIXED_SCRIPT_CONFUSABLE, WHOLE_SCRIPT_CONFUSABLE, MIXED_NUMBERS, ANY_CASE})
278 // This default configuration is adjusted below as necessary.
279
280 // Set the restriction level to moderate. It allows mixing Latin with another
281 // script (+ COMMON and INHERITED). Except for Chinese(Han + Bopomofo),
282 // Japanese(Hiragana + Katakana + Han), and Korean(Hangul + Han), only one
283 // script other than Common and Inherited can be mixed with Latin. Cyrillic
284 // and Greek are not allowed to mix with Latin.
285 // See http://www.unicode.org/reports/tr39/#Restriction_Level_Detection
286 uspoof_setRestrictionLevel(checker_, USPOOF_MODERATELY_RESTRICTIVE);
287
288 // Restrict allowed characters in IDN labels and turn on USPOOF_CHAR_LIMIT.
289 SetAllowedUnicodeSet(&status);
290
291 // Enable the return of auxillary (non-error) information.
jshinb2b928a2016-11-02 17:16:22292 // We used to disable WHOLE_SCRIPT_CONFUSABLE check explicitly, but as of
293 // ICU 58.1, WSC is a no-op in a single string check API.
jshin62a92832016-03-18 18:42:39294 int32_t checks = uspoof_getChecks(checker_, &status) | USPOOF_AUX_INFO;
jshin62a92832016-03-18 18:42:39295 uspoof_setChecks(checker_, checks, &status);
296
297 // Four characters handled differently by IDNA 2003 and IDNA 2008. UTS46
298 // transitional processing treats them as IDNA 2003 does; maps U+00DF and
299 // U+03C2 and drops U+200[CD].
300 deviation_characters_ =
301 icu::UnicodeSet(UNICODE_STRING_SIMPLE("[\\u00df\\u03c2\\u200c\\u200d]"),
302 status);
303 deviation_characters_.freeze();
304
jshin62a92832016-03-18 18:42:39305 // Latin letters outside ASCII. 'Script_Extensions=Latin' is not necessary
306 // because additional characters pulled in with scx=Latn are not included in
307 // the allowed set.
308 non_ascii_latin_letters_ = icu::UnicodeSet(
309 UNICODE_STRING_SIMPLE("[[:Latin:] - [a-zA-Z]]"), status);
310 non_ascii_latin_letters_.freeze();
311
jshinb2b928a2016-11-02 17:16:22312 // These letters are parts of |dangerous_patterns_|.
313 kana_letters_exceptions_ = icu::UnicodeSet(UNICODE_STRING_SIMPLE(
314 "[\\u3078-\\u307a\\u30d8-\\u30da\\u30fb\\u30fc]"), status);
315 kana_letters_exceptions_.freeze();
316
jshin62a92832016-03-18 18:42:39317 DCHECK(U_SUCCESS(status));
318}
319
320bool IDNSpoofChecker::Check(base::StringPiece16 label) {
321 UErrorCode status = U_ZERO_ERROR;
322 int32_t result = uspoof_check(checker_, label.data(),
323 base::checked_cast<int32_t>(label.size()),
324 NULL, &status);
325 // If uspoof_check fails (due to library failure), or if any of the checks
326 // fail, treat the IDN as unsafe.
327 if (U_FAILURE(status) || (result & USPOOF_ALL_CHECKS))
328 return false;
329
330 icu::UnicodeString label_string(FALSE, label.data(),
331 base::checked_cast<int32_t>(label.size()));
332
333 // A punycode label with 'xn--' prefix is not subject to the URL
334 // canonicalization and is stored as it is in GURL. If it encodes a deviation
335 // character (UTS 46; e.g. U+00DF/sharp-s), it should be still shown in
336 // punycode instead of Unicode. Without this check, xn--fu-hia for
337 // 'fu<sharp-s>' would be converted to 'fu<sharp-s>' for display because
338 // "UTS 46 section 4 Processing step 4" applies validity criteria for
339 // non-transitional processing (i.e. do not map deviation characters) to any
340 // punycode labels regardless of whether transitional or non-transitional is
341 // chosen. On the other hand, 'fu<sharp-s>' typed or copy and pasted
342 // as Unicode would be canonicalized to 'fuss' by GURL and is displayed as
343 // such. See http://crbug.com/595263 .
344 if (deviation_characters_.containsSome(label_string))
345 return false;
346
347 // If there's no script mixing, the input is regarded as safe without any
jshinb2b928a2016-11-02 17:16:22348 // extra check unless it contains Kana letter exceptions. Note that
349 // the following combinations of scripts are treated as a 'logical' single
350 // script.
jshin62a92832016-03-18 18:42:39351 // - Chinese: Han, Bopomofo, Common
352 // - Japanese: Han, Hiragana, Katakana, Common
353 // - Korean: Hangul, Han, Common
jshinb2b928a2016-11-02 17:16:22354 result &= USPOOF_RESTRICTION_LEVEL_MASK;
355 if (result == USPOOF_ASCII ||
356 (result == USPOOF_SINGLE_SCRIPT_RESTRICTIVE &&
357 kana_letters_exceptions_.containsNone(label_string)))
jshin62a92832016-03-18 18:42:39358 return true;
359
360 // Additional checks for |label| with multiple scripts, one of which is Latin.
361 // Disallow non-ASCII Latin letters to mix with a non-Latin script.
362 if (non_ascii_latin_letters_.containsSome(label_string))
363 return false;
364
365 if (!tls_index.initialized())
366 tls_index.Initialize(&OnThreadTermination);
367 icu::RegexMatcher* dangerous_pattern =
368 reinterpret_cast<icu::RegexMatcher*>(tls_index.Get());
369 if (!dangerous_pattern) {
370 // Disallow the katakana no, so, zo, or n, as they may be mistaken for
371 // slashes when they're surrounded by non-Japanese scripts (i.e. scripts
372 // other than Katakana, Hiragana or Han). If {no, so, zo, n} next to a
373 // non-Japanese script on either side is disallowed, legitimate cases like
374 // '{vitamin in Katakana}b6' are blocked. Note that trying to block those
375 // characters when used alone as a label is futile because those cases
376 // would not reach here.
jshinb2b928a2016-11-02 17:16:22377 // Also disallow what used to be blocked by mixed-script-confusable (MSC)
378 // detection. ICU 58 does not detect MSC any more for a single input string.
379 // See http://bugs.icu-project.org/trac/ticket/12823 .
380 // TODO(jshin): adjust the pattern once the above ICU bug is fixed.
381 // - Disallow U+30FB (Katakana Middle Dot) and U+30FC (Hiragana-Katakana
382 // Prolonged Sound) used out-of-context.
383 // - Disallow three Hiragana letters (U+307[8-A]) or Katakana letters
384 // (U+30D[8-A]) that look exactly like each other when they're used in a
385 // label otherwise entirely in Katakna or Hiragana.
386 // - Disallow U+0585 (Armenian Small Letter Oh) and U+0581 (Armenian Small
387 // Letter Co) to be next to Latin.
388 // - Disallow Latin 'o' and 'g' next to Armenian.
jshin62a92832016-03-18 18:42:39389 dangerous_pattern = new icu::RegexMatcher(
390 icu::UnicodeString(
391 "[^\\p{scx=kana}\\p{scx=hira}\\p{scx=hani}]"
392 "[\\u30ce\\u30f3\\u30bd\\u30be]"
jshinb2b928a2016-11-02 17:16:22393 "[^\\p{scx=kana}\\p{scx=hira}\\p{scx=hani}]|"
394 "[^\\p{scx=kana}\\p{scx=hira}]\\u30fc|"
395 "\\u30fc[^\\p{scx=kana}\\p{scx=hira}]|"
396 "^[\\p{scx=kana}]+[\\u3078-\\u307a][\\p{scx=kana}]+$|"
397 "^[\\p{scx=hira}]+[\\u30d8-\\u30da][\\p{scx=hira}]+$|"
398 "[a-z]\\u30fb|\\u30fb[a-z]|"
399 "^[\\u0585\\u0581]+[a-z]|[a-z][\\u0585\\u0581]+$|"
400 "[a-z][\\u0585\\u0581]+[a-z]|"
401 "^[og]+[\\p{scx=armn}]|[\\p{scx=armn}][og]+$|"
402 "[\\p{scx=armn}][og]+[\\p{scx=armn}]", -1, US_INV),
jshin62a92832016-03-18 18:42:39403 0, status);
404 tls_index.Set(dangerous_pattern);
405 }
406 dangerous_pattern->reset(label_string);
407 return !dangerous_pattern->find();
408}
409
410void IDNSpoofChecker::SetAllowedUnicodeSet(UErrorCode* status) {
411 if (U_FAILURE(*status))
412 return;
413
414 // The recommended set is a set of characters for identifiers in a
415 // security-sensitive environment taken from UTR 39
416 // (http://unicode.org/reports/tr39/) and
417 // http://www.unicode.org/Public/security/latest/xidmodifications.txt .
418 // The inclusion set comes from "Candidate Characters for Inclusion
419 // in idenfiers" of UTR 31 (http://www.unicode.org/reports/tr31). The list
420 // may change over the time and will be updated whenever the version of ICU
421 // used in Chromium is updated.
422 const icu::UnicodeSet* recommended_set =
423 uspoof_getRecommendedUnicodeSet(status);
424 icu::UnicodeSet allowed_set;
425 allowed_set.addAll(*recommended_set);
426 const icu::UnicodeSet* inclusion_set = uspoof_getInclusionUnicodeSet(status);
427 allowed_set.addAll(*inclusion_set);
428
429 // Five aspirational scripts are taken from UTR 31 Table 6 at
430 // http://www.unicode.org/reports/tr31/#Aspirational_Use_Scripts .
431 // Not all the characters of aspirational scripts are suitable for
432 // identifiers. Therefore, only characters belonging to
433 // [:Identifier_Type=Aspirational:] (listed in 'Status/Type=Aspirational'
434 // section at
435 // http://www.unicode.org/Public/security/latest/xidmodifications.txt) are
436 // are added to the allowed set. The list has to be updated when a new
jshin424584b42016-10-21 08:15:50437 // version of Unicode is released. The current version is 9.0.0 and ICU 60
438 // will have Unicode 10.0 data.
439#if U_ICU_VERSION_MAJOR_NUM < 60
jshin62a92832016-03-18 18:42:39440 const icu::UnicodeSet aspirational_scripts(
441 icu::UnicodeString(
442 // Unified Canadian Syllabics
443 "[\\u1401-\\u166C\\u166F-\\u167F"
444 // Mongolian
445 "\\u1810-\\u1819\\u1820-\\u1877\\u1880-\\u18AA"
446 // Unified Canadian Syllabics
447 "\\u18B0-\\u18F5"
448 // Tifinagh
449 "\\u2D30-\\u2D67\\u2D7F"
450 // Yi
451 "\\uA000-\\uA48C"
452 // Miao
jshin424584b42016-10-21 08:15:50453 "\\U00016F00-\\U00016F44\\U00016F50-\\U00016F7E"
jshin62a92832016-03-18 18:42:39454 "\\U00016F8F-\\U00016F9F]",
455 -1, US_INV),
456 *status);
457 allowed_set.addAll(aspirational_scripts);
458#else
jshin424584b42016-10-21 08:15:50459#error "Update aspirational_scripts per Unicode 10.0"
jshin62a92832016-03-18 18:42:39460#endif
461
462 // U+0338 is included in the recommended set, while U+05F4 and U+2027 are in
463 // the inclusion set. However, they are blacklisted as a part of Mozilla's
464 // IDN blacklist (http://kb.mozillazine.org/Network.IDN.blacklist_chars).
465 // U+0338 and U+2027 are dropped; the former can look like a slash when
466 // rendered with a broken font, and the latter can be confused with U+30FB
467 // (Katakana Middle Dot). U+05F4 (Hebrew Punctuation Gershayim) is kept,
468 // even though it can look like a double quotation mark. Using it in Hebrew
469 // should be safe. When used with a non-Hebrew script, it'd be filtered by
470 // other checks in place.
471 allowed_set.remove(0x338u); // Combining Long Solidus Overlay
472 allowed_set.remove(0x2027u); // Hyphenation Point
473
474 uspoof_setAllowedUnicodeSet(checker_, &allowed_set, status);
rsleevi24f64dc22015-08-07 21:39:21475}
476
477// Returns true if the given Unicode host component is safe to display to the
jshin62a92832016-03-18 18:42:39478// user. Note that this function does not deal with pure ASCII domain labels at
479// all even though it's possible to make up look-alike labels with ASCII
480// characters alone.
481bool IsIDNComponentSafe(base::StringPiece16 label) {
482 return g_idn_spoof_checker.Get().Check(label);
rsleevi24f64dc22015-08-07 21:39:21483}
484
485// A wrapper to use LazyInstance<>::Leaky with ICU's UIDNA, a C pointer to
486// a UTS46/IDNA 2008 handling object opened with uidna_openUTS46().
487//
jshin62a92832016-03-18 18:42:39488// We use UTS46 with BiDiCheck to migrate from IDNA 2003 to IDNA 2008 with the
489// backward compatibility in mind. What it does:
rsleevi24f64dc22015-08-07 21:39:21490//
491// 1. Use the up-to-date Unicode data.
jshin62a92832016-03-18 18:42:39492// 2. Define a case folding/mapping with the up-to-date Unicode data as in
493// IDNA 2003.
rsleevi24f64dc22015-08-07 21:39:21494// 3. Use transitional mechanism for 4 deviation characters (sharp-s,
495// final sigma, ZWJ and ZWNJ) for now.
496// 4. Continue to allow symbols and punctuations.
497// 5. Apply new BiDi check rules more permissive than the IDNA 2003 BiDI rules.
498// 6. Do not apply STD3 rules
499// 7. Do not allow unassigned code points.
500//
501// It also closely matches what IE 10 does except for the BiDi check (
502// http://goo.gl/3XBhqw ).
jshin62a92832016-03-18 18:42:39503// See http://http://unicode.org/reports/tr46/ and references therein/ for more
504// details.
rsleevi24f64dc22015-08-07 21:39:21505struct UIDNAWrapper {
506 UIDNAWrapper() {
507 UErrorCode err = U_ZERO_ERROR;
508 // TODO(jungshik): Change options as different parties (browsers,
509 // registrars, search engines) converge toward a consensus.
510 value = uidna_openUTS46(UIDNA_CHECK_BIDI, &err);
511 if (U_FAILURE(err))
512 value = NULL;
513 }
514
515 UIDNA* value;
516};
517
jshin62a92832016-03-18 18:42:39518base::LazyInstance<UIDNAWrapper>::Leaky g_uidna = LAZY_INSTANCE_INITIALIZER;
rsleevi24f64dc22015-08-07 21:39:21519
jshin62a92832016-03-18 18:42:39520// Converts one component (label) of a host (between dots) to Unicode if safe.
521// The result will be APPENDED to the given output string and will be the
522// same as the input if it is not IDN in ACE/punycode or the IDN is unsafe to
523// display.
524// Returns whether any conversion was performed.
rsleevi24f64dc22015-08-07 21:39:21525bool IDNToUnicodeOneComponent(const base::char16* comp,
526 size_t comp_len,
rsleevi24f64dc22015-08-07 21:39:21527 base::string16* out) {
528 DCHECK(out);
529 if (comp_len == 0)
530 return false;
531
532 // Only transform if the input can be an IDN component.
533 static const base::char16 kIdnPrefix[] = {'x', 'n', '-', '-'};
534 if ((comp_len > arraysize(kIdnPrefix)) &&
brucedawsona9f0e322015-08-10 20:34:14535 !memcmp(comp, kIdnPrefix, sizeof(kIdnPrefix))) {
rsleevi24f64dc22015-08-07 21:39:21536 UIDNA* uidna = g_uidna.Get().value;
537 DCHECK(uidna != NULL);
538 size_t original_length = out->length();
jshin62a92832016-03-18 18:42:39539 int32_t output_length = 64;
rsleevi24f64dc22015-08-07 21:39:21540 UIDNAInfo info = UIDNA_INFO_INITIALIZER;
541 UErrorCode status;
542 do {
543 out->resize(original_length + output_length);
544 status = U_ZERO_ERROR;
545 // This returns the actual length required. If this is more than 64
546 // code units, |status| will be U_BUFFER_OVERFLOW_ERROR and we'll try
547 // the conversion again, but with a sufficiently large buffer.
548 output_length = uidna_labelToUnicode(
549 uidna, comp, static_cast<int32_t>(comp_len), &(*out)[original_length],
550 output_length, &info, &status);
551 } while ((status == U_BUFFER_OVERFLOW_ERROR && info.errors == 0));
552
553 if (U_SUCCESS(status) && info.errors == 0) {
554 // Converted successfully. Ensure that the converted component
555 // can be safely displayed to the user.
556 out->resize(original_length + output_length);
jshin62a92832016-03-18 18:42:39557 if (IsIDNComponentSafe(
558 base::StringPiece16(out->data() + original_length,
559 base::checked_cast<size_t>(output_length))))
rsleevi24f64dc22015-08-07 21:39:21560 return true;
561 }
562
563 // Something went wrong. Revert to original string.
564 out->resize(original_length);
565 }
566
567 // We get here with no IDN or on error, in which case we just append the
568 // literal input.
569 out->append(comp, comp_len);
570 return false;
571}
572
573} // namespace
574
575const FormatUrlType kFormatUrlOmitNothing = 0;
576const FormatUrlType kFormatUrlOmitUsernamePassword = 1 << 0;
577const FormatUrlType kFormatUrlOmitHTTP = 1 << 1;
578const FormatUrlType kFormatUrlOmitTrailingSlashOnBareHostname = 1 << 2;
579const FormatUrlType kFormatUrlOmitAll =
580 kFormatUrlOmitUsernamePassword | kFormatUrlOmitHTTP |
581 kFormatUrlOmitTrailingSlashOnBareHostname;
582
583base::string16 FormatUrl(const GURL& url,
rsleevi24f64dc22015-08-07 21:39:21584 FormatUrlTypes format_types,
585 net::UnescapeRule::Type unescape_rules,
586 url::Parsed* new_parsed,
587 size_t* prefix_end,
588 size_t* offset_for_adjustment) {
589 std::vector<size_t> offsets;
590 if (offset_for_adjustment)
591 offsets.push_back(*offset_for_adjustment);
592 base::string16 result =
jshin1fb76462016-04-05 22:13:03593 FormatUrlWithOffsets(url, format_types, unescape_rules, new_parsed,
594 prefix_end, &offsets);
rsleevi24f64dc22015-08-07 21:39:21595 if (offset_for_adjustment)
596 *offset_for_adjustment = offsets[0];
597 return result;
598}
599
600base::string16 FormatUrlWithOffsets(
601 const GURL& url,
rsleevi24f64dc22015-08-07 21:39:21602 FormatUrlTypes format_types,
603 net::UnescapeRule::Type unescape_rules,
604 url::Parsed* new_parsed,
605 size_t* prefix_end,
606 std::vector<size_t>* offsets_for_adjustment) {
607 base::OffsetAdjuster::Adjustments adjustments;
608 const base::string16& format_url_return_value =
jshin1fb76462016-04-05 22:13:03609 FormatUrlWithAdjustments(url, format_types, unescape_rules, new_parsed,
610 prefix_end, &adjustments);
rsleevi24f64dc22015-08-07 21:39:21611 base::OffsetAdjuster::AdjustOffsets(adjustments, offsets_for_adjustment);
612 if (offsets_for_adjustment) {
613 std::for_each(
614 offsets_for_adjustment->begin(), offsets_for_adjustment->end(),
615 base::LimitOffset<std::string>(format_url_return_value.length()));
616 }
617 return format_url_return_value;
618}
619
620base::string16 FormatUrlWithAdjustments(
621 const GURL& url,
rsleevi24f64dc22015-08-07 21:39:21622 FormatUrlTypes format_types,
623 net::UnescapeRule::Type unescape_rules,
624 url::Parsed* new_parsed,
625 size_t* prefix_end,
626 base::OffsetAdjuster::Adjustments* adjustments) {
627 DCHECK(adjustments != NULL);
628 adjustments->clear();
629 url::Parsed parsed_temp;
630 if (!new_parsed)
631 new_parsed = &parsed_temp;
632 else
633 *new_parsed = url::Parsed();
634
635 // Special handling for view-source:. Don't use content::kViewSourceScheme
636 // because this library shouldn't depend on chrome.
637 const char kViewSource[] = "view-source";
638 // Reject "view-source:view-source:..." to avoid deep recursion.
639 const char kViewSourceTwice[] = "view-source:view-source:";
640 if (url.SchemeIs(kViewSource) &&
641 !base::StartsWith(url.possibly_invalid_spec(), kViewSourceTwice,
642 base::CompareCase::INSENSITIVE_ASCII)) {
jshin62a92832016-03-18 18:42:39643 return FormatViewSourceUrl(url, format_types, unescape_rules,
rsleevi24f64dc22015-08-07 21:39:21644 new_parsed, prefix_end, adjustments);
645 }
646
647 // We handle both valid and invalid URLs (this will give us the spec
648 // regardless of validity).
649 const std::string& spec = url.possibly_invalid_spec();
650 const url::Parsed& parsed = url.parsed_for_possibly_invalid_spec();
651
652 // Scheme & separators. These are ASCII.
653 base::string16 url_string;
654 url_string.insert(
655 url_string.end(), spec.begin(),
656 spec.begin() + parsed.CountCharactersBefore(url::Parsed::USERNAME, true));
657 const char kHTTP[] = "http://";
658 const char kFTP[] = "ftp.";
659 // url_formatter::FixupURL() treats "ftp.foo.com" as ftp://ftp.foo.com. This
660 // means that if we trim "http://" off a URL whose host starts with "ftp." and
661 // the user inputs this into any field subject to fixup (which is basically
662 // all input fields), the meaning would be changed. (In fact, often the
663 // formatted URL is directly pre-filled into an input field.) For this reason
664 // we avoid stripping "http://" in this case.
665 bool omit_http =
666 (format_types & kFormatUrlOmitHTTP) &&
667 base::EqualsASCII(url_string, kHTTP) &&
668 !base::StartsWith(url.host(), kFTP, base::CompareCase::SENSITIVE);
669 new_parsed->scheme = parsed.scheme;
670
671 // Username & password.
672 if ((format_types & kFormatUrlOmitUsernamePassword) != 0) {
673 // Remove the username and password fields. We don't want to display those
674 // to the user since they can be used for attacks,
675 // e.g. "http://google.com:[email protected]/"
676 new_parsed->username.reset();
677 new_parsed->password.reset();
678 // Update the adjustments based on removed username and/or password.
679 if (parsed.username.is_nonempty() || parsed.password.is_nonempty()) {
680 if (parsed.username.is_nonempty() && parsed.password.is_nonempty()) {
681 // The seeming off-by-two is to account for the ':' after the username
682 // and '@' after the password.
683 adjustments->push_back(base::OffsetAdjuster::Adjustment(
684 static_cast<size_t>(parsed.username.begin),
685 static_cast<size_t>(parsed.username.len + parsed.password.len + 2),
686 0));
687 } else {
688 const url::Component* nonempty_component =
689 parsed.username.is_nonempty() ? &parsed.username : &parsed.password;
690 // The seeming off-by-one is to account for the '@' after the
691 // username/password.
692 adjustments->push_back(base::OffsetAdjuster::Adjustment(
693 static_cast<size_t>(nonempty_component->begin),
694 static_cast<size_t>(nonempty_component->len + 1), 0));
695 }
696 }
697 } else {
698 AppendFormattedComponent(spec, parsed.username,
699 NonHostComponentTransform(unescape_rules),
700 &url_string, &new_parsed->username, adjustments);
701 if (parsed.password.is_valid())
702 url_string.push_back(':');
703 AppendFormattedComponent(spec, parsed.password,
704 NonHostComponentTransform(unescape_rules),
705 &url_string, &new_parsed->password, adjustments);
706 if (parsed.username.is_valid() || parsed.password.is_valid())
707 url_string.push_back('@');
708 }
709 if (prefix_end)
710 *prefix_end = static_cast<size_t>(url_string.length());
711
712 // Host.
jshin62a92832016-03-18 18:42:39713 AppendFormattedComponent(spec, parsed.host, HostComponentTransform(),
rsleevi24f64dc22015-08-07 21:39:21714 &url_string, &new_parsed->host, adjustments);
715
716 // Port.
717 if (parsed.port.is_nonempty()) {
718 url_string.push_back(':');
719 new_parsed->port.begin = url_string.length();
720 url_string.insert(url_string.end(), spec.begin() + parsed.port.begin,
721 spec.begin() + parsed.port.end());
722 new_parsed->port.len = url_string.length() - new_parsed->port.begin;
723 } else {
724 new_parsed->port.reset();
725 }
726
727 // Path & query. Both get the same general unescape & convert treatment.
728 if (!(format_types & kFormatUrlOmitTrailingSlashOnBareHostname) ||
729 !CanStripTrailingSlash(url)) {
730 AppendFormattedComponent(spec, parsed.path,
731 NonHostComponentTransform(unescape_rules),
732 &url_string, &new_parsed->path, adjustments);
733 } else {
734 if (parsed.path.len > 0) {
735 adjustments->push_back(base::OffsetAdjuster::Adjustment(
736 parsed.path.begin, parsed.path.len, 0));
737 }
738 }
739 if (parsed.query.is_valid())
740 url_string.push_back('?');
741 AppendFormattedComponent(spec, parsed.query,
742 NonHostComponentTransform(unescape_rules),
743 &url_string, &new_parsed->query, adjustments);
744
745 // Ref. This is valid, unescaped UTF-8, so we can just convert.
746 if (parsed.ref.is_valid())
747 url_string.push_back('#');
748 AppendFormattedComponent(spec, parsed.ref,
749 NonHostComponentTransform(net::UnescapeRule::NONE),
750 &url_string, &new_parsed->ref, adjustments);
751
752 // If we need to strip out http do it after the fact.
753 if (omit_http && base::StartsWith(url_string, base::ASCIIToUTF16(kHTTP),
754 base::CompareCase::SENSITIVE)) {
755 const size_t kHTTPSize = arraysize(kHTTP) - 1;
756 url_string = url_string.substr(kHTTPSize);
757 // Because offsets in the |adjustments| are already calculated with respect
758 // to the string with the http:// prefix in it, those offsets remain correct
759 // after stripping the prefix. The only thing necessary is to add an
760 // adjustment to reflect the stripped prefix.
761 adjustments->insert(adjustments->begin(),
762 base::OffsetAdjuster::Adjustment(0, kHTTPSize, 0));
763
764 if (prefix_end)
765 *prefix_end -= kHTTPSize;
766
767 // Adjust new_parsed.
768 DCHECK(new_parsed->scheme.is_valid());
769 int delta = -(new_parsed->scheme.len + 3); // +3 for ://.
770 new_parsed->scheme.reset();
771 AdjustAllComponentsButScheme(delta, new_parsed);
772 }
773
774 return url_string;
775}
776
777bool CanStripTrailingSlash(const GURL& url) {
778 // Omit the path only for standard, non-file URLs with nothing but "/" after
779 // the hostname.
780 return url.IsStandard() && !url.SchemeIsFile() && !url.SchemeIsFileSystem() &&
csharrison88b3b712016-11-14 23:12:35781 !url.has_query() && !url.has_ref() && url.path_piece() == "/";
rsleevi24f64dc22015-08-07 21:39:21782}
783
jshin1fb76462016-04-05 22:13:03784void AppendFormattedHost(const GURL& url, base::string16* output) {
rsleevi24f64dc22015-08-07 21:39:21785 AppendFormattedComponent(
786 url.possibly_invalid_spec(), url.parsed_for_possibly_invalid_spec().host,
jshin62a92832016-03-18 18:42:39787 HostComponentTransform(), output, NULL, NULL);
rsleevi24f64dc22015-08-07 21:39:21788}
789
jshin78809c4d82016-10-06 20:15:45790base::string16 IDNToUnicode(base::StringPiece host) {
jshin1fb76462016-04-05 22:13:03791 return IDNToUnicodeWithAdjustments(host, nullptr);
rsleevi24f64dc22015-08-07 21:39:21792}
793
tfarinad7241e52015-10-07 17:16:34794base::string16 StripWWW(const base::string16& text) {
795 const base::string16 www(base::ASCIIToUTF16("www."));
796 return base::StartsWith(text, www, base::CompareCase::SENSITIVE)
797 ? text.substr(www.length()) : text;
798}
799
800base::string16 StripWWWFromHost(const GURL& url) {
801 DCHECK(url.is_valid());
802 return StripWWW(base::ASCIIToUTF16(url.host_piece()));
803}
804
805} // namespace url_formatter