isURL.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
  2. function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
  3. function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
  4. function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
  5. function _iterableToArrayLimit(arr, i) { if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return; var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
  6. function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
  7. import assertString from './util/assertString';
  8. import isFQDN from './isFQDN';
  9. import isIP from './isIP';
  10. import merge from './util/merge';
  11. /*
  12. options for isURL method
  13. require_protocol - if set as true isURL will return false if protocol is not present in the URL
  14. require_valid_protocol - isURL will check if the URL's protocol is present in the protocols option
  15. protocols - valid protocols can be modified with this option
  16. require_host - if set as false isURL will not check if host is present in the URL
  17. require_port - if set as true isURL will check if port is present in the URL
  18. allow_protocol_relative_urls - if set as true protocol relative URLs will be allowed
  19. validate_length - if set as false isURL will skip string length validation (IE maximum is 2083)
  20. */
  21. var default_url_options = {
  22. protocols: ['http', 'https', 'ftp'],
  23. require_tld: true,
  24. require_protocol: false,
  25. require_host: true,
  26. require_port: false,
  27. require_valid_protocol: true,
  28. allow_underscores: false,
  29. allow_trailing_dot: false,
  30. allow_protocol_relative_urls: false,
  31. allow_fragments: true,
  32. allow_query_components: true,
  33. validate_length: true
  34. };
  35. var wrapped_ipv6 = /^\[([^\]]+)\](?::([0-9]+))?$/;
  36. function isRegExp(obj) {
  37. return Object.prototype.toString.call(obj) === '[object RegExp]';
  38. }
  39. function checkHost(host, matches) {
  40. for (var i = 0; i < matches.length; i++) {
  41. var match = matches[i];
  42. if (host === match || isRegExp(match) && match.test(host)) {
  43. return true;
  44. }
  45. }
  46. return false;
  47. }
  48. export default function isURL(url, options) {
  49. assertString(url);
  50. if (!url || /[\s<>]/.test(url)) {
  51. return false;
  52. }
  53. if (url.indexOf('mailto:') === 0) {
  54. return false;
  55. }
  56. options = merge(options, default_url_options);
  57. if (options.validate_length && url.length >= 2083) {
  58. return false;
  59. }
  60. if (!options.allow_fragments && url.includes('#')) {
  61. return false;
  62. }
  63. if (!options.allow_query_components && (url.includes('?') || url.includes('&'))) {
  64. return false;
  65. }
  66. var protocol, auth, host, hostname, port, port_str, split, ipv6;
  67. split = url.split('#');
  68. url = split.shift();
  69. split = url.split('?');
  70. url = split.shift();
  71. split = url.split('://');
  72. if (split.length > 1) {
  73. protocol = split.shift().toLowerCase();
  74. if (options.require_valid_protocol && options.protocols.indexOf(protocol) === -1) {
  75. return false;
  76. }
  77. } else if (options.require_protocol) {
  78. return false;
  79. } else if (url.substr(0, 2) === '//') {
  80. if (!options.allow_protocol_relative_urls) {
  81. return false;
  82. }
  83. split[0] = url.substr(2);
  84. }
  85. url = split.join('://');
  86. if (url === '') {
  87. return false;
  88. }
  89. split = url.split('/');
  90. url = split.shift();
  91. if (url === '' && !options.require_host) {
  92. return true;
  93. }
  94. split = url.split('@');
  95. if (split.length > 1) {
  96. if (options.disallow_auth) {
  97. return false;
  98. }
  99. if (split[0] === '') {
  100. return false;
  101. }
  102. auth = split.shift();
  103. if (auth.indexOf(':') >= 0 && auth.split(':').length > 2) {
  104. return false;
  105. }
  106. var _auth$split = auth.split(':'),
  107. _auth$split2 = _slicedToArray(_auth$split, 2),
  108. user = _auth$split2[0],
  109. password = _auth$split2[1];
  110. if (user === '' && password === '') {
  111. return false;
  112. }
  113. }
  114. hostname = split.join('@');
  115. port_str = null;
  116. ipv6 = null;
  117. var ipv6_match = hostname.match(wrapped_ipv6);
  118. if (ipv6_match) {
  119. host = '';
  120. ipv6 = ipv6_match[1];
  121. port_str = ipv6_match[2] || null;
  122. } else {
  123. split = hostname.split(':');
  124. host = split.shift();
  125. if (split.length) {
  126. port_str = split.join(':');
  127. }
  128. }
  129. if (port_str !== null && port_str.length > 0) {
  130. port = parseInt(port_str, 10);
  131. if (!/^[0-9]+$/.test(port_str) || port <= 0 || port > 65535) {
  132. return false;
  133. }
  134. } else if (options.require_port) {
  135. return false;
  136. }
  137. if (options.host_whitelist) {
  138. return checkHost(host, options.host_whitelist);
  139. }
  140. if (!isIP(host) && !isFQDN(host, options) && (!ipv6 || !isIP(ipv6, 6))) {
  141. return false;
  142. }
  143. host = host || ipv6;
  144. if (options.host_blacklist && checkHost(host, options.host_blacklist)) {
  145. return false;
  146. }
  147. return true;
  148. }