isEmail.js 5.8 KB

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