Skip to content

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

:lock: Fix https://github.com/siyuan-note/siyuan/security/advisories/GHSA-56cv-c5p2-j2wg

Daniel3284e10028de
diff --git a/kernel/api/network.go b/kernel/api/network.go
index 910bf17dd8..a57937c5f6 100644
--- a/kernel/api/network.go
+++ b/kernel/api/network.go
@@ -23,10 +23,12 @@ import (
"fmt"
"io"
"math"
+ "net"
"net/http"
"net/textproto"
"net/url"
"strings"
+ "syscall"
"time"
"github.com/88250/gulu"
@@ -160,31 +162,40 @@ func forwardProxy(c *gin.Context) {
}
destURL := arg["url"].(string)
- if _, e := url.ParseRequestURI(destURL); nil != e {
+ u, e := url.ParseRequestURI(destURL)
+ if nil != e {
ret.Code = -1
ret.Msg = "invalid [url]"
return
}
+ if u.Scheme != "http" && u.Scheme != "https" {
+ ret.Code = -1
+ ret.Msg = "only http/https is allowed"
+ return
+ }
+
method := "POST"
if methodArg := arg["method"]; nil != methodArg {
method = strings.ToUpper(methodArg.(string))
}
- timeout := 7 * 1000
+ timeout := 7000
if timeoutArg := arg["timeout"]; nil != timeoutArg {
timeout = int(timeoutArg.(float64))
if 1 > timeout {
- timeout = 7 * 1000
+ timeout = 7000
}
}
- client := req.C()
- client.SetTimeout(time.Duration(timeout) * time.Millisecond)
+ client := getSafeClient(time.Duration(timeout) * time.Millisecond)
request := client.R()
- headers := arg["headers"].([]interface{})
- for _, pair := range headers {
- for k, v := range pair.(map[string]interface{}) {
- request.SetHeader(k, fmt.Sprintf("%s", v))
+ if headers, ok := arg["headers"].([]interface{}); ok {
+ for _, pair := range headers {
+ if m, ok := pair.(map[string]interface{}); ok {
+ for k, v := range m {
+ request.SetHeader(k, fmt.Sprintf("%v", v))
+ }
+ }
}
}
@@ -315,3 +326,33 @@ func forwardProxy(c *gin.Context) {
//logging.LogInfof("elapsed [%.1fs], length [%d], request [url=%s, headers=%s, content-type=%s, body=%s], status [%d], body [%s]",
// elapsed.Seconds(), len(bodyData), data["url"], headers, contentType, arg["payload"], data["status"], shortBody)
}
+
+// 校验 IP 是否为私有内网地址
+func isPrivateIP(ip net.IP) bool {
+ return ip.IsLoopback() || ip.IsLinkLocalUnicast() || ip.IsPrivate() || ip.IsUnspecified()
+}
+
+// 创建安全的 HTTP Client,防止 SSRF 和 DNS 重绑定
+func getSafeClient(timeout time.Duration) *req.Client {
+ dialer := &net.Dialer{
+ Timeout: timeout,
+ // Control 函数在解析出 IP 后、建立连接前执行,是防御 DNS Rebinding 的关键
+ Control: func(network, address string, c syscall.RawConn) error {
+ host, _, err := net.SplitHostPort(address)
+ if err != nil {
+ return err
+ }
+ ip := net.ParseIP(host)
+ if ip != nil && isPrivateIP(ip) {
+ return fmt.Errorf("ip address %s is prohibited", host)
+ }
+ return nil
+ },
+ }
+
+ client := req.C()
+ client.SetTimeout(timeout)
+ client.SetDial(dialer.DialContext)
+ client.SetRedirectPolicy(req.MaxRedirectPolicy(3))
+ return client
+}