isURL.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 _checkHost = _interopRequireDefault(require("./util/checkHost"));
  8. var _includesString = _interopRequireDefault(require("./util/includesString"));
  9. var _isFQDN = _interopRequireDefault(require("./isFQDN"));
  10. var _isIP = _interopRequireDefault(require("./isIP"));
  11. var _merge = _interopRequireDefault(require("./util/merge"));
  12. function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
  13. function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
  14. 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."); }
  15. function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
  16. function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
  17. function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
  18. function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
  19. /*
  20. options for isURL method
  21. protocols - valid protocols can be modified with this option.
  22. require_tld - If set to false isURL will not check if the URL's host includes a top-level domain.
  23. require_protocol - if set to true isURL will return false if protocol is not present in the URL.
  24. require_host - if set to false isURL will not check if host is present in the URL.
  25. require_port - if set to true isURL will check if port is present in the URL.
  26. require_valid_protocol - isURL will check if the URL's protocol is present in the protocols option.
  27. allow_underscores - if set to true, the validator will allow underscores in the URL.
  28. host_whitelist - if set to an array of strings or regexp, and the domain matches none of the strings
  29. defined in it, the validation fails.
  30. host_blacklist - if set to an array of strings or regexp, and the domain matches any of the strings
  31. defined in it, the validation fails.
  32. allow_trailing_dot - if set to true, the validator will allow the domain to end with
  33. a `.` character.
  34. allow_protocol_relative_urls - if set to true protocol relative URLs will be allowed.
  35. allow_fragments - if set to false isURL will return false if fragments are present.
  36. allow_query_components - if set to false isURL will return false if query components are present.
  37. disallow_auth - if set to true, the validator will fail if the URL contains an authentication
  38. component, e.g. `http://username:password@example.com`
  39. validate_length - if set to false isURL will skip string length validation. `max_allowed_length`
  40. will be ignored if this is set as `false`.
  41. max_allowed_length - if set, isURL will not allow URLs longer than the specified value (default is
  42. 2084 that IE maximum URL length).
  43. */
  44. var default_url_options = {
  45. protocols: ['http', 'https', 'ftp'],
  46. require_tld: true,
  47. require_protocol: false,
  48. require_host: true,
  49. require_port: false,
  50. require_valid_protocol: true,
  51. allow_underscores: false,
  52. allow_trailing_dot: false,
  53. allow_protocol_relative_urls: false,
  54. allow_fragments: true,
  55. allow_query_components: true,
  56. validate_length: true,
  57. max_allowed_length: 2084
  58. };
  59. var wrapped_ipv6 = /^\[([^\]]+)\](?::([0-9]+))?$/;
  60. function isURL(url, options) {
  61. (0, _assertString.default)(url);
  62. if (!url || /[\s<>]/.test(url)) {
  63. return false;
  64. }
  65. if (url.indexOf('mailto:') === 0) {
  66. return false;
  67. }
  68. options = (0, _merge.default)(options, default_url_options);
  69. if (options.validate_length && url.length > options.max_allowed_length) {
  70. return false;
  71. }
  72. if (!options.allow_fragments && (0, _includesString.default)(url, '#')) {
  73. return false;
  74. }
  75. if (!options.allow_query_components && ((0, _includesString.default)(url, '?') || (0, _includesString.default)(url, '&'))) {
  76. return false;
  77. }
  78. var protocol, auth, host, hostname, port, port_str, split, ipv6;
  79. split = url.split('#');
  80. url = split.shift();
  81. split = url.split('?');
  82. url = split.shift();
  83. // Replaced the 'split("://")' logic with a regex to match the protocol.
  84. // This correctly identifies schemes like `javascript:` which don't use `//`.
  85. // However, we need to be careful not to confuse authentication credentials (user:password@host)
  86. // with protocols. A colon before an @ symbol might be part of auth, not a protocol separator.
  87. var protocol_match = url.match(/^([a-z][a-z0-9+\-.]*):/i);
  88. var had_explicit_protocol = false;
  89. var cleanUpProtocol = function cleanUpProtocol(potential_protocol) {
  90. had_explicit_protocol = true;
  91. protocol = potential_protocol.toLowerCase();
  92. if (options.require_valid_protocol && options.protocols.indexOf(protocol) === -1) {
  93. // The identified protocol is not in the allowed list.
  94. return false;
  95. }
  96. // Remove the protocol from the URL string.
  97. return url.substring(protocol_match[0].length);
  98. };
  99. if (protocol_match) {
  100. var potential_protocol = protocol_match[1];
  101. var after_colon = url.substring(protocol_match[0].length);
  102. // Check if what follows looks like authentication credentials (user:password@host)
  103. // rather than a protocol. This happens when:
  104. // 1. There's no `//` after the colon (protocols like `http://` have this)
  105. // 2. There's an `@` symbol before any `/`
  106. // 3. The part before `@` contains only valid auth characters (alphanumeric, -, _, ., %, :)
  107. var starts_with_slashes = after_colon.slice(0, 2) === '//';
  108. if (!starts_with_slashes) {
  109. var first_slash_position = after_colon.indexOf('/');
  110. var before_slash = first_slash_position === -1 ? after_colon : after_colon.substring(0, first_slash_position);
  111. var at_position = before_slash.indexOf('@');
  112. if (at_position !== -1) {
  113. var before_at = before_slash.substring(0, at_position);
  114. var valid_auth_regex = /^[a-zA-Z0-9\-_.%:]*$/;
  115. var is_valid_auth = valid_auth_regex.test(before_at);
  116. if (is_valid_auth) {
  117. // This looks like authentication (e.g., user:password@host), not a protocol
  118. if (options.require_protocol) {
  119. return false;
  120. }
  121. // Don't consume the colon; let the auth parsing handle it later
  122. } else {
  123. // This looks like a malicious protocol (e.g., javascript:alert();@host)
  124. url = cleanUpProtocol(potential_protocol);
  125. if (url === false) {
  126. return false;
  127. }
  128. }
  129. } else {
  130. // No @ symbol found. Check if this could be a port number instead of a protocol.
  131. // If what's after the colon is numeric (or starts with a digit and contains only
  132. // valid port characters until a path separator), it's likely hostname:port, not a protocol.
  133. var looks_like_port = /^[0-9]/.test(after_colon);
  134. if (looks_like_port) {
  135. // This looks like hostname:port, not a protocol
  136. if (options.require_protocol) {
  137. return false;
  138. }
  139. // Don't consume anything; let it be parsed as hostname:port
  140. } else {
  141. // This is definitely a protocol
  142. url = cleanUpProtocol(potential_protocol);
  143. if (url === false) {
  144. return false;
  145. }
  146. }
  147. }
  148. } else {
  149. // Starts with '//', this is definitely a protocol like http://
  150. url = cleanUpProtocol(potential_protocol);
  151. if (url === false) {
  152. return false;
  153. }
  154. }
  155. } else if (options.require_protocol) {
  156. return false;
  157. }
  158. // Handle leading '//' only as protocol-relative when there was NO explicit protocol.
  159. // If there was an explicit protocol, '//' is the normal separator
  160. // and should be stripped unconditionally.
  161. if (url.slice(0, 2) === '//') {
  162. if (!had_explicit_protocol && !options.allow_protocol_relative_urls) {
  163. return false;
  164. }
  165. url = url.slice(2);
  166. }
  167. if (url === '') {
  168. return false;
  169. }
  170. split = url.split('/');
  171. url = split.shift();
  172. if (url === '' && !options.require_host) {
  173. return true;
  174. }
  175. split = url.split('@');
  176. if (split.length > 1) {
  177. if (options.disallow_auth) {
  178. return false;
  179. }
  180. if (split[0] === '') {
  181. return false;
  182. }
  183. auth = split.shift();
  184. if (auth.indexOf(':') >= 0 && auth.split(':').length > 2) {
  185. return false;
  186. }
  187. var _auth$split = auth.split(':'),
  188. _auth$split2 = _slicedToArray(_auth$split, 2),
  189. user = _auth$split2[0],
  190. password = _auth$split2[1];
  191. if (user === '' && password === '') {
  192. return false;
  193. }
  194. }
  195. hostname = split.join('@');
  196. port_str = null;
  197. ipv6 = null;
  198. var ipv6_match = hostname.match(wrapped_ipv6);
  199. if (ipv6_match) {
  200. host = '';
  201. ipv6 = ipv6_match[1];
  202. port_str = ipv6_match[2] || null;
  203. } else {
  204. split = hostname.split(':');
  205. host = split.shift();
  206. if (split.length) {
  207. port_str = split.join(':');
  208. }
  209. }
  210. if (port_str !== null && port_str.length > 0) {
  211. port = parseInt(port_str, 10);
  212. if (!/^[0-9]+$/.test(port_str) || port <= 0 || port > 65535) {
  213. return false;
  214. }
  215. } else if (options.require_port) {
  216. return false;
  217. }
  218. if (options.host_whitelist) {
  219. return (0, _checkHost.default)(host, options.host_whitelist);
  220. }
  221. if (host === '' && !options.require_host) {
  222. return true;
  223. }
  224. if (!(0, _isIP.default)(host) && !(0, _isFQDN.default)(host, options) && (!ipv6 || !(0, _isIP.default)(ipv6, 6))) {
  225. return false;
  226. }
  227. host = host || ipv6;
  228. if (options.host_blacklist && (0, _checkHost.default)(host, options.host_blacklist)) {
  229. return false;
  230. }
  231. return true;
  232. }
  233. module.exports = exports.default;
  234. module.exports.default = exports.default;