123456789101112131415// Zero-dependency private subnet and loopback isolation
function isSafeUrl(urlString: string): boolean {
const url = new URL(urlString);
const hostname = url.hostname.toLowerCase();
if (['localhost', '127.0.0.1', '[::1]', '0.0.0.0'].includes(hostname)) return false;
const match = hostname.match(/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/);
if (match) {
const [, o1, o2] = match.map(Number);
if (o1 === 10 || (o1 === 172 && o2 >= 16 && o2 <= 31) || (o1 === 192 && o2 === 168) || (o1 === 169 && o2 === 254)) {
return false; // Blocks RFC 1918 and AWS link-local metadata
}
}
return !hostname.startsWith('[fc') && !hostname.startsWith('[fd') && !hostname.startsWith('[fe');
}