Skip to content

Commit

Permalink
improved URL validation (specifically spaces)
Browse files Browse the repository at this point in the history
  • Loading branch information
HowardBraham committed Aug 3, 2023
1 parent 2ca3978 commit 2c264d6
Showing 1 changed file with 26 additions and 25 deletions.
51 changes: 26 additions & 25 deletions app/scripts/lib/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,47 +240,48 @@ export function addUrlProtocolPrefix(urlString: string) {
trimmed = `https://${trimmed}`;
}

if (isValidUrl(trimmed)) {
if (getValidUrl(trimmed) !== null) {
return trimmed;
}

return null;
}

export function isLocalhostOrHttps(urlString: string) {
export function getValidUrl(urlString: string): URL | null {
try {
const url = new URL(urlString);
return (
url.hostname === 'localhost' ||
url.hostname === '127.0.0.1' ||
url.protocol === 'https:'
);

if (url.hostname.length === 0 || url.pathname.length === 0) {
return null;
}

if (url.hostname != decodeURIComponent(url.hostname)) {
return null; // will happen if there's a %, a space, or other invalid character in the hostname
}

return url;
} catch (error) {
return false;
return null;
}
}

export function isValidUrl(urlString: string): boolean {
try {
const url = new URL(urlString);
export function isLocalhostOrHttps(urlString: string) {
const url = getValidUrl(urlString);

return url.hostname.length > 0 && url.pathname.length > 0;
} catch (error) {
return false;
}
return (
url !== null &&
(url.hostname === 'localhost' ||
url.hostname === '127.0.0.1' ||
url.protocol === 'https:')
);
}

export function isWebUrl(urlString: string): boolean {
try {
const url = new URL(urlString);
return (
url.hostname.length > 0 &&
(url.protocol === 'https:' || url.protocol === 'http:') &&
url.pathname.length > 0
);
} catch (error) {
return false;
}
const url = getValidUrl(urlString);

return (
url !== null && (url.protocol === 'https:' || url.protocol === 'http:')
);
}

interface FormattedTransactionMeta {
Expand Down

0 comments on commit 2c264d6

Please sign in to comment.