isURL.js 9.9 KB

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