isEmail.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = isEmail;
  6. var _assertString = _interopRequireDefault(require("./util/assertString"));
  7. var _merge = _interopRequireDefault(require("./util/merge"));
  8. var _isByteLength = _interopRequireDefault(require("./isByteLength"));
  9. var _isFQDN = _interopRequireDefault(require("./isFQDN"));
  10. var _isIP = _interopRequireDefault(require("./isIP"));
  11. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  12. var default_email_options = {
  13. allow_display_name: false,
  14. require_display_name: false,
  15. allow_utf8_local_part: true,
  16. require_tld: true,
  17. blacklisted_chars: '',
  18. ignore_max_length: false,
  19. host_blacklist: [],
  20. host_whitelist: []
  21. };
  22. /* eslint-disable max-len */
  23. /* eslint-disable no-control-regex */
  24. var splitNameAddress = /^([^\x00-\x1F\x7F-\x9F\cX]+)</i;
  25. var emailUserPart = /^[a-z\d!#\$%&'\*\+\-\/=\?\^_`{\|}~]+$/i;
  26. var gmailUserPart = /^[a-z\d]+$/;
  27. var quotedEmailUser = /^([\s\x01-\x08\x0b\x0c\x0e-\x1f\x7f\x21\x23-\x5b\x5d-\x7e]|(\\[\x01-\x09\x0b\x0c\x0d-\x7f]))*$/i;
  28. var emailUserUtf8Part = /^[a-z\d!#\$%&'\*\+\-\/=\?\^_`{\|}~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+$/i;
  29. 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;
  30. var defaultMaxEmailLength = 254;
  31. /* eslint-enable max-len */
  32. /* eslint-enable no-control-regex */
  33. /**
  34. * Validate display name according to the RFC2822: https://tools.ietf.org/html/rfc2822#appendix-A.1.2
  35. * @param {String} display_name
  36. */
  37. function validateDisplayName(display_name) {
  38. var display_name_without_quotes = display_name.replace(/^"(.+)"$/, '$1'); // display name with only spaces is not valid
  39. if (!display_name_without_quotes.trim()) {
  40. return false;
  41. } // check whether display name contains illegal character
  42. var contains_illegal = /[\.";<>]/.test(display_name_without_quotes);
  43. if (contains_illegal) {
  44. // if contains illegal characters,
  45. // must to be enclosed in double-quotes, otherwise it's not a valid display name
  46. if (display_name_without_quotes === display_name) {
  47. return false;
  48. } // the quotes in display name must start with character symbol \
  49. var all_start_with_back_slash = display_name_without_quotes.split('"').length === display_name_without_quotes.split('\\"').length;
  50. if (!all_start_with_back_slash) {
  51. return false;
  52. }
  53. }
  54. return true;
  55. }
  56. function isEmail(str, options) {
  57. (0, _assertString.default)(str);
  58. options = (0, _merge.default)(options, default_email_options);
  59. if (options.require_display_name || options.allow_display_name) {
  60. var display_email = str.match(splitNameAddress);
  61. if (display_email) {
  62. var display_name = display_email[1]; // 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, ''); // 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(); // Removing sub-address from username before gmail validation
  100. var username = user.split('+')[0]; // Dots are not included in gmail length restriction
  101. if (!(0, _isByteLength.default)(username.replace(/\./g, ''), {
  102. min: 6,
  103. max: 30
  104. })) {
  105. return false;
  106. }
  107. var _user_parts = username.split('.');
  108. for (var i = 0; i < _user_parts.length; i++) {
  109. if (!gmailUserPart.test(_user_parts[i])) {
  110. return false;
  111. }
  112. }
  113. }
  114. if (options.ignore_max_length === false && (!(0, _isByteLength.default)(user, {
  115. max: 64
  116. }) || !(0, _isByteLength.default)(domain, {
  117. max: 254
  118. }))) {
  119. return false;
  120. }
  121. if (!(0, _isFQDN.default)(domain, {
  122. require_tld: options.require_tld,
  123. ignore_max_length: options.ignore_max_length
  124. })) {
  125. if (!options.allow_ip_domain) {
  126. return false;
  127. }
  128. if (!(0, _isIP.default)(domain)) {
  129. if (!domain.startsWith('[') || !domain.endsWith(']')) {
  130. return false;
  131. }
  132. var noBracketdomain = domain.slice(1, -1);
  133. if (noBracketdomain.length === 0 || !(0, _isIP.default)(noBracketdomain)) {
  134. return false;
  135. }
  136. }
  137. }
  138. if (user[0] === '"') {
  139. user = user.slice(1, user.length - 1);
  140. return options.allow_utf8_local_part ? quotedEmailUserUtf8.test(user) : quotedEmailUser.test(user);
  141. }
  142. var pattern = options.allow_utf8_local_part ? emailUserUtf8Part : emailUserPart;
  143. var user_parts = user.split('.');
  144. for (var _i = 0; _i < user_parts.length; _i++) {
  145. if (!pattern.test(user_parts[_i])) {
  146. return false;
  147. }
  148. }
  149. if (options.blacklisted_chars) {
  150. if (user.search(new RegExp("[".concat(options.blacklisted_chars, "]+"), 'g')) !== -1) return false;
  151. }
  152. return true;
  153. }
  154. module.exports = exports.default;
  155. module.exports.default = exports.default;