背景与价值
IDN可能引入同形异义攻击。采用ASCII策略与Punycode检测可降低风险并提升治理效率。
统一规范
- ASCII策略:默认仅接受ASCII域名(`A-Za-z0-9.-`)。
- Punycode检测:标识 `xn--` 前缀的域名并仅白名单允许。
- 来源校验:对来源域进行严格匹配与审计。
核心实现
域名校验
const allowHosts = new Set(['app.example.com','api.example.com'])
function asciiHost(h: string): boolean { return /^[A-Za-z0-9.-]+$/.test(h) }
function isPunycode(h: string): boolean { return h.toLowerCase().includes('xn--') }
function hostAllowed(h: string): boolean {
const host = h.toLowerCase()
if (!asciiHost(host)) return false
if (isPunycode(host) && !allowHosts.has(host)) return false
return allowHosts.has(host)
}
来源校验
function originAllowed(o: string | undefined): boolean { if (!o) return false; try { const u = new URL(o); return hostAllowed(u.hostname) } catch { return false } }
落地建议
- 默认仅接受ASCII域名来源;对含 `xn--` 的Punycode域名实行白名单策略。
- 对来源进行严格校验与审计,发现疑似同形域名及时加入屏蔽清单。
验证清单
- 来源域名是否为ASCII或白名单Punycode;是否严格匹配允许集合。

发表评论 取消回复