An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:bug: Preserve request host in publish proxy https://github.com/siyuan-note/siyuan/issues/18670
Danielf23a8dffff92
diff --git a/kernel/server/proxy/publish.go b/kernel/server/proxy/publish.goindex 6ad978e186..2cf1f0b99f 100644--- a/kernel/server/proxy/publish.go+++ b/kernel/server/proxy/publish.go@@ -24,6 +24,7 @@ import ("net""net/http""net/http/httputil"+ "net/url""strconv""time"@@ -132,10 +133,7 @@ func closePublishListener() {func startPublishReverseProxyService() {logging.LogInfof("publish service [%s:%s] is running", Host, Port)- handler := &httputil.ReverseProxy{- Rewrite: rewrite,- Transport: transport,- }+ handler := newPublishReverseProxy(util.ServerURL, transport)certPath, keyPath, certErr := util.GetOrCreateTLSCert()if certErr == nil && "" != certPath {@@ -159,10 +157,15 @@ func startPublishReverseProxyService() {logging.LogInfof("publish service [%s:%s] is stopped", Host, Port)}-func rewrite(r *httputil.ProxyRequest) {- r.SetURL(util.ServerURL)- r.SetXForwarded()- // r.Out.Host = r.In.Host // if desired+func newPublishReverseProxy(target *url.URL, roundTripper http.RoundTripper) *httputil.ReverseProxy {+ return &httputil.ReverseProxy{+ Rewrite: func(request *httputil.ProxyRequest) {+ request.SetURL(target)+ request.Out.Host = request.In.Host+ request.SetXForwarded()+ },+ Transport: roundTripper,+ }}// publishAuthRejectResponse 构造发布服务认证拒绝响应,retryAfter 大于 0 时附加 Retry-After 头。diff --git a/kernel/server/proxy/publish_test.go b/kernel/server/proxy/publish_test.gonew file mode 100644index 0000000000..0586eea241--- /dev/null+++ b/kernel/server/proxy/publish_test.go@@ -0,0 +1,143 @@+// SiYuan - From thought to insight, with agents+// Copyright (c) 2020-present, b3log.org+//+// This program is free software: you can redistribute it and/or modify+// it under the terms of the GNU Affero General Public License as published by+// the Free Software Foundation, either version 3 of the License, or+// (at your option) any later version.+//+// This program is distributed in the hope that it will be useful,+// but WITHOUT ANY WARRANTY; without even the implied warranty of+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+// GNU Affero General Public License for more details.+//+// You should have received a copy of the GNU Affero General Public License+// along with this program. If not, see <https://www.gnu.org/licenses/>.++package proxy++import (+ "net"+ "net/http"+ "net/http/httptest"+ "strings"+ "testing"+ "time"++ "github.com/gorilla/websocket"+ "github.com/siyuan-note/siyuan/kernel/util"+)++func TestPublishReverseProxyPreservesRequestHost(t *testing.T) {+ requestInfo := make(chan http.Header, 1)+ hostInfo := make(chan string, 1)+ backend := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {+ requestInfo <- request.Header.Clone()+ hostInfo <- request.Host+ writer.WriteHeader(http.StatusNoContent)+ }))+ defer backend.Close()++ proxy := httptest.NewServer(newPublishReverseProxy(mustParseURL(t, backend.URL), http.DefaultTransport))+ defer proxy.Close()++ request, err := http.NewRequest(http.MethodGet, proxy.URL+"/api/system/version", nil)+ if err != nil {+ t.Fatal(err)+ }+ request.Host = "siyuan.example"+ request.Header.Set("Forwarded", "for=192.0.2.1")+ request.Header.Set("X-Forwarded-For", "192.0.2.1")+ request.Header.Set("X-Forwarded-Host", "spoofed.example")+ request.Header.Set("X-Forwarded-Proto", "https")++ response, err := http.DefaultClient.Do(request)+ if err != nil {+ t.Fatal(err)+ }+ response.Body.Close()+ if response.StatusCode != http.StatusNoContent {+ t.Fatalf("proxy returned %d, want %d", response.StatusCode, http.StatusNoContent)+ }++ headers := <-requestInfo+ forwardedFor := headers.Get("X-Forwarded-For")+ if ip := net.ParseIP(forwardedFor); ip == nil || !ip.IsLoopback() {+ t.Fatalf("X-Forwarded-For = %q, want proxy client loopback address", forwardedFor)+ }+ if actual := headers.Get("X-Forwarded-Host"); actual != request.Host {+ t.Fatalf("X-Forwarded-Host = %q, want %q", actual, request.Host)+ }+ if actual := headers.Get("X-Forwarded-Proto"); actual != "http" {+ t.Fatalf("X-Forwarded-Proto = %q, want http", actual)+ }+ if actual := headers.Get("Forwarded"); actual != "" {+ t.Fatalf("Forwarded = %q, want empty", actual)+ }+ if actual := <-hostInfo; actual != request.Host {+ t.Fatalf("Host = %q, want %q", actual, request.Host)+ }+}++func TestPublishReverseProxyWebSocketOrigin(t *testing.T) {+ upgrader := websocket.Upgrader{+ CheckOrigin: func(request *http.Request) bool {+ return util.IsSessionOriginAllowed(request.Header.Get("Origin"), request.Host)+ },+ }+ backend := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {+ connection, err := upgrader.Upgrade(writer, request, nil)+ if err != nil {+ return+ }+ defer connection.Close()+ for {+ if _, _, err = connection.ReadMessage(); err != nil {+ return+ }+ }+ }))+ defer backend.Close()++ proxy := httptest.NewServer(newPublishReverseProxy(mustParseURL(t, backend.URL), http.DefaultTransport))+ defer proxy.Close()+ websocketURL := "ws" + strings.TrimPrefix(proxy.URL, "http") + "/ws"+ dialer := websocket.Dialer{HandshakeTimeout: 5 * time.Second}++ tests := []struct {+ name string+ origin string+ wantStatus int+ }{+ {name: "same origin", origin: "https://siyuan.example", wantStatus: http.StatusSwitchingProtocols},+ {name: "cross origin", origin: "https://evil.example", wantStatus: http.StatusForbidden},+ }+ for _, test := range tests {+ t.Run(test.name, func(t *testing.T) {+ headers := http.Header{+ "Host": {"siyuan.example"},+ "Origin": {test.origin},+ }+ connection, response, err := dialer.Dial(websocketURL, headers)+ if test.wantStatus == http.StatusSwitchingProtocols {+ if err != nil {+ t.Fatalf("websocket handshake failed: %v", err)+ }+ connection.Close()+ return+ }++ if err == nil {+ connection.Close()+ t.Fatal("cross-origin websocket handshake unexpectedly succeeded")+ }+ if response == nil {+ t.Fatalf("websocket handshake returned no response: %v", err)+ }+ defer response.Body.Close()+ if response.StatusCode != test.wantStatus {+ t.Fatalf("websocket handshake returned %d, want %d", response.StatusCode, test.wantStatus)+ }+ })+ }+}