isEmail.js 6.4 KB

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