Improve localhost detection

This commit is contained in:
Rūdolfs Ošiņš
2026-01-17 09:35:54 -06:00
parent 8f52b2bdfb
commit ebe4e426e8
2 changed files with 59 additions and 16 deletions
+13 -2
View File
@@ -204,8 +204,19 @@ export function isMarkdownPath(path: string): boolean {
}
export function isLocal(addr: string): boolean {
// Support *.localhost subdomains and 127.0.0.1.
return addr.endsWith("localhost") || addr.startsWith("127.0.0.1");
try {
const url = new URL(`scheme://${addr}`);
const hostname = url.hostname;
return (
hostname === "[::1]" ||
hostname === "127.0.0.1" ||
hostname === "localhost" ||
hostname.endsWith(".localhost")
);
} catch {
return false;
}
}
// Check whether the given domain name is an onion domain name.