Skip to content

Commit

Permalink
feat: isEmail: validate display name using standards of RFC2822
Browse files Browse the repository at this point in the history
  • Loading branch information
Jin Huang committed Mar 19, 2019
1 parent c0b905e commit 42faca6
Show file tree
Hide file tree
Showing 5 changed files with 182 additions and 183 deletions.
61 changes: 56 additions & 5 deletions lib/isEmail.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ var _isIP = _interopRequireDefault(require("./isIP"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }

function _iterableToArrayLimit(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

var default_email_options = {
allow_display_name: false,
require_display_name: false,
Expand All @@ -27,7 +35,7 @@ var default_email_options = {

/* eslint-disable no-control-regex */

var displayName = /^[a-z\d!#\$%&'\*\+\-\/=\?\^_`{\|}~\.\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+[a-z\d!#\$%&'\*\+\-\/=\?\^_`{\|}~\,\.\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF\s]*<(.+)>$/i;
var splitNameAddress = /^([^\x00-\x1F\x7F-\x9F\cX]+)<(.+)>$/i;
var emailUserPart = /^[a-z\d!#\$%&'\*\+\-\/=\?\^_`{\|}~]+$/i;
var gmailUserPart = /^[a-z\d]+$/;
var quotedEmailUser = /^([\s\x01-\x08\x0b\x0c\x0e-\x1f\x7f\x21\x23-\x5b\x5d-\x7e]|(\\[\x01-\x09\x0b\x0c\x0d-\x7f]))*$/i;
Expand All @@ -37,15 +45,58 @@ var quotedEmailUserUtf8 = /^([\s\x01-\x08\x0b\x0c\x0e-\x1f\x7f\x21\x23-\x5b\x5d-

/* eslint-enable no-control-regex */

/**
* Validate display name according to the RFC2822: https://tools.ietf.org/html/rfc2822#appendix-A.1.2
* @param {String} display_name
*/

function validateDisplayName(display_name) {
var trim_quotes = display_name.match(/^"(.+)"$/i);
var display_name_without_quotes = trim_quotes ? trim_quotes[1] : display_name; // display name with only spaces is not valid

if (!display_name_without_quotes.trim()) {
return false;
} // check whether display name contains illegal character


var contains_illegal = /[\.";<>]/.test(display_name_without_quotes);

if (contains_illegal) {
// if contains illegal characters,
// must to be enclosed in double-quotes, otherwise it's not a valid display name
if (!trim_quotes) {
return false;
} // the quotes in display name must start with character symbol \


var all_start_with_back_slash = display_name_without_quotes.split('"').length === display_name_without_quotes.split('\\"').length;

if (!all_start_with_back_slash) {
return false;
}
}

return true;
}

function isEmail(str, options) {
(0, _assertString.default)(str);
options = (0, _merge.default)(options, default_email_options);

if (options.require_display_name || options.allow_display_name) {
var display_email = str.match(displayName);
var display_email = str.match(splitNameAddress);

if (display_email) {
str = display_email[1];
var display_name;

var _display_email = _slicedToArray(display_email, 3);

display_name = _display_email[1];
str = _display_email[2];

if (!validateDisplayName(display_name)) {
return false;
}
} else if (options.require_display_name) {
return false;
}
Expand Down Expand Up @@ -120,8 +171,8 @@ function isEmail(str, options) {
var pattern = options.allow_utf8_local_part ? emailUserUtf8Part : emailUserPart;
var user_parts = user.split('.');

for (var _i = 0; _i < user_parts.length; _i++) {
if (!pattern.test(user_parts[_i])) {
for (var _i2 = 0; _i2 < user_parts.length; _i2++) {
if (!pattern.test(user_parts[_i2])) {
return false;
}
}
Expand Down
45 changes: 42 additions & 3 deletions src/lib/isEmail.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const default_email_options = {

/* eslint-disable max-len */
/* eslint-disable no-control-regex */
const displayName = /^[a-z\d!#\$%&'\*\+\-\/=\?\^_`{\|}~\.\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+[a-z\d!#\$%&'\*\+\-\/=\?\^_`{\|}~\,\.\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF\s]*<(.+)>$/i;
const splitNameAddress = /^([^\x00-\x1F\x7F-\x9F\cX]+)<(.+)>$/i;
const emailUserPart = /^[a-z\d!#\$%&'\*\+\-\/=\?\^_`{\|}~]+$/i;
const gmailUserPart = /^[a-z\d]+$/;
const quotedEmailUser = /^([\s\x01-\x08\x0b\x0c\x0e-\x1f\x7f\x21\x23-\x5b\x5d-\x7e]|(\\[\x01-\x09\x0b\x0c\x0d-\x7f]))*$/i;
Expand All @@ -23,14 +23,53 @@ const quotedEmailUserUtf8 = /^([\s\x01-\x08\x0b\x0c\x0e-\x1f\x7f\x21\x23-\x5b\x5
/* eslint-enable max-len */
/* eslint-enable no-control-regex */

/**
* Validate display name according to the RFC2822: https://tools.ietf.org/html/rfc2822#appendix-A.1.2
* @param {String} display_name
*/
function validateDisplayName(display_name) {
const trim_quotes = display_name.match(/^"(.+)"$/i);
const display_name_without_quotes = trim_quotes ? trim_quotes[1] : display_name;

// display name with only spaces is not valid
if (!display_name_without_quotes.trim()) {
return false;
}

// check whether display name contains illegal character
const contains_illegal = /[\.";<>]/.test(display_name_without_quotes);
if (contains_illegal) {
// if contains illegal characters,
// must to be enclosed in double-quotes, otherwise it's not a valid display name
if (!trim_quotes) {
return false;
}

// the quotes in display name must start with character symbol \
const all_start_with_back_slash =
display_name_without_quotes.split('"').length === display_name_without_quotes.split('\\"').length;
if (!all_start_with_back_slash) {
return false;
}
}

return true;
}


export default function isEmail(str, options) {
assertString(str);
options = merge(options, default_email_options);

if (options.require_display_name || options.allow_display_name) {
const display_email = str.match(displayName);
const display_email = str.match(splitNameAddress);
if (display_email) {
str = display_email[1];
let display_name;
[, display_name, str] = display_email;

if (!validateDisplayName(display_name)) {
return false;
}
} else if (options.require_display_name) {
return false;
}
Expand Down
13 changes: 13 additions & 0 deletions test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,11 @@ describe('Validators', () => {
'Name <[email protected]>',
'Name<[email protected]>',
'Some Name <[email protected]>',
'Name🍓With🍑Emoji🚴‍♀️🏆<[email protected]>',
'🍇🍗🍑<[email protected]>',
'"<displayNameInBrackets>"<[email protected]>',
'"\\"quotes\\""<[email protected]>',
'"name;"<[email protected]>',
],
invalid: [
'invalidemail@',
Expand All @@ -195,6 +200,14 @@ describe('Validators', () => {
'Some Name < [email protected] >',
'Name [email protected]',
'Some Name <[email protected]>',
'Some Name<emoji_in_address🍈@aftership.com>',
'invisibleCharacter\u001F<[email protected]>',
'<displayNameInBrackets><[email protected]>',
'\\"quotes\\"<[email protected]>',
'""quotes""<[email protected]>',
'name;<[email protected]>',
' <[email protected]>',
'" "<[email protected]>',
],
});
});
Expand Down
Loading

0 comments on commit 42faca6

Please sign in to comment.