You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
5.0KB

  1. <?php
  2. /**
  3. * Validates a host according to the IPv4, IPv6 and DNS (future) specifications.
  4. */
  5. class HTMLPurifier_AttrDef_URI_Host extends HTMLPurifier_AttrDef
  6. {
  7. /**
  8. * IPv4 sub-validator.
  9. * @type HTMLPurifier_AttrDef_URI_IPv4
  10. */
  11. protected $ipv4;
  12. /**
  13. * IPv6 sub-validator.
  14. * @type HTMLPurifier_AttrDef_URI_IPv6
  15. */
  16. protected $ipv6;
  17. public function __construct()
  18. {
  19. $this->ipv4 = new HTMLPurifier_AttrDef_URI_IPv4();
  20. $this->ipv6 = new HTMLPurifier_AttrDef_URI_IPv6();
  21. }
  22. /**
  23. * @param string $string
  24. * @param HTMLPurifier_Config $config
  25. * @param HTMLPurifier_Context $context
  26. * @return bool|string
  27. */
  28. public function validate($string, $config, $context)
  29. {
  30. $length = strlen($string);
  31. // empty hostname is OK; it's usually semantically equivalent:
  32. // the default host as defined by a URI scheme is used:
  33. //
  34. // If the URI scheme defines a default for host, then that
  35. // default applies when the host subcomponent is undefined
  36. // or when the registered name is empty (zero length).
  37. if ($string === '') {
  38. return '';
  39. }
  40. if ($length > 1 && $string[0] === '[' && $string[$length - 1] === ']') {
  41. //IPv6
  42. $ip = substr($string, 1, $length - 2);
  43. $valid = $this->ipv6->validate($ip, $config, $context);
  44. if ($valid === false) {
  45. return false;
  46. }
  47. return '[' . $valid . ']';
  48. }
  49. // need to do checks on unusual encodings too
  50. $ipv4 = $this->ipv4->validate($string, $config, $context);
  51. if ($ipv4 !== false) {
  52. return $ipv4;
  53. }
  54. // A regular domain name.
  55. // This doesn't match I18N domain names, but we don't have proper IRI support,
  56. // so force users to insert Punycode.
  57. // There is not a good sense in which underscores should be
  58. // allowed, since it's technically not! (And if you go as
  59. // far to allow everything as specified by the DNS spec...
  60. // well, that's literally everything, modulo some space limits
  61. // for the components and the overall name (which, by the way,
  62. // we are NOT checking!). So we (arbitrarily) decide this:
  63. // let's allow underscores wherever we would have allowed
  64. // hyphens, if they are enabled. This is a pretty good match
  65. // for browser behavior, for example, a large number of browsers
  66. // cannot handle foo_.example.com, but foo_bar.example.com is
  67. // fairly well supported.
  68. $underscore = $config->get('Core.AllowHostnameUnderscore') ? '_' : '';
  69. // Based off of RFC 1738, but amended so that
  70. // as per RFC 3696, the top label need only not be all numeric.
  71. // The productions describing this are:
  72. $a = '[a-z]'; // alpha
  73. $an = '[a-z0-9]'; // alphanum
  74. $and = "[a-z0-9-$underscore]"; // alphanum | "-"
  75. // domainlabel = alphanum | alphanum *( alphanum | "-" ) alphanum
  76. $domainlabel = "$an(?:$and*$an)?";
  77. // AMENDED as per RFC 3696
  78. // toplabel = alphanum | alphanum *( alphanum | "-" ) alphanum
  79. // side condition: not all numeric
  80. $toplabel = "$an(?:$and*$an)?";
  81. // hostname = *( domainlabel "." ) toplabel [ "." ]
  82. if (preg_match("/^(?:$domainlabel\.)*($toplabel)\.?$/i", $string, $matches)) {
  83. if (!ctype_digit($matches[1])) {
  84. return $string;
  85. }
  86. }
  87. // PHP 5.3 and later support this functionality natively
  88. if (function_exists('idn_to_ascii')) {
  89. return idn_to_ascii($string);
  90. // If we have Net_IDNA2 support, we can support IRIs by
  91. // punycoding them. (This is the most portable thing to do,
  92. // since otherwise we have to assume browsers support
  93. } elseif ($config->get('Core.EnableIDNA')) {
  94. $idna = new Net_IDNA2(array('encoding' => 'utf8', 'overlong' => false, 'strict' => true));
  95. // we need to encode each period separately
  96. $parts = explode('.', $string);
  97. try {
  98. $new_parts = array();
  99. foreach ($parts as $part) {
  100. $encodable = false;
  101. for ($i = 0, $c = strlen($part); $i < $c; $i++) {
  102. if (ord($part[$i]) > 0x7a) {
  103. $encodable = true;
  104. break;
  105. }
  106. }
  107. if (!$encodable) {
  108. $new_parts[] = $part;
  109. } else {
  110. $new_parts[] = $idna->encode($part);
  111. }
  112. }
  113. $string = implode('.', $new_parts);
  114. if (preg_match("/^($domainlabel\.)*$toplabel\.?$/i", $string)) {
  115. return $string;
  116. }
  117. } catch (Exception $e) {
  118. // XXX error reporting
  119. }
  120. }
  121. return false;
  122. }
  123. }
  124. // vim: et sw=4 sts=4