Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace email validator #214

Merged
merged 1 commit into from Jun 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
[Fix] validators.email: allowing comments, prevent catastrophic bac…
…ktracking
  • Loading branch information
thusoy authored and ljharb committed Apr 10, 2020
commit d4bd5b5febfe49c1f585f162e04ec810f8dc47a0
13 changes: 9 additions & 4 deletions lib/validators.js
Expand Up @@ -4,6 +4,7 @@ var util = require('util');
var is = require('is');
var trim = require('string.prototype.trim');
var every = require('array.prototype.every');
var emailValidator = require('email-addresses');

var format = function format(message) {
if (arguments.length < 2 || message.lastIndexOf('%s') >= 0) {
Expand Down Expand Up @@ -168,10 +169,14 @@ exports.color = function (message) {

exports.email = function (message) {
var msg = message || 'Please enter a valid email address.';
// regular expression by Scott Gonzalez:
// http://projects.scottsplayground.com/email_address_validation/
// eslint-disable-next-line no-control-regex, no-useless-escape
return exports.regexp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\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]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i, msg);
return function (form, field, callback) {
var object = emailValidator.parseOneAddress(field.data);
if (object) {
callback();
} else {
callback(msg);
}
};
};

exports.url = function (include_localhost, message) {
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -37,6 +37,7 @@
"array.prototype.every": "^1.1.0",
"array.prototype.some": "^1.1.1",
"async": "^2.6.3",
"email-addresses": "^3.1.0",
"formidable": "^1.2.2",
"is": "^3.3.0",
"object-keys": "^1.1.1",
Expand Down
6 changes: 3 additions & 3 deletions test/test-validators.js
Expand Up @@ -171,7 +171,7 @@ test('email', function (t) {
t.equal(err, undefined, 'a←+b@f.museum');
});
v('form', { data: 'asdf(with comments)@example.com' }, function (err) {
t.equal(err, 'Please enter a valid email address.', 'asdf(with comments)@example.com');
t.error(err, 'asdf(with comments)@example.com');
});
v('form', { data: '"quoted"@example.com' }, function (err) {
t.error(err, '"quoted"@example.com');
Expand All @@ -182,8 +182,8 @@ test('email', function (t) {
v('form', { data: '"quoted@at"@example.com' }, function (err) {
t.error(err, '"quoted@at"@example.com');
});
v('form', { data: '"aaaaaaaaaaaaaaaaaaaaaaaaa' }, function (err) {
t.equal(err, 'Please enter a valid email address.', '"aaaaaaaaaaaaaaaaaaaaaaaaa');
v('form', { data: '"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' }, function (err) {
t.equal(err, 'Please enter a valid email address.', 'lots of a’s');
});
t.end();
});
Expand Down