An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:lock: Fix SSRF bypass via IPv6 transition addresses in CheckHostSSRF https://github.com/siyuan-note/siyuan/security/advisories/GHSA-rg26-cg95-gq6p
Daniela8d2cc4e4952
diff --git a/kernel/util/httprequest.go b/kernel/util/httprequest.goindex ddff0135b0..961fb76557 100644--- a/kernel/util/httprequest.go+++ b/kernel/util/httprequest.go@@ -38,13 +38,15 @@ const (// CheckHostSSRF 校验主机名解析出的 IP 不落在内网/回环等不可达地址段,// 防止智能体被诱导发起 SSRF 攻击。web_fetch 与 http_request 共用此校验。+// https://github.com/siyuan-note/siyuan/security/advisories/GHSA-rg26-cg95-gq6pfunc CheckHostSSRF(host string) error {ips, err := net.LookupIP(host)if err != nil {return errors.New("failed to resolve host: " + err.Error())}for _, ip := range ips {- if ip.IsLoopback() || ip.IsLinkLocalUnicast() || ip.IsPrivate() || ip.IsUnspecified() {+ // 与 SSRFSafeDialer 共用 isPrivateIP,覆盖 NAT64、6to4、Teredo 等 IPv6 过渡地址。+ if isPrivateIP(ip) {return errors.New("access to private/internal IP is prohibited")}}diff --git a/kernel/util/net.go b/kernel/util/net.goindex 890a1de70e..6f50ed6bb2 100644--- a/kernel/util/net.go+++ b/kernel/util/net.go@@ -169,29 +169,54 @@ func SSRFSafeDialer(timeout time.Duration) *net.Dialer {}}-// isPrivateIP 判断 IP 是否为私网地址,含内嵌私网 IPv4 的 IPv6 过渡地址(NAT64、6to4、Teredo)。+// isPrivateIP 判断 IP 是否为私网地址,含内嵌私网 IPv4 的 IPv6 过渡地址(NAT64、6to4、Teredo、IPv4 兼容)。+// https://github.com/siyuan-note/siyuan/security/advisories/GHSA-qq8m-8p8v-x4xg+// https://github.com/siyuan-note/siyuan/security/advisories/GHSA-rg26-cg95-gq6pfunc isPrivateIP(ip net.IP) bool {if ip.IsLoopback() || ip.IsLinkLocalUnicast() || ip.IsLinkLocalMulticast() ||ip.IsPrivate() || ip.IsUnspecified() || ip.IsMulticast() {return true}+ // Go 标准库的分类方法不识别 IPv6 过渡地址,需按 RFC 内嵌格式提取其中的 IPv4 后再递归判断。+ if ip4 := extractEmbeddedIPv4(ip); nil != ip4 && !ip4.Equal(ip) {+ return isPrivateIP(ip4)+ }+ return false+}- // Go 标准库的 IsPrivate 不识别 IPv6 过渡地址,需按 RFC 内嵌格式提取其中的 IPv4 后再递归判断。- if ip16 := ip.To16(); nil != ip16 && 16 == len(ip16) {- // NAT64(RFC 6052 64:ff9b::/96,含 RFC 8215 64:ff9b:1::/48):低 32 位为内嵌 IPv4。- if ip16[0] == 0x00 && ip16[1] == 0x64 && ip16[2] == 0xff && ip16[3] == 0x9b {- return isPrivateIP(net.IPv4(ip16[12], ip16[13], ip16[14], ip16[15]))- }- // 6to4(RFC 3056 2002::/16):第 16-47 位为内嵌 IPv4。- if ip16[0] == 0x20 && ip16[1] == 0x02 {- return isPrivateIP(net.IPv4(ip16[2], ip16[3], ip16[4], ip16[5]))- }- // Teredo(RFC 4380 2001:0000::/32):低 32 位按位取反后为内嵌 IPv4。- if ip16[0] == 0x20 && ip16[1] == 0x01 && ip16[2] == 0x00 && ip16[3] == 0x00 {- return isPrivateIP(net.IPv4(ip16[12]^0xff, ip16[13]^0xff, ip16[14]^0xff, ip16[15]^0xff))+// extractEmbeddedIPv4 提取 IPv6 过渡地址(NAT64、6to4、Teredo、IPv4 兼容)中内嵌的 IPv4 地址,非过渡地址返回 nil。+func extractEmbeddedIPv4(ip net.IP) net.IP {+ ip16 := ip.To16()+ if nil == ip16 || 16 != len(ip16) {+ return nil+ }+ // NAT64(RFC 6052 64:ff9b::/96,含 RFC 8215 64:ff9b:1::/48):低 32 位为内嵌 IPv4。+ if ip16[0] == 0x00 && ip16[1] == 0x64 && ip16[2] == 0xff && ip16[3] == 0x9b {+ return net.IPv4(ip16[12], ip16[13], ip16[14], ip16[15])+ }+ // 6to4(RFC 3056 2002::/16):第 16-47 位为内嵌 IPv4。+ if ip16[0] == 0x20 && ip16[1] == 0x02 {+ return net.IPv4(ip16[2], ip16[3], ip16[4], ip16[5])+ }+ // Teredo(RFC 4380 2001:0000::/32):低 32 位按位取反后为内嵌 IPv4。+ if ip16[0] == 0x20 && ip16[1] == 0x01 && ip16[2] == 0x00 && ip16[3] == 0x00 {+ return net.IPv4(ip16[12]^0xff, ip16[13]^0xff, ip16[14]^0xff, ip16[15]^0xff)+ }+ // IPv4 兼容地址(RFC 4291 已废弃 ::/96):低 32 位为内嵌 IPv4。+ if isZeroIPv6(ip16[0:12]) {+ return net.IPv4(ip16[12], ip16[13], ip16[14], ip16[15])+ }+ return nil+}++// isZeroIPv6 判断字节切片是否全为零。+func isZeroIPv6(b []byte) bool {+ for _, v := range b {+ if 0 != v {+ return false}}- return false+ return true}func IsOnline(checkURL string, skipTlsVerify bool, timeout int) bool {diff --git a/kernel/util/net_test.go b/kernel/util/net_test.goindex 550f15944e..f222f5c33f 100644--- a/kernel/util/net_test.go+++ b/kernel/util/net_test.go@@ -54,6 +54,7 @@ func TestIsSessionOriginAllowed(t *testing.T) {// TestIsPrivateIP 验证 isPrivateIP 对私网地址及 IPv6 过渡地址的判断// https://github.com/siyuan-note/siyuan/security/advisories/GHSA-qq8m-8p8v-x4xg+// https://github.com/siyuan-note/siyuan/security/advisories/GHSA-rg26-cg95-gq6pfunc TestIsPrivateIP(t *testing.T) {tests := []struct {name string@@ -90,6 +91,11 @@ func TestIsPrivateIP(t *testing.T) {{name: "Teredo private", ip: "2001:0000:0000:0000:0000:0000:3f57:fefe", want: true},{name: "Teredo link-local", ip: "2001:0000:0000:0000:0000:0000:5601:5601", want: true},{name: "Teredo public", ip: "2001:0000:0000:0000:0000:0000:f7f7:f7f7", want: false},+ // IPv4 兼容地址(RFC 4291 已废弃 ::/96)+ {name: "IPv4-compatible loopback", ip: "::127.0.0.1", want: true},+ {name: "IPv4-compatible private", ip: "::c0a8:101", want: true},+ {name: "IPv4-compatible public", ip: "::808:808", want: false},+ {name: "IPv6 non-transition", ip: "::2", want: false},}for _, test := range tests {@@ -105,6 +111,42 @@ func TestIsPrivateIP(t *testing.T) {}}+// TestCheckHostSSRF 验证 CheckHostSSRF 对私网地址及 IPv6 过渡地址的拦截+// https://github.com/siyuan-note/siyuan/security/advisories/GHSA-rg26-cg95-gq6p+func TestCheckHostSSRF(t *testing.T) {+ tests := []struct {+ name string+ host string+ want bool+ }{+ {name: "IPv4 loopback", host: "127.0.0.1", want: true},+ {name: "IPv4 private", host: "192.168.1.1", want: true},+ {name: "IPv4 link-local", host: "169.254.169.254", want: true},+ {name: "IPv6 loopback", host: "::1", want: true},+ {name: "IPv6 IPv4-mapped loopback", host: "::ffff:127.0.0.1", want: true},+ {name: "NAT64 loopback", host: "64:ff9b::7f00:1", want: true},+ {name: "NAT64 link-local", host: "64:ff9b::a9fe:a9fe", want: true},+ {name: "NAT64 private", host: "64:ff9b::c0a8:101", want: true},+ {name: "NAT64 local-use private", host: "64:ff9b:1::c0a8:101", want: true},+ {name: "6to4 loopback", host: "2002:7f00:1::1", want: true},+ {name: "6to4 private", host: "2002:c0a8:101::1", want: true},+ {name: "Teredo private", host: "2001:0000:0000:0000:0000:0000:3f57:fefe", want: true},+ {name: "IPv4-compatible loopback", host: "::127.0.0.1", want: true},+ {name: "public IPv4", host: "8.8.8.8", want: false},+ {name: "public IPv6", host: "2606:4700:4700::1111", want: false},+ {name: "NAT64 public", host: "64:ff9b::808:808", want: false},+ }++ for _, test := range tests {+ t.Run(test.name, func(t *testing.T) {+ err := CheckHostSSRF(test.host)+ if test.want != (nil != err) {+ t.Fatalf("CheckHostSSRF(%q) error [%v], want blocked [%v]", test.host, err, test.want)+ }+ })+ }+}+// TestSSRFSafeDialerSafeMode 验证安全模式下 SSRFSafeDialer 会阻止对私网及 IPv6 过渡地址的连接// https://github.com/siyuan-note/siyuan/security/advisories/GHSA-qq8m-8p8v-x4xgfunc TestSSRFSafeDialerSafeMode(t *testing.T) {