-
Notifications
You must be signed in to change notification settings - Fork 934
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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,}))\\.?'; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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}`; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this regex will pass with string
Suggested change
|
||||||
|
||||||
return new RegExp(regex, 'ig'); | ||||||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
]); | ||
}); | ||
|
||
|
There was a problem hiding this comment.
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()
?