Help us shape the future of Go! Take the 2026 Go Developer Survey

egress

package
v0.14.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 22, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package egress 出口围栏:LLM 可控 URL 的字面量层 SSRF 防护——阻断指向 本机/私网/保留段的 IP 字面量与 localhost 域,放行普通域名。

对标 ZCode webfetch-egress-guard.ts:

  • 只做字面量判定,不对域名做 DNS preflight——部分网络 1s 内解析不完会 误杀公网目标(解析层防护由消费方按需叠加,如自定义 DialContext 的 Control 钩子二次校验解析结果);
  • IPv4-mapped IPv6(::ffff:0:0/96)与 NAT64 well-known prefix (64:ff9b::/96)先还原内嵌 IPv4 再套同一套 IPv4 策略——这是绕过 IP 围栏的经典逃逸路径;
  • 显式排除 special-use IPv6 段(ORCHID/discard/benchmark/文档段)—— 标准库 IsPrivate 等判定不覆盖这些段。

用法:

if err := egress.Check(u); err != nil { …拒绝抓取… }

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Check

func Check(rawURL string) error

Check 校验完整 URL 的 host(解析失败 fail-closed)。

func CheckHost

func CheckHost(host string) error

CheckHost 校验 host(URL hostname 或去端口/括号后的 host 字面量)。 空值 fail-closed;非 IP 字面量(域名)放行——见包注释的 preflight 取舍。

func CheckHostWith

func CheckHostWith(host string, o Options) error

CheckHostWith 校验 host(Options 放行开关仅作用于私网与回环/未指定两族; 链路本地(含云元数据)、组播、CGNAT 与保留段一律拒绝——放行面只该覆盖 「目标在内网是合法场景」的那部分)。

func CheckWith

func CheckWith(rawURL string, o Options) error

CheckWith 校验完整 URL 的 host(Options 语义见 Options;解析失败 fail-closed)。

Types

type BlockedError

type BlockedError struct {
	Host   string // 原始 host(URL Hostname 形态)
	Addr   string // 命中判定的地址(载体还原后的字面量;域名为空)
	Reason string // 命中原因(人可读)
}

BlockedError 围栏命中(结构化:调用方按需呈现原因与目标)。

func (*BlockedError) Error

func (e *BlockedError) Error() string

type Options

type Options struct {
	// AllowPrivate 放行私网段(IPv4 RFC1918 / IPv6 唯一本地)。
	AllowPrivate bool
	// AllowLoopback 放行回环与未指定地址(仅 127.0.0.0/8、::1、0.0.0.0、::
	// 四个字面量)。0.0.0.0/8 余下整段是 RFC 1122 的 "this network" 保留段,
	// 与 CGNAT/benchmark 同列——不在放行面内。
	AllowLoopback bool
}

Options IP 策略放行开关(缺省零值 = 全拒,与 Check/CheckHost 同语义)。 典型场景:运维平台探测内网服务/内网 IM 网关是能力本身(AllowPrivate), 探测本机自建服务同理(AllowLoopback)。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL