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

feat: Added better url validation that supports wider range of urls #1859

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: 2 additions & 5 deletions src/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,13 @@ import type {
Optionals,
} from './util/types';
import Schema from './schema';
import { getUrlRegex } from './util/getUrlRegex';

// Taken from HTML spec: https://html.spec.whatwg.org/multipage/input.html#valid-e-mail-address
let rEmail =
// eslint-disable-next-line
/^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;

let rUrl =
// eslint-disable-next-line
/^((https?|ftp):)?\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([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])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i;

// eslint-disable-next-line
let rUUID =
/^(?:[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}|00000000-0000-0000-0000-000000000000)$/i;
Expand Down Expand Up @@ -186,7 +183,7 @@ export default class StringSchema<
}

url(message = locale.url) {
return this.matches(rUrl, {
return this.matches(getUrlRegex(), {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would you create same RegExp every time? Isn't it better to have const rUrl = getUrlRegExp()?

name: 'url',
message,
excludeEmptyString: true,
Expand Down
18 changes: 18 additions & 0 deletions src/util/getUrlRegex.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export const getUrlRegex = () => {
const protocol = '(?:(?:[a-z]+:)?//)';
const auth = '(?:\\S+(?::\\S*)?@)?';
const ipv4 =
'((?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])[.]){3}(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])';
const host = '(?:(?:[a-z\\u00a1-\\uffff0-9][-_]*)*[a-z\\u00a1-\\uffff0-9]+)';
const ipv6 =
'\\[(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]).){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]).){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))\\]';
const domain =
'(?:\\.(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)*';
const tld = '(?:\\.(?:[a-z\\u00a1-\\uffff]{2,}))\\.?';

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this TLD regex does not allow becoded names such as .xn--90ae which stands for internationalized .бг for Bulgaria.

const port = '(?::\\d{2,5})?';
const path = '(?:[/?#][^\\s"]*)?';

const regex = `(?:${protocol}|www\\.)${auth}(?:localhost|${ipv4}|${ipv6}|${host}${domain}${tld})${port}${path}`;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this regex will pass with string hi, i'm obviously not a good string www.uwu.www and something other. Which was not the case for previous version.

Suggested change
const regex = `(?:${protocol}|www\\.)${auth}(?:localhost|${ipv4}|${ipv6}|${host}${domain}${tld})${port}${path}`;
const regex = `^(?:${protocol}|www\\.)${auth}(?:localhost|${ipv4}|${ipv6}|${host}${domain}${tld})${port}${path}$`;


return new RegExp(regex, 'ig');
};
9 changes: 9 additions & 0 deletions test/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,16 @@ describe('String types', () => {
return Promise.all([
expect(v.isValid('//www.github.com/')).resolves.toBe(true),
expect(v.isValid('https://www.github.com/')).resolves.toBe(true),
expect(v.isValid('http://localhost:8000/')).resolves.toBe(true),
expect(v.isValid('http://128.0.0.1:8000')).resolves.toBe(true),
expect(v.isValid('http://128.0.0.1')).resolves.toBe(true),
expect(v.isValid('ftp://128.0.0.1')).resolves.toBe(true),
expect(v.isValid('http://0.0.0.0')).resolves.toBe(true),
expect(v.isValid('http://[2001:0db8:85a3:0000:0000:8a2e:0370:7334]')).resolves.toBe(true),
expect(v.isValid('http://[::255.255.255.255]')).resolves.toBe(true),
expect(v.isValid('http://[56FE::2159:5BBC::6594]')).resolves.toBe(false),
expect(v.isValid('this is not a url')).resolves.toBe(false),
expect(v.isValid('128.0.0.1')).resolves.toBe(false),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, you've implemented support for IRI but there's no test case for that. I guess some tlds from this article might help or this one

]);
});

Expand Down