Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:lock: Fix SSRF bypass via IPv6 transition addresses in safe mode https://github.com/siyuan-note/siyuan/security/advisories/GHSA-qq8m-8p8v-x4xg

Danielecdf6038054d
diff --git a/kernel/util/net.go b/kernel/util/net.go
index 3cc6ac2e72..34cc228028 100644
--- a/kernel/util/net.go
+++ b/kernel/util/net.go
@@ -169,8 +169,29 @@ func SSRFSafeDialer(timeout time.Duration) *net.Dialer {
}
}
+// isPrivateIP 判断 IP 是否为私网地址,含内嵌私网 IPv4 的 IPv6 过渡地址(NAT64、6to4、Teredo)。
func isPrivateIP(ip net.IP) bool {
- return ip.IsLoopback() || ip.IsLinkLocalUnicast() || ip.IsPrivate() || ip.IsUnspecified()
+ if ip.IsLoopback() || ip.IsLinkLocalUnicast() || ip.IsLinkLocalMulticast() ||
+ ip.IsPrivate() || ip.IsUnspecified() || ip.IsMulticast() {
+ return true
+ }
+
+ // 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))
+ }
+ }
+ return false
}
func IsOnline(checkURL string, skipTlsVerify bool, timeout int) bool {
diff --git a/kernel/util/net_test.go b/kernel/util/net_test.go
index 10093acf49..4dbce01953 100644
--- a/kernel/util/net_test.go
+++ b/kernel/util/net_test.go
@@ -16,7 +16,10 @@
package util
-import "testing"
+import (
+ "net"
+ "testing"
+)
// TestIsSessionOriginAllowed 验证会话 Cookie 认证的 Origin 校验逻辑
// https://github.com/siyuan-note/siyuan/security/advisories/GHSA-hhm2-g993-p656
@@ -48,3 +51,88 @@ func TestIsSessionOriginAllowed(t *testing.T) {
})
}
}
+
+// TestIsPrivateIP 验证 isPrivateIP 对私网地址及 IPv6 过渡地址的判断
+// https://github.com/siyuan-note/siyuan/security/advisories/GHSA-qq8m-8p8v-x4xg
+func TestIsPrivateIP(t *testing.T) {
+ tests := []struct {
+ name string
+ ip string
+ want bool
+ }{
+ // IPv4
+ {name: "IPv4 loopback", ip: "127.0.0.1", want: true},
+ {name: "IPv4 private 10/8", ip: "10.0.0.1", want: true},
+ {name: "IPv4 private 172.16/12", ip: "172.16.0.1", want: true},
+ {name: "IPv4 private 192.168/16", ip: "192.168.1.1", want: true},
+ {name: "IPv4 link-local", ip: "169.254.169.254", want: true},
+ {name: "IPv4 unspecified", ip: "0.0.0.0", want: true},
+ {name: "IPv4 multicast", ip: "224.0.0.1", want: true},
+ {name: "IPv4 public", ip: "8.8.8.8", want: false},
+ // IPv6
+ {name: "IPv6 loopback", ip: "::1", want: true},
+ {name: "IPv6 ULA", ip: "fc00::1", want: true},
+ {name: "IPv6 link-local", ip: "fe80::1", want: true},
+ {name: "IPv6 unspecified", ip: "::", want: true},
+ {name: "IPv6 multicast", ip: "ff02::1", want: true},
+ {name: "IPv6 IPv4-mapped private", ip: "::ffff:192.168.1.1", want: true},
+ {name: "IPv6 documentation", ip: "2001:db8::1", want: false},
+ {name: "IPv6 public", ip: "2606:4700:4700::1111", want: false},
+ // IPv6 过渡地址
+ {name: "NAT64 loopback", ip: "64:ff9b::7f00:1", want: true},
+ {name: "NAT64 link-local", ip: "64:ff9b::a9fe:a9fe", want: true},
+ {name: "NAT64 private", ip: "64:ff9b::c0a8:101", want: true},
+ {name: "NAT64 public", ip: "64:ff9b::808:808", want: false},
+ {name: "NAT64 local-use private", ip: "64:ff9b:1::c0a8:101", want: true},
+ {name: "6to4 private", ip: "2002:c0a8:101::1", want: true},
+ {name: "6to4 loopback", ip: "2002:7f00:1::1", want: true},
+ {name: "6to4 public", ip: "2002:808:808::1", want: false},
+ {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},
+ }
+
+ for _, test := range tests {
+ t.Run(test.name, func(t *testing.T) {
+ ip := net.ParseIP(test.ip)
+ if nil == ip {
+ t.Fatalf("parse IP [%s] failed", test.ip)
+ }
+ if got := isPrivateIP(ip); got != test.want {
+ t.Fatalf("isPrivateIP(%q) = %v, want %v", test.ip, got, test.want)
+ }
+ })
+ }
+}
+
+// TestSSRFSafeDialerSafeMode 验证安全模式下 SSRFSafeDialer 会阻止对私网及 IPv6 过渡地址的连接
+// https://github.com/siyuan-note/siyuan/security/advisories/GHSA-qq8m-8p8v-x4xg
+func TestSSRFSafeDialerSafeMode(t *testing.T) {
+ previous := SafeMode
+ SafeMode = true
+ defer func() { SafeMode = previous }()
+
+ tests := []struct {
+ name string
+ address string
+ blocked bool
+ }{
+ {name: "loopback", address: "127.0.0.1:80", blocked: true},
+ {name: "private", address: "192.168.1.1:80", blocked: true},
+ {name: "NAT64 loopback", address: "64:ff9b::7f00:1:80", blocked: true},
+ {name: "NAT64 link-local", address: "64:ff9b::a9fe:a9fe:80", blocked: true},
+ {name: "6to4 private", address: "2002:c0a8:101::1:80", blocked: true},
+ {name: "Teredo private", address: "2001:0000:0000:0000:0000:0000:3f57:fefe:80", blocked: true},
+ {name: "public", address: "8.8.8.8:80", blocked: false},
+ }
+
+ dialer := SSRFSafeDialer(30)
+ for _, test := range tests {
+ t.Run(test.name, func(t *testing.T) {
+ err := dialer.Control("tcp", test.address, nil)
+ if test.blocked != (nil != err) {
+ t.Fatalf("Control(%q) error [%v], want blocked [%v]", test.address, err, test.blocked)
+ }
+ })
+ }
+}