url_idna_icu.cc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Copyright 2013 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. // ICU-based IDNA converter.
  5. #include <stdint.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <ostream>
  9. #include "base/check_op.h"
  10. #include "third_party/icu/source/common/unicode/uidna.h"
  11. #include "third_party/icu/source/common/unicode/utypes.h"
  12. #include "url/url_canon_icu.h"
  13. #include "url/url_canon_internal.h" // for _itoa_s
  14. namespace url {
  15. // Use UIDNA, a C pointer to a UTS46/IDNA 2008 handling object opened with
  16. // uidna_openUTS46().
  17. //
  18. // We use UTS46 with BiDiCheck to migrate from IDNA 2003 (with unassigned
  19. // code points allowed) to IDNA 2008 with
  20. // the backward compatibility in mind. What it does:
  21. //
  22. // 1. Use the up-to-date Unicode data.
  23. // 2. Define a case folding/mapping with the up-to-date Unicode data as
  24. // in IDNA 2003.
  25. // 3. Use transitional mechanism for 4 deviation characters (sharp-s,
  26. // final sigma, ZWJ and ZWNJ) for now.
  27. // 4. Continue to allow symbols and punctuations.
  28. // 5. Apply new BiDi check rules more permissive than the IDNA 2003 BiDI rules.
  29. // 6. Do not apply STD3 rules
  30. // 7. Do not allow unassigned code points.
  31. //
  32. // It also closely matches what IE 10 does except for the BiDi check (
  33. // http://goo.gl/3XBhqw ).
  34. // See http://http://unicode.org/reports/tr46/ and references therein
  35. // for more details.
  36. UIDNA* GetUIDNA() {
  37. static UIDNA* uidna = [] {
  38. UErrorCode err = U_ZERO_ERROR;
  39. // TODO(jungshik): Change options as different parties (browsers,
  40. // registrars, search engines) converge toward a consensus.
  41. UIDNA* value = uidna_openUTS46(UIDNA_CHECK_BIDI, &err);
  42. if (U_FAILURE(err)) {
  43. CHECK(false) << "failed to open UTS46 data with error: "
  44. << u_errorName(err)
  45. << ". If you see this error message in a test environment "
  46. << "your test environment likely lacks the required data "
  47. << "tables for libicu. See https://crbug.com/778929.";
  48. value = nullptr;
  49. }
  50. return value;
  51. }();
  52. return uidna;
  53. }
  54. // Converts the Unicode input representing a hostname to ASCII using IDN rules.
  55. // The output must be ASCII, but is represented as wide characters.
  56. //
  57. // On success, the output will be filled with the ASCII host name and it will
  58. // return true. Unlike most other canonicalization functions, this assumes that
  59. // the output is empty. The beginning of the host will be at offset 0, and
  60. // the length of the output will be set to the length of the new host name.
  61. //
  62. // On error, this will return false. The output in this case is undefined.
  63. // TODO(jungshik): use UTF-8/ASCII version of nameToASCII.
  64. // Change the function signature and callers accordingly to avoid unnecessary
  65. // conversions in our code. In addition, consider using icu::IDNA's UTF-8/ASCII
  66. // version with StringByteSink. That way, we can avoid C wrappers and additional
  67. // string conversion.
  68. bool IDNToASCII(const char16_t* src, int src_len, CanonOutputW* output) {
  69. DCHECK(output->length() == 0); // Output buffer is assumed empty.
  70. UIDNA* uidna = GetUIDNA();
  71. DCHECK(uidna != nullptr);
  72. while (true) {
  73. UErrorCode err = U_ZERO_ERROR;
  74. UIDNAInfo info = UIDNA_INFO_INITIALIZER;
  75. int output_length = uidna_nameToASCII(uidna, src, src_len, output->data(),
  76. output->capacity(), &info, &err);
  77. // Ignore various errors for web compatibility. The options are specified
  78. // by the WHATWG URL Standard. See
  79. // - https://unicode.org/reports/tr46/
  80. // - https://url.spec.whatwg.org/#concept-domain-to-ascii
  81. // (we set beStrict to false)
  82. // Disable the "CheckHyphens" option in UTS #46. See
  83. // - https://crbug.com/804688
  84. // - https://github.com/whatwg/url/issues/267
  85. info.errors &= ~UIDNA_ERROR_HYPHEN_3_4;
  86. info.errors &= ~UIDNA_ERROR_LEADING_HYPHEN;
  87. info.errors &= ~UIDNA_ERROR_TRAILING_HYPHEN;
  88. // Disable the "VerifyDnsLength" option in UTS #46.
  89. info.errors &= ~UIDNA_ERROR_EMPTY_LABEL;
  90. info.errors &= ~UIDNA_ERROR_LABEL_TOO_LONG;
  91. info.errors &= ~UIDNA_ERROR_DOMAIN_NAME_TOO_LONG;
  92. if (U_SUCCESS(err) && info.errors == 0) {
  93. // Per WHATWG URL, it is a failure if the ToASCII output is empty.
  94. //
  95. // ICU would usually return UIDNA_ERROR_EMPTY_LABEL in this case, but we
  96. // want to continue allowing http://abc..def/ while forbidding http:///.
  97. //
  98. if (output_length == 0) {
  99. return false;
  100. }
  101. output->set_length(output_length);
  102. return true;
  103. }
  104. if (err != U_BUFFER_OVERFLOW_ERROR || info.errors != 0)
  105. return false; // Unknown error, give up.
  106. // Not enough room in our buffer, expand.
  107. output->Resize(output_length);
  108. }
  109. }
  110. } // namespace url