Merge pull request #548 from codimd/fix/545-urls-with-credentials

Use URL constructor instead of regex to check for valid URL
This commit is contained in:
David Mehren 2020-11-10 23:01:58 +01:00 committed by GitHub
commit 62fd5c894d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -178,16 +178,11 @@ function slugifyWithUTF8 (text) {
}
export function isValidURL (str) {
const pattern = new RegExp('^(https?:\\/\\/)?' + // protocol
'((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|' + // domain name
'((\\d{1,3}\\.){3}\\d{1,3}))' + // OR ip (v4) address
'(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*' + // port and path
'(\\?[;&a-z\\d%_.~+=-]*)?' + // query string
'(\\#[-a-z\\d_]*)?$', 'i') // fragment locator
if (!pattern.test(str)) {
try {
const url = new URL(str)
return ['http:', 'https:'].includes(url.protocol)
} catch (e) {
return false
} else {
return true
}
}