From aa50728a2d6a1df7d425df32289f6cf3d3175492 Mon Sep 17 00:00:00 2001 From: benya Date: Thu, 4 Jun 2026 19:48:47 +0300 Subject: [PATCH] Proxy LuCI responses without body rewriting --- server/internal/httpapi/server.go | 27 -------------------------- server/internal/httpapi/server_test.go | 9 ++++----- 2 files changed, 4 insertions(+), 32 deletions(-) diff --git a/server/internal/httpapi/server.go b/server/internal/httpapi/server.go index 01dd183..0467cf5 100644 --- a/server/internal/httpapi/server.go +++ b/server/internal/httpapi/server.go @@ -1,7 +1,6 @@ package httpapi import ( - "bytes" "context" "crypto/rand" "crypto/tls" @@ -9,7 +8,6 @@ import ( "encoding/json" "errors" "fmt" - "io" "log" "net/http" "net/http/httputil" @@ -1147,19 +1145,6 @@ func (a *App) proxyLuCI(w http.ResponseWriter, r *http.Request, deviceID, sessio }) } rewriteLuCIHeaders(resp.Header, prefix) - contentType := resp.Header.Get("Content-Type") - if !strings.Contains(contentType, "text/html") { - return nil - } - body, err := io.ReadAll(resp.Body) - if err != nil { - return err - } - _ = resp.Body.Close() - body = rewriteLuCIBody(body, prefix) - resp.Body = io.NopCloser(bytes.NewReader(body)) - resp.ContentLength = int64(len(body)) - resp.Header.Set("Content-Length", strconv.Itoa(len(body))) return nil } proxy.ErrorHandler = func(w http.ResponseWriter, _ *http.Request, err error) { @@ -1200,18 +1185,6 @@ func rewriteLuCIHeaders(header http.Header, prefix string) { } } -func rewriteLuCIBody(body []byte, prefix string) []byte { - for _, marker := range []string{`="/`, `'/`, `url(/`} { - replacement := marker[:len(marker)-1] + prefix + "/" - body = bytes.ReplaceAll(body, []byte(marker), []byte(replacement)) - } - escapedPrefix := strings.ReplaceAll(prefix, "/", `\/`) - for _, root := range []string{`\/cgi-bin\/luci`, `\/luci-static`, `\/ubus\/`} { - body = bytes.ReplaceAll(body, []byte(root), []byte(escapedPrefix+root)) - } - return body -} - func (a *App) handleCloseRemoteSession(w http.ResponseWriter, r *http.Request) { deviceID, sessionID, ok := remoteSessionCloseIDsFromPath(r.URL.Path) if !ok { diff --git a/server/internal/httpapi/server_test.go b/server/internal/httpapi/server_test.go index 15e012b..b6ce3f9 100644 --- a/server/internal/httpapi/server_test.go +++ b/server/internal/httpapi/server_test.go @@ -474,13 +474,12 @@ func TestLuCIProxyRequiresActiveSessionAndRewritesPaths(t *testing.T) { body := requestText(t, http.MethodGet, srv.URL+"/luci/"+enrolled.DeviceID+"/"+session.ID+"/", "operator-test", nil, http.StatusOK) prefix := "/luci/" + enrolled.DeviceID + "/" + session.ID - if !strings.Contains(body, `href="`+prefix+`/cgi-bin/luci/admin"`) || !strings.Contains(body, `href="`+prefix+`/luci-static/test.css"`) { - t.Fatalf("LuCI paths were not rewritten: %s", body) + if !strings.Contains(body, `href="/cgi-bin/luci/admin"`) || !strings.Contains(body, `href="/luci-static/test.css"`) { + t.Fatalf("LuCI body was unexpectedly modified: %s", body) } - escapedPrefix := strings.ReplaceAll(prefix, "/", `\/`) for _, path := range []string{`\/luci-static\/resources`, `\/cgi-bin\/luci`, `\/ubus\/`} { - if !strings.Contains(body, escapedPrefix+path) { - t.Fatalf("escaped LuCI path %s was not rewritten: %s", path, body) + if !strings.Contains(body, path) { + t.Fatalf("escaped LuCI path %s was unexpectedly modified: %s", path, body) } }