isEmail.js 6.2 KB

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