Skip to content

Commit

Permalink
security: fix catastrophic backtracking
Browse files Browse the repository at this point in the history
Problem:
The regex used to validate the user portion was vulnerable
to catastrophic backtracking.

This made valid-email vulnerable to a weak REDOS attack.
Each malicious input blocks the event loop for about 0.1 seconds.

Solution:
I tweaked the behavior of the regex pattern.

It now accepts double-quote and space ('"' and ' ', respectively)
characters anywhere in the user portion.

It used to accept a broader range of characters provided they were escaped.

To retain the original language, I suspect a custom parser would be necessary.
Seems like overkill for this module.
  • Loading branch information
davisjam authored and johnhenry committed Feb 26, 2018
1 parent 7f30ae7 commit 624ffa6
Showing 1 changed file with 3 additions and 6 deletions.
9 changes: 3 additions & 6 deletions lib/valid-email.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -51,12 +51,9 @@ module.exports = function valid(email) {
if (domain.match(/\\.\\./)) { if (domain.match(/\\.\\./)) {
return false; // domain part has two consecutive dots return false; // domain part has two consecutive dots
} }
if ( if (!user.match(/^[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.\-" ]+$/)) {
!user return false; // user part has invalid characters
.replace("\\\\", "")
.match(/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/)
) {
if (!user.replace("\\\\", "").match(/^"(\\\\"|[^"])+"$/)) return false;
} }

return true; return true;
}; };

0 comments on commit 624ffa6

Please sign in to comment.