fix(web): share loopback hostname policy

This commit is contained in:
ZiyaZhang
2026-08-02 13:24:49 -07:00
parent 5ee544081a
commit 7a71ab9a92
5 changed files with 34 additions and 17 deletions

View File

@@ -0,0 +1,12 @@
/**
* Whether a normalized URL hostname names the local loopback authority.
* @param hostname - WHATWG URL hostname (IPv6 literals retain brackets).
* @returns true for localhost, IPv6 loopback, or any IPv4 address in 127/8.
*/
export function isLoopbackHostname(hostname: string): boolean {
if (hostname === 'localhost' || hostname === '[::1]') return true
const parts = hostname.split('.')
return parts.length === 4
&& parts[0] === '127'
&& parts.every(part => /^\d{1,3}$/.test(part) && Number(part) <= 255)
}