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

fix(isEmail): reject quoted emails with blacklisted characters #2469

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/lib/isEmail.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,14 @@ export default function isEmail(str, options) {
}

if (options.blacklisted_chars) {
// Check for blacklisted characters in the raw user part
if (user.search(new RegExp(`[${options.blacklisted_chars}]+`, 'g')) !== -1) return false;

// If the user part is quoted, remove the quotes and recheck
if (user[0] === '"' && user[user.length - 1] === '"') {
const strippedUser = user.slice(1, user.length - 1);
if (strippedUser.search(new RegExp(`[${options.blacklisted_chars}]+`, 'g')) !== -1) return false;
}
}

if (user[0] === '"' && user[user.length - 1] === '"') {
Expand Down
17 changes: 17 additions & 0 deletions test/validators.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,23 @@ describe('Validators', () => {
});
});

it('should not validate email addresses with quotes in the local part', () => {
test({
validator: 'isEmail',
args: [{ blacklisted_chars: '"' }],
valid: [
'[email protected]',
'[email protected]',
],
invalid: [
'"foobar"@example.com',
'"foo"[email protected]',
'foo"bar"@example.com',
'" foo m端ller "@example.com',
'"foo\\@bar"@example.com',
],
});
});

it('should validate really long emails if ignore_max_length is set', () => {
test({
Expand Down
Loading