isURL.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = isURL;
  6. var _assertString = _interopRequireDefault(require("./util/assertString"));
  7. var _isFQDN = _interopRequireDefault(require("./isFQDN"));
  8. var _isIP = _interopRequireDefault(require("./isIP"));
  9. var _merge = _interopRequireDefault(require("./util/merge"));
  10. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  11. function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
  12. 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."); }
  13. 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); }
  14. 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; }
  15. 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; }
  16. function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
  17. /*
  18. options for isURL method
  19. require_protocol - if set as true isURL will return false if protocol is not present in the URL
  20. require_valid_protocol - isURL will check if the URL's protocol is present in the protocols option
  21. protocols - valid protocols can be modified with this option
  22. require_host - if set as false isURL will not check if host is present in the URL
  23. require_port - if set as true isURL will check if port is present in the URL
  24. allow_protocol_relative_urls - if set as true protocol relative URLs will be allowed
  25. validate_length - if set as false isURL will skip string length validation (IE maximum is 2083)
  26. */
  27. var default_url_options = {
  28. protocols: ['http', 'https', 'ftp'],
  29. require_tld: true,
  30. require_protocol: false,
  31. require_host: true,
  32. require_port: false,
  33. require_valid_protocol: true,
  34. allow_underscores: false,
  35. allow_trailing_dot: false,
  36. allow_protocol_relative_urls: false,
  37. allow_fragments: true,
  38. allow_query_components: true,
  39. validate_length: true
  40. };
  41. var wrapped_ipv6 = /^\[([^\]]+)\](?::([0-9]+))?$/;
  42. function isRegExp(obj) {
  43. return Object.prototype.toString.call(obj) === '[object RegExp]';
  44. }
  45. function checkHost(host, matches) {
  46. for (var i = 0; i < matches.length; i++) {
  47. var match = matches[i];
  48. if (host === match || isRegExp(match) && match.test(host)) {
  49. return true;
  50. }
  51. }
  52. return false;
  53. }
  54. function isURL(url, options) {
  55. (0, _assertString.default)(url);
  56. if (!url || /[\s<>]/.test(url)) {
  57. return false;
  58. }
  59. if (url.indexOf('mailto:') === 0) {
  60. return false;
  61. }
  62. options = (0, _merge.default)(options, default_url_options);
  63. if (options.validate_length && url.length >= 2083) {
  64. return false;
  65. }
  66. if (!options.allow_fragments && url.includes('#')) {
  67. return false;
  68. }
  69. if (!options.allow_query_components && (url.includes('?') || url.includes('&'))) {
  70. return false;
  71. }
  72. var protocol, auth, host, hostname, port, port_str, split, ipv6;
  73. split = url.split('#');
  74. url = split.shift();
  75. split = url.split('?');
  76. url = split.shift();
  77. split = url.split('://');
  78. if (split.length > 1) {
  79. protocol = split.shift().toLowerCase();
  80. if (options.require_valid_protocol && options.protocols.indexOf(protocol) === -1) {
  81. return false;
  82. }
  83. } else if (options.require_protocol) {
  84. return false;
  85. } else if (url.substr(0, 2) === '//') {
  86. if (!options.allow_protocol_relative_urls) {
  87. return false;
  88. }
  89. split[0] = url.substr(2);
  90. }
  91. url = split.join('://');
  92. if (url === '') {
  93. return false;
  94. }
  95. split = url.split('/');
  96. url = split.shift();
  97. if (url === '' && !options.require_host) {
  98. return true;
  99. }
  100. split = url.split('@');
  101. if (split.length > 1) {
  102. if (options.disallow_auth) {
  103. return false;
  104. }
  105. if (split[0] === '') {
  106. return false;
  107. }
  108. auth = split.shift();
  109. if (auth.indexOf(':') >= 0 && auth.split(':').length > 2) {
  110. return false;
  111. }
  112. var _auth$split = auth.split(':'),
  113. _auth$split2 = _slicedToArray(_auth$split, 2),
  114. user = _auth$split2[0],
  115. password = _auth$split2[1];
  116. if (user === '' && password === '') {
  117. return false;
  118. }
  119. }
  120. hostname = split.join('@');
  121. port_str = null;
  122. ipv6 = null;
  123. var ipv6_match = hostname.match(wrapped_ipv6);
  124. if (ipv6_match) {
  125. host = '';
  126. ipv6 = ipv6_match[1];
  127. port_str = ipv6_match[2] || null;
  128. } else {
  129. split = hostname.split(':');
  130. host = split.shift();
  131. if (split.length) {
  132. port_str = split.join(':');
  133. }
  134. }
  135. if (port_str !== null && port_str.length > 0) {
  136. port = parseInt(port_str, 10);
  137. if (!/^[0-9]+$/.test(port_str) || port <= 0 || port > 65535) {
  138. return false;
  139. }
  140. } else if (options.require_port) {
  141. return false;
  142. }
  143. if (options.host_whitelist) {
  144. return checkHost(host, options.host_whitelist);
  145. }
  146. if (!(0, _isIP.default)(host) && !(0, _isFQDN.default)(host, options) && (!ipv6 || !(0, _isIP.default)(ipv6, 6))) {
  147. return false;
  148. }
  149. host = host || ipv6;
  150. if (options.host_blacklist && checkHost(host, options.host_blacklist)) {
  151. return false;
  152. }
  153. return true;
  154. }
  155. module.exports = exports.default;
  156. module.exports.default = exports.default;