fix: More flexible link validation in editor allows custom protocols

closes #4319
This commit is contained in:
Tom Moor
2022-10-23 08:15:15 -04:00
parent 9af22017fe
commit 60f6a1f1c6
2 changed files with 29 additions and 11 deletions

View File

@@ -45,10 +45,11 @@ export function isInternalUrl(href: string) {
/**
* Returns true if the given string is a url.
*
* @param url The url to check.
* @param text The url to check.
* @param options Parsing options.
* @returns True if a url, false otherwise.
*/
export function isUrl(text: string) {
export function isUrl(text: string, options?: { requireHostname: boolean }) {
if (text.match(/\n/)) {
return false;
}
@@ -57,7 +58,18 @@ export function isUrl(text: string) {
const url = new URL(text);
const blockedProtocols = ["javascript:", "file:", "vbscript:", "data:"];
return url.hostname !== "" && !blockedProtocols.includes(url.protocol);
if (blockedProtocols.includes(url.protocol)) {
return false;
}
if (url.hostname) {
return true;
}
return (
url.protocol !== "" &&
url.pathname.startsWith("//") &&
!options?.requireHostname
);
} catch (err) {
return false;
}
@@ -86,7 +98,7 @@ export function sanitizeUrl(url: string | null | undefined) {
}
if (
!isUrl(url) &&
!isUrl(url, { requireHostname: false }) &&
!url.startsWith("/") &&
!url.startsWith("#") &&
!url.startsWith("mailto:") &&