Skip to content

Fix: isURL incorrectly accepts email-like strings (#1674) #2557

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

Open
wants to merge 1 commit 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
13 changes: 13 additions & 0 deletions src/lib/isURL.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,19 @@
if (url.indexOf('mailto:') === 0) {
return false;
}

const isURLHasProtocol = /^([a-z0-9.+-]+:)?\/\//i.test(url);

if (!isURLHasProtocol) {
const emailLikeRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;

if (!options || options.disallow_auth !== false) {
if (emailLikeRegex.test(url)) {

Check failure

Code scanning / CodeQL

Polynomial regular expression used on uncontrolled data High

This
regular expression
that depends on
library input
may run slow on strings starting with '!@!.' and with many repetitions of '!.'.
return false;
}
}
}

options = merge(options, default_url_options);

if (options.validate_length && url.length > options.max_allowed_length) {
Expand Down
15 changes: 15 additions & 0 deletions test/validators.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,21 @@
],
});
});

Check failure on line 472 in test/validators.test.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 22

Trailing spaces not allowed

Check failure on line 472 in test/validators.test.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 16

Trailing spaces not allowed

Check failure on line 472 in test/validators.test.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 18

Trailing spaces not allowed

Check failure on line 472 in test/validators.test.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 20

Trailing spaces not allowed

Check failure on line 472 in test/validators.test.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 12

Trailing spaces not allowed

Check failure on line 472 in test/validators.test.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 8

Trailing spaces not allowed

Check failure on line 472 in test/validators.test.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 10

Trailing spaces not allowed

Check failure on line 472 in test/validators.test.js

View workflow job for this annotation

GitHub Actions / Run tests on Node.js 14

Trailing spaces not allowed
it('should not validate email-like strings as URLs', () => {
test({
validator: 'isURL',
valid: [],
invalid: [
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
],
});
});

it('should validate URLs with custom protocols', () => {
test({
Expand Down
Loading