原文
// services/preview/src/net.ts (new)
import ipaddr from 'ipaddr.js';
import dns from 'node:dns/promises';
const BLOCKED_RANGES = [
'0.0.0.0/8', '10.0.0.0/8', '127.0.0.0/8', '169.254.0.0/16',
'172.16.0.0/12', '192.168.0.0/16',
'::/128', '::1/128', 'fc00::/7', 'fe80::/10',
].map((cidr) => ipaddr.parseCIDR(cidr));
export async function assertSafeTarget(hostname: string): Promise<string> {
const { address } = await dns.lookup(hostname);
const parsed = ipaddr.parse(address);
if (BLOCKED_RANGES.some((range) => parsed.match(range))) {
throw new UnsafeTargetError(hostname, address);
}
return address;
}