Rewrite LuCI bootstrap paths under proxy prefix

This commit is contained in:
benya
2026-06-04 19:42:34 +03:00
parent 072025be54
commit 44e8e47cbc
2 changed files with 12 additions and 2 deletions
+5 -1
View File
@@ -1135,7 +1135,7 @@ func (a *App) proxyLuCI(w http.ResponseWriter, r *http.Request, deviceID, sessio
removeCookie(req, luciRouteCookie) removeCookie(req, luciRouteCookie)
} }
proxy.ModifyResponse = func(resp *http.Response) error { proxy.ModifyResponse = func(resp *http.Response) error {
if resp.StatusCode >= 400 { if resp.StatusCode >= 400 && resp.Header.Get("X-LuCI-Login-Required") == "" {
logStructured(map[string]any{ logStructured(map[string]any{
"event": "luci.upstream_error", "event": "luci.upstream_error",
"request_id": requestID(r.Context()), "request_id": requestID(r.Context()),
@@ -1205,6 +1205,10 @@ func rewriteLuCIBody(body []byte, prefix string) []byte {
replacement := marker[:len(marker)-1] + prefix + "/" replacement := marker[:len(marker)-1] + prefix + "/"
body = bytes.ReplaceAll(body, []byte(marker), []byte(replacement)) 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 return body
} }
+7 -1
View File
@@ -426,7 +426,7 @@ func TestLuCIProxyRequiresActiveSessionAndRewritesPaths(t *testing.T) {
} }
upstreamPath = r.URL.Path upstreamPath = r.URL.Path
w.Header().Set("Content-Type", "text/html") w.Header().Set("Content-Type", "text/html")
_, _ = io.WriteString(w, `<a href="/cgi-bin/luci/admin">LuCI</a><link href="/luci-static/test.css">`) _, _ = io.WriteString(w, `<a href="/cgi-bin/luci/admin">LuCI</a><link href="/luci-static/test.css"><script>L = new LuCI({ "resource": "\/luci-static\/resources", "scriptname": "\/cgi-bin\/luci", "ubuspath": "\/ubus\/" });</script>`)
})) }))
defer upstream.Close() defer upstream.Close()
upstreamURL, err := url.Parse(upstream.URL) upstreamURL, err := url.Parse(upstream.URL)
@@ -472,6 +472,12 @@ func TestLuCIProxyRequiresActiveSessionAndRewritesPaths(t *testing.T) {
if !strings.Contains(body, `href="`+prefix+`/cgi-bin/luci/admin"`) || !strings.Contains(body, `href="`+prefix+`/luci-static/test.css"`) { 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) t.Fatalf("LuCI paths were not rewritten: %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)
}
}
jar, err := cookiejar.New(nil) jar, err := cookiejar.New(nil)
if err != nil { if err != nil {