isEmail.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. import assertString from './util/assertString';
  2. import merge from './util/merge';
  3. import isByteLength from './isByteLength';
  4. import isFQDN from './isFQDN';
  5. import isIP from './isIP';
  6. var default_email_options = {
  7. allow_display_name: false,
  8. require_display_name: false,
  9. allow_utf8_local_part: true,
  10. require_tld: true,
  11. blacklisted_chars: '',
  12. ignore_max_length: false,
  13. host_blacklist: [],
  14. host_whitelist: []
  15. };
  16. /* eslint-disable max-len */
  17. /* eslint-disable no-control-regex */
  18. var splitNameAddress = /^([^\x00-\x1F\x7F-\x9F\cX]+)</i;
  19. var emailUserPart = /^[a-z\d!#\$%&'\*\+\-\/=\?\^_`{\|}~]+$/i;
  20. var gmailUserPart = /^[a-z\d]+$/;
  21. var quotedEmailUser = /^([\s\x01-\x08\x0b\x0c\x0e-\x1f\x7f\x21\x23-\x5b\x5d-\x7e]|(\\[\x01-\x09\x0b\x0c\x0d-\x7f]))*$/i;
  22. var emailUserUtf8Part = /^[a-z\d!#\$%&'\*\+\-\/=\?\^_`{\|}~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+$/i;
  23. var quotedEmailUserUtf8 = /^([\s\x01-\x08\x0b\x0c\x0e-\x1f\x7f\x21\x23-\x5b\x5d-\x7e\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]|(\\[\x01-\x09\x0b\x0c\x0d-\x7f\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))*$/i;
  24. var defaultMaxEmailLength = 254;
  25. /* eslint-enable max-len */
  26. /* eslint-enable no-control-regex */
  27. /**
  28. * Validate display name according to the RFC2822: https://tools.ietf.org/html/rfc2822#appendix-A.1.2
  29. * @param {String} display_name
  30. */
  31. function validateDisplayName(display_name) {
  32. var display_name_without_quotes = display_name.replace(/^"(.+)"$/, '$1'); // display name with only spaces is not valid
  33. if (!display_name_without_quotes.trim()) {
  34. return false;
  35. } // check whether display name contains illegal character
  36. var contains_illegal = /[\.";<>]/.test(display_name_without_quotes);
  37. if (contains_illegal) {
  38. // if contains illegal characters,
  39. // must to be enclosed in double-quotes, otherwise it's not a valid display name
  40. if (display_name_without_quotes === display_name) {
  41. return false;
  42. } // the quotes in display name must start with character symbol \
  43. var all_start_with_back_slash = display_name_without_quotes.split('"').length === display_name_without_quotes.split('\\"').length;
  44. if (!all_start_with_back_slash) {
  45. return false;
  46. }
  47. }
  48. return true;
  49. }
  50. export default function isEmail(str, options) {
  51. assertString(str);
  52. options = merge(options, default_email_options);
  53. if (options.require_display_name || options.allow_display_name) {
  54. var display_email = str.match(splitNameAddress);
  55. if (display_email) {
  56. var display_name = display_email[1]; // Remove display name and angle brackets to get email address
  57. // Can be done in the regex but will introduce a ReDOS (See #1597 for more info)
  58. str = str.replace(display_name, '').replace(/(^<|>$)/g, ''); // sometimes need to trim the last space to get the display name
  59. // because there may be a space between display name and email address
  60. // eg. myname <address@gmail.com>
  61. // the display name is `myname` instead of `myname `, so need to trim the last space
  62. if (display_name.endsWith(' ')) {
  63. display_name = display_name.slice(0, -1);
  64. }
  65. if (!validateDisplayName(display_name)) {
  66. return false;
  67. }
  68. } else if (options.require_display_name) {
  69. return false;
  70. }
  71. }
  72. if (!options.ignore_max_length && str.length > defaultMaxEmailLength) {
  73. return false;
  74. }
  75. var parts = str.split('@');
  76. var domain = parts.pop();
  77. var lower_domain = domain.toLowerCase();
  78. if (options.host_blacklist.includes(lower_domain)) {
  79. return false;
  80. }
  81. if (options.host_whitelist.length > 0 && !options.host_whitelist.includes(lower_domain)) {
  82. return false;
  83. }
  84. var user = parts.join('@');
  85. if (options.domain_specific_validation && (lower_domain === 'gmail.com' || lower_domain === 'googlemail.com')) {
  86. /*
  87. Previously we removed dots for gmail addresses before validating.
  88. This was removed because it allows `multiple..dots@gmail.com`
  89. to be reported as valid, but it is not.
  90. Gmail only normalizes single dots, removing them from here is pointless,
  91. should be done in normalizeEmail
  92. */
  93. user = user.toLowerCase(); // Removing sub-address from username before gmail validation
  94. var username = user.split('+')[0]; // Dots are not included in gmail length restriction
  95. if (!isByteLength(username.replace(/\./g, ''), {
  96. min: 6,
  97. max: 30
  98. })) {
  99. return false;
  100. }
  101. var _user_parts = username.split('.');
  102. for (var i = 0; i < _user_parts.length; i++) {
  103. if (!gmailUserPart.test(_user_parts[i])) {
  104. return false;
  105. }
  106. }
  107. }
  108. if (options.ignore_max_length === false && (!isByteLength(user, {
  109. max: 64
  110. }) || !isByteLength(domain, {
  111. max: 254
  112. }))) {
  113. return false;
  114. }
  115. if (!isFQDN(domain, {
  116. require_tld: options.require_tld,
  117. ignore_max_length: options.ignore_max_length
  118. })) {
  119. if (!options.allow_ip_domain) {
  120. return false;
  121. }
  122. if (!isIP(domain)) {
  123. if (!domain.startsWith('[') || !domain.endsWith(']')) {
  124. return false;
  125. }
  126. var noBracketdomain = domain.slice(1, -1);
  127. if (noBracketdomain.length === 0 || !isIP(noBracketdomain)) {
  128. return false;
  129. }
  130. }
  131. }
  132. if (user[0] === '"') {
  133. user = user.slice(1, user.length - 1);
  134. return options.allow_utf8_local_part ? quotedEmailUserUtf8.test(user) : quotedEmailUser.test(user);
  135. }
  136. var pattern = options.allow_utf8_local_part ? emailUserUtf8Part : emailUserPart;
  137. var user_parts = user.split('.');
  138. for (var _i = 0; _i < user_parts.length; _i++) {
  139. if (!pattern.test(user_parts[_i])) {
  140. return false;
  141. }
  142. }
  143. if (options.blacklisted_chars) {
  144. if (user.search(new RegExp("[".concat(options.blacklisted_chars, "]+"), 'g')) !== -1) return false;
  145. }
  146. return true;
  147. }