Skip to content

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

:bug: Preserve raw forward proxy response bytes https://github.com/siyuan-note/siyuan/issues/18978

Daniele3a0800ce570
diff --git a/docs/API.ja.md b/docs/API.ja.md
index 606422a22e..7e5e808c9f 100644
--- a/docs/API.ja.md
+++ b/docs/API.ja.md
@@ -1622,6 +1622,8 @@
* `base32` | `base32-std`
* `base32-hex`
* `hex`
+
+ `text`は既存の動作を維持し、該当する場合は文字コードをUTF-8に変換します。バイナリエンコーディングは文字コード変換前のレスポンス本文データに適用され、gzip展開など既存のHTTPコンテンツデコード動作は変わりません。
* 戻り値
```json
diff --git a/docs/API.md b/docs/API.md
index 103f7875f4..7462b6b352 100644
--- a/docs/API.md
+++ b/docs/API.md
@@ -1628,6 +1628,8 @@ Note: To ensure data security, access to this interface is prohibited in Publish
* `base32` | `base32-std`
* `base32-hex`
* `hex`
+
+ `text` preserves the existing behavior and converts the character set to UTF-8 when applicable. The binary encodings encode the response body before character-set conversion; existing HTTP content decoding behavior, such as gzip decompression, is unchanged.
* Return value
```json
diff --git a/docs/API.zh-CN.md b/docs/API.zh-CN.md
index 4a3ae7d94c..bbefb498f3 100644
--- a/docs/API.zh-CN.md
+++ b/docs/API.zh-CN.md
@@ -1622,6 +1622,8 @@
* `base32` | `base32-std`
* `base32-hex`
* `hex`
+
+ `text` 保持现有行为,在适用时将字符集转换为 UTF-8。二进制编码作用于字符集转换前的响应正文数据;gzip 解压等现有 HTTP 内容解码行为不变。
* 返回值
```json
diff --git a/kernel/api/network.go b/kernel/api/network.go
index d2464dad11..df64243bb0 100644
--- a/kernel/api/network.go
+++ b/kernel/api/network.go
@@ -186,6 +186,7 @@ func forwardProxy(c *gin.Context) {
}
client := getSafeClient(time.Duration(timeout) * time.Millisecond)
+ responseEncoding := configureForwardProxyResponseEncoding(client, arg["responseEncoding"])
if redirectArg, ok := arg["redirect"].(bool); ok && !redirectArg {
client.SetRedirectPolicy(req.NoRedirectPolicy())
}
@@ -278,11 +279,6 @@ func forwardProxy(c *gin.Context) {
elapsed := time.Since(started)
- responseEncoding := "text"
- if responseEncodingArg := arg["responseEncoding"]; nil != responseEncodingArg {
- responseEncoding = responseEncodingArg.(string)
- }
-
body := ""
switch responseEncoding {
case "base64":
@@ -328,6 +324,21 @@ func forwardProxy(c *gin.Context) {
// elapsed.Seconds(), len(bodyData), data["url"], headers, contentType, arg["payload"], data["status"], shortBody)
}
+func configureForwardProxyResponseEncoding(client *req.Client, value any) string {
+ responseEncoding, ok := value.(string)
+ if !ok {
+ return "text"
+ }
+
+ switch responseEncoding {
+ case "base64", "base64-std", "base64-url", "base32", "base32-std", "base32-hex", "hex":
+ client.DisableAutoDecode()
+ return responseEncoding
+ default:
+ return "text"
+ }
+}
+
// 创建安全的 HTTP Client,防止 SSRF 和 DNS 重绑定
func getSafeClient(timeout time.Duration) *req.Client {
dialer := util.SSRFSafeDialer(timeout)
diff --git a/kernel/api/network_test.go b/kernel/api/network_test.go
new file mode 100644
index 0000000000..f8980c781f
--- /dev/null
+++ b/kernel/api/network_test.go
@@ -0,0 +1,63 @@
+package api
+
+import (
+ "bytes"
+ "io"
+ "net/http"
+ "net/http/httptest"
+ "testing"
+
+ "github.com/imroc/req/v3"
+)
+
+func TestConfigureForwardProxyResponseEncoding(t *testing.T) {
+ rawBody := append([]byte("<html><head><title>"), 0xd6, 0xd0, 0xb9, 0xfa)
+ rawBody = append(rawBody, []byte("</title></head></html>")...)
+ textBody := []byte("<html><head><title>中国</title></head></html>")
+
+ server := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, _ *http.Request) {
+ writer.Header().Set("Content-Type", "text/html; charset=gbk")
+ _, _ = writer.Write(rawBody)
+ }))
+ defer server.Close()
+
+ tests := []struct {
+ name string
+ value any
+ wantEncoding string
+ wantBody []byte
+ }{
+ {name: "default", value: nil, wantEncoding: "text", wantBody: textBody},
+ {name: "text", value: "text", wantEncoding: "text", wantBody: textBody},
+ {name: "unknown", value: "unknown", wantEncoding: "text", wantBody: textBody},
+ {name: "invalid type", value: 1, wantEncoding: "text", wantBody: textBody},
+ {name: "base64", value: "base64", wantEncoding: "base64", wantBody: rawBody},
+ {name: "base64 std", value: "base64-std", wantEncoding: "base64-std", wantBody: rawBody},
+ {name: "base64 url", value: "base64-url", wantEncoding: "base64-url", wantBody: rawBody},
+ {name: "base32", value: "base32", wantEncoding: "base32", wantBody: rawBody},
+ {name: "base32 std", value: "base32-std", wantEncoding: "base32-std", wantBody: rawBody},
+ {name: "base32 hex", value: "base32-hex", wantEncoding: "base32-hex", wantBody: rawBody},
+ {name: "hex", value: "hex", wantEncoding: "hex", wantBody: rawBody},
+ }
+
+ for _, test := range tests {
+ t.Run(test.name, func(t *testing.T) {
+ client := req.C()
+ if encoding := configureForwardProxyResponseEncoding(client, test.value); encoding != test.wantEncoding {
+ t.Fatalf("encoding = %q, want %q", encoding, test.wantEncoding)
+ }
+
+ response, err := client.R().Get(server.URL)
+ if err != nil {
+ t.Fatalf("get response failed: %s", err)
+ }
+ body, err := io.ReadAll(response.Body)
+ if err != nil {
+ t.Fatalf("read response failed: %s", err)
+ }
+ if !bytes.Equal(body, test.wantBody) {
+ t.Fatalf("body = %x, want %x", body, test.wantBody)
+ }
+ })
+ }
+}