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: Prevent DNS rebinding SSRF in agent HTTP tools https://github.com/siyuan-note/siyuan/security/advisories/GHSA-x8gv-g2g3-65fj
Danieldd2778b70d02
diff --git a/kernel/util/httprequest.go b/kernel/util/httprequest.goindex 961fb76557..1fcd7a1f87 100644--- a/kernel/util/httprequest.go+++ b/kernel/util/httprequest.go@@ -21,12 +21,13 @@ import ("fmt""io""net"+ "net/http""net/url""os""path/filepath""strings"+ "time"- "github.com/imroc/req/v3""github.com/siyuan-note/httpclient")@@ -53,6 +54,18 @@ func CheckHostSSRF(host string) error {return nil}+// ssrfSafeClient 是智能体出站请求专用的 HTTP 客户端:保留 httpclient 的统一 UA 注入与传输配置,+// 拨号走 ssrfSafeDialContext,连接阶段无条件拦截私网地址(不依赖 SafeMode),+// 防止 CheckHostSSRF 与拨号之间的 DNS 重绑定 TOCTOU 绕过。+// https://github.com/siyuan-note/siyuan/security/advisories/GHSA-x8gv-g2g3-65fj+var ssrfSafeClient = newSSRFSafeClient()++func newSSRFSafeClient() *http.Client {+ transport := httpclient.NewTransport(false)+ transport.DialContext = ssrfSafeDialContext(30 * time.Second)+ return &http.Client{Timeout: 30 * time.Second, Transport: &httpclient.UserAgentTransport{Base: transport}}+}+// HTTPRequest 发起一次通用 HTTP 调用,供智能体 http_request 工具使用。// 与 WebFetch 不同:本函数不做 HTML→Markdown 转换,文本类响应(含 JSON/XML)原样返回,// 便于智能体直接消费 REST API 的 JSON 输出。method 取值:GET/POST/PUT/DELETE/PATCH。@@ -75,15 +88,19 @@ func HTTPRequest(method, rawURL string, headers map[string]string, body string)method = "GET"}- request := httpclient.NewBrowserRequest()- for k, v := range headers {- request.SetHeader(k, v)- }+ var reqBody io.Readerif body != "" && method != "GET" && method != "HEAD" {- request.SetBody(body)+ reqBody = strings.NewReader(body)+ }+ req, err := http.NewRequest(method, rawURL, reqBody)+ if err != nil {+ return 0, "", "", errors.New("invalid request: " + err.Error())+ }+ for k, v := range headers {+ req.Header.Set(k, v)}- resp, err := sendByMethod(request, method, rawURL)+ resp, err := ssrfSafeClient.Do(req)if err != nil {return 0, "", "", errors.New("request failed: " + err.Error())}@@ -126,24 +143,6 @@ func HTTPRequest(method, rawURL string, headers map[string]string, body string)return statusCode, contentType, truncateRunes(string(respBody), maxHTTPRequestChars), nil}-// sendByMethod 按 method 分发请求,统一走 NewBrowserRequest 返回的 *req.Request。-func sendByMethod(request *req.Request, method, rawURL string) (*req.Response, error) {- switch method {- case "GET", "":- return request.Get(rawURL)- case "POST":- return request.Post(rawURL)- case "PUT":- return request.Put(rawURL)- case "DELETE":- return request.Delete(rawURL)- case "PATCH":- return request.Patch(rawURL)- default:- return nil, fmt.Errorf("unsupported method: %s", method)- }-}-// isTextContentType 判断 Content-Type 是否为可直接展示给智能体的文本类响应。// 覆盖 text/*、application/json、application/xml、application/*+json 等。func isTextContentType(contentType string) bool {diff --git a/kernel/util/net.go b/kernel/util/net.goindex 6f50ed6bb2..29f276af5a 100644--- a/kernel/util/net.go+++ b/kernel/util/net.go@@ -17,6 +17,7 @@package utilimport (+ "context""errors""fmt""io"@@ -169,6 +170,46 @@ func SSRFSafeDialer(timeout time.Duration) *net.Dialer {}}+// ssrfSafeDialContext 返回智能体出站请求专用的拨号函数:拨号时自行解析主机名并拒绝私网地址,+// 同时直接连接解析出的公网 IP,使 CheckHostSSRF 的守卫结果与拨号目标一致,+// 从根上杜绝 DNS 重绑定导致的 TOCTOU 绕过。+// 与 SSRFSafeDialer 不同,本拨号函数不依赖 SafeMode,始终强制执行。+// https://github.com/siyuan-note/siyuan/security/advisories/GHSA-x8gv-g2g3-65fj+func ssrfSafeDialContext(timeout time.Duration) func(ctx context.Context, network, addr string) (net.Conn, error) {+ dialer := &net.Dialer{Timeout: timeout}+ return func(ctx context.Context, network, addr string) (net.Conn, error) {+ host, port, err := net.SplitHostPort(addr)+ if err != nil {+ return nil, err+ }+ if ip := net.ParseIP(host); ip != nil {+ if isPrivateIP(ip) {+ return nil, errors.New("access to private/internal IP is prohibited")+ }+ return dialer.DialContext(ctx, network, addr)+ }+ ips, err := net.DefaultResolver.LookupIPAddr(ctx, host)+ if err != nil {+ return nil, err+ }+ var lastErr error+ for _, ipAddr := range ips {+ if isPrivateIP(ipAddr.IP) {+ continue+ }+ conn, err := dialer.DialContext(ctx, network, net.JoinHostPort(ipAddr.IP.String(), port))+ if err == nil {+ return conn, nil+ }+ lastErr = err+ }+ if lastErr != nil {+ return nil, lastErr+ }+ return nil, errors.New("host has no public IP: " + host)+ }+}+// 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-gq6pdiff --git a/kernel/util/net_test.go b/kernel/util/net_test.goindex f222f5c33f..4b5a3cdcb6 100644--- a/kernel/util/net_test.go+++ b/kernel/util/net_test.go@@ -17,8 +17,10 @@package utilimport (+ "context""net""testing"+ "time")// TestIsSessionOriginAllowed 验证会话 Cookie 认证的 Origin 校验逻辑@@ -178,3 +180,35 @@ func TestSSRFSafeDialerSafeMode(t *testing.T) {})}}++// TestSSRFSafeDialContext 验证 ssrfSafeDialContext 在连接阶段无条件拦截私网地址(不依赖 SafeMode),+// 并拒绝解析结果全部为私网的域名,杜绝 DNS 重绑定 TOCTOU。+// https://github.com/siyuan-note/siyuan/security/advisories/GHSA-x8gv-g2g3-65fj+func TestSSRFSafeDialContext(t *testing.T) {+ dialContext := ssrfSafeDialContext(500 * time.Millisecond)++ 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: "link-local", address: "169.254.169.254:80", blocked: true},+ {name: "IPv6 loopback", address: "[::1]:80", blocked: true},+ {name: "NAT64 loopback", address: "[64:ff9b::7f00:1]:80", blocked: true},+ {name: "localhost 解析结果全为私网", address: "localhost:80", blocked: true},+ }++ for _, test := range tests {+ t.Run(test.name, func(t *testing.T) {+ conn, err := dialContext(context.Background(), "tcp", test.address)+ if conn != nil {+ conn.Close()+ }+ if test.blocked != (nil != err) {+ t.Fatalf("dial %q error [%v], want blocked [%v]", test.address, err, test.blocked)+ }+ })+ }+}diff --git a/kernel/util/webfetch.go b/kernel/util/webfetch.goindex fec8ffc594..d5fe66f84a 100644--- a/kernel/util/webfetch.go+++ b/kernel/util/webfetch.go@@ -29,7 +29,6 @@ import ("github.com/88250/gulu""github.com/88250/lute"- "github.com/siyuan-note/httpclient")const (@@ -51,7 +50,7 @@ func WebFetch(rawURL, format string) (string, error) {return "", err}- resp, err := httpclient.NewBrowserRequest().Get(rawURL)+ resp, err := ssrfSafeClient.Get(rawURL)if err != nil {return "", errors.New("fetch failed: " + err.Error())}