isEmail.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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');
  34. // display name with only spaces is not valid
  35. if (!display_name_without_quotes.trim()) {
  36. return false;
  37. }
  38. // check whether display name contains illegal character
  39. var contains_illegal = /[\.";<>]/.test(display_name_without_quotes);
  40. if (contains_illegal) {
  41. // if contains illegal characters,
  42. // must to be enclosed in double-quotes, otherwise it's not a valid display name
  43. if (display_name_without_quotes === display_name) {
  44. return false;
  45. }
  46. // the quotes in display name must start with character symbol \
  47. var all_start_with_back_slash = display_name_without_quotes.split('"').length === display_name_without_quotes.split('\\"').length;
  48. if (!all_start_with_back_slash) {
  49. return false;
  50. }
  51. }
  52. return true;
  53. }
  54. export default function isEmail(str, options) {
  55. assertString(str);
  56. options = merge(options, default_email_options);
  57. if (options.require_display_name || options.allow_display_name) {
  58. var display_email = str.match(splitNameAddress);
  59. if (display_email) {
  60. var display_name = display_email[1];
  61. // Remove display name and angle brackets to get email address
  62. // Can be done in the regex but will introduce a ReDOS (See #1597 for more info)
  63. str = str.replace(display_name, '').replace(/(^<|>$)/g, '');
  64. // sometimes need to trim the last space to get the display name
  65. // because there may be a space between display name and email address
  66. // eg. myname <address@gmail.com>
  67. // the display name is `myname` instead of `myname `, so need to trim the last space
  68. if (display_name.endsWith(' ')) {
  69. display_name = display_name.slice(0, -1);
  70. }
  71. if (!validateDisplayName(display_name)) {
  72. return false;
  73. }
  74. } else if (options.require_display_name) {
  75. return false;
  76. }
  77. }
  78. if (!options.ignore_max_length && str.length > defaultMaxEmailLength) {
  79. return false;
  80. }
  81. var parts = str.split('@');
  82. var domain = parts.pop();
  83. var lower_domain = domain.toLowerCase();
  84. if (options.host_blacklist.includes(lower_domain)) {
  85. return false;
  86. }
  87. if (options.host_whitelist.length > 0 && !options.host_whitelist.includes(lower_domain)) {
  88. return false;
  89. }
  90. var user = parts.join('@');
  91. if (options.domain_specific_validation && (lower_domain === 'gmail.com' || lower_domain === 'googlemail.com')) {
  92. /*
  93. Previously we removed dots for gmail addresses before validating.
  94. This was removed because it allows `multiple..dots@gmail.com`
  95. to be reported as valid, but it is not.
  96. Gmail only normalizes single dots, removing them from here is pointless,
  97. should be done in normalizeEmail
  98. */
  99. user = user.toLowerCase();
  100. // Removing sub-address from username before gmail validation
  101. var username = user.split('+')[0];
  102. // Dots are not included in gmail length restriction
  103. if (!isByteLength(username.replace(/\./g, ''), {
  104. min: 6,
  105. max: 30
  106. })) {
  107. return false;
  108. }
  109. var _user_parts = username.split('.');
  110. for (var i = 0; i < _user_parts.length; i++) {
  111. if (!gmailUserPart.test(_user_parts[i])) {
  112. return false;
  113. }
  114. }
  115. }
  116. if (options.ignore_max_length === false && (!isByteLength(user, {
  117. max: 64
  118. }) || !isByteLength(domain, {
  119. max: 254
  120. }))) {
  121. return false;
  122. }
  123. if (!isFQDN(domain, {
  124. require_tld: options.require_tld,
  125. ignore_max_length: options.ignore_max_length,
  126. allow_underscores: options.allow_underscores
  127. })) {
  128. if (!options.allow_ip_domain) {
  129. return false;
  130. }
  131. if (!isIP(domain)) {
  132. if (!domain.startsWith('[') || !domain.endsWith(']')) {
  133. return false;
  134. }
  135. var noBracketdomain = domain.slice(1, -1);
  136. if (noBracketdomain.length === 0 || !isIP(noBracketdomain)) {
  137. return false;
  138. }
  139. }
  140. }
  141. if (user[0] === '"') {
  142. user = user.slice(1, user.length - 1);
  143. return options.allow_utf8_local_part ? quotedEmailUserUtf8.test(user) : quotedEmailUser.test(user);
  144. }
  145. var pattern = options.allow_utf8_local_part ? emailUserUtf8Part : emailUserPart;
  146. var user_parts = user.split('.');
  147. for (var _i = 0; _i < user_parts.length; _i++) {
  148. if (!pattern.test(user_parts[_i])) {
  149. return false;
  150. }
  151. }
  152. if (options.blacklisted_chars) {
  153. if (user.search(new RegExp("[".concat(options.blacklisted_chars, "]+"), 'g')) !== -1) return false;
  154. }
  155. return true;
  156. }