Documentation
¶
Overview ¶
Package urlguard 提供出站 SSRF 防护原语:URL 字面量校验 + 解析后 IP 校验。
两层防护缺一不可:
- IsBlockedURL / IsBlockedHost:拒绝 loopback、私网 IP、链路本地、 IPv4-mapped IPv6、.internal/.local 等保留域名——挡住低成本探测;
- GuardAddr / SafeDialer:拨号前解析并校验目标 IP——挡住 DNS rebinding。
典型消费方是「出站目标由用户自配」的服务(webhook / 回调 / 第三方源)。 测试需打本机 mock 时用 SetAllowPrivateForTest(true),进程级开关,用完复位。
Index ¶
- func GuardAddr(addr string) (string, error)
- func GuardAddrContext(ctx context.Context, addr string) (string, error)
- func IsBlockedHost(host string) error
- func IsBlockedIP(ip netip.Addr) error
- func IsBlockedURL(raw string) error
- func SafeDialer(next func(network, addr string) (net.Conn, error)) func(network, addr string) (net.Conn, error)
- func SetAllowPrivateForTest(enabled bool)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GuardAddr ¶
GuardAddr 解析 addr(host:port)并校验全部解析结果;任一 IP 落在受保护 网段即拒绝。返回校验后的第一个地址(ip:port),供下游拨号,缩小 DNS rebinding 窗口。测试放行时跳过解析,原样返回。
func GuardAddrContext ¶
GuardAddrContext 同 GuardAddr,但解析走可取消的 ctx。
func IsBlockedHost ¶
IsBlockedHost 校验主机名字面量:IP 直接过 IsBlockedIP; 域名检查保留后缀与 localhost。不解析 DNS。
func IsBlockedIP ¶
IsBlockedIP 判断 IP 是否落在受保护网段。IPv4-mapped IPv6 先还原再判。 测试放行开关打开时恒返回 nil。
func IsBlockedURL ¶
IsBlockedURL 校验出站 URL 字面量:scheme 必须 http/https,host 过 IsBlockedHost。不解析 DNS。
func SafeDialer ¶
func SafeDialer(next func(network, addr string) (net.Conn, error)) func(network, addr string) (net.Conn, error)
SafeDialer 包装拨号函数:拨号前 GuardAddr,用校验后的 IP:port 建连。 next 为 nil 时使用 net.Dial。
func SetAllowPrivateForTest ¶
func SetAllowPrivateForTest(enabled bool)
SetAllowPrivateForTest 放行私网/loopback(仅测试环境)。默认 false。
Types ¶
This section is empty.