isEmail.js 5.7 KB

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