From 48bd654839e8e2cdb69a77b790fca26fd6d11837 Mon Sep 17 00:00:00 2001 From: Lukasz Raczylo Date: Tue, 24 Nov 2026 02:11:32 +0000 Subject: [PATCH] fix(app): give proxy handlers a request that owns its strings The fiber adaptor builds the URL, Host, Method and header strings of the net/http request over the fasthttp request buffer. The next request on the connection overwrites that buffer. The proxies pass these strings to background scans, singleflight keys and analytics, which outlive the request. Each proxy route now receives a copy. --- pkg/app/app.go | 12 +++++----- pkg/app/detach.go | 54 ++++++++++++++++++++++++++++++++++++++++++ pkg/app/detach_test.go | 37 +++++++++++++++++++++++++++++ 3 files changed, 97 insertions(+), 6 deletions(-) create mode 100644 pkg/app/detach.go create mode 100644 pkg/app/detach_test.go diff --git a/pkg/app/app.go b/pkg/app/app.go index b8d3dec..4054e12 100644 --- a/pkg/app/app.go +++ b/pkg/app/app.go @@ -542,9 +542,9 @@ func (a *App) setupServer() error { a.prewarmFetcher.Mount("go", goProxyMounted) goProxyWithCDN := a.cdnMiddleware.Handler(goProxyMounted) if authEnabled { - a.app.All("/go/*", mwAuth, adaptor.HTTPHandler(goProxyWithCDN)) + a.app.All("/go/*", mwAuth, proxyHandler(goProxyWithCDN)) } else { - a.app.All("/go/*", adaptor.HTTPHandler(goProxyWithCDN)) + a.app.All("/go/*", proxyHandler(goProxyWithCDN)) } } @@ -566,9 +566,9 @@ func (a *App) setupServer() error { return c.Next() }) if authEnabled { - a.app.All("/npm/*", mwAuth, adaptor.HTTPHandler(npmProxyWithCDN)) + a.app.All("/npm/*", mwAuth, proxyHandler(npmProxyWithCDN)) } else { - a.app.All("/npm/*", adaptor.HTTPHandler(npmProxyWithCDN)) + a.app.All("/npm/*", proxyHandler(npmProxyWithCDN)) } } @@ -584,9 +584,9 @@ func (a *App) setupServer() error { a.prewarmFetcher.Mount("pypi", pypiProxyMounted) pypiProxyWithCDN := a.cdnMiddleware.Handler(pypiProxyMounted) if authEnabled { - a.app.All("/pypi/*", mwAuth, adaptor.HTTPHandler(pypiProxyWithCDN)) + a.app.All("/pypi/*", mwAuth, proxyHandler(pypiProxyWithCDN)) } else { - a.app.All("/pypi/*", adaptor.HTTPHandler(pypiProxyWithCDN)) + a.app.All("/pypi/*", proxyHandler(pypiProxyWithCDN)) } } diff --git a/pkg/app/detach.go b/pkg/app/detach.go new file mode 100644 index 0000000..8fa7164 --- /dev/null +++ b/pkg/app/detach.go @@ -0,0 +1,54 @@ +package app + +import ( + "net/http" + "net/url" + "strings" + + "github.com/gofiber/fiber/v3" + "github.com/gofiber/fiber/v3/middleware/adaptor" +) + +// proxyHandler mounts a net/http proxy handler on fiber with a request that owns its strings. +// The adaptor builds URL, Host, Method and header strings over fasthttp's request buffer, which the +// next request on the connection overwrites; the proxies hand these strings to background scans, +// singleflight keys and analytics that outlive the request. +func proxyHandler(h http.Handler) fiber.Handler { + return adaptor.HTTPHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + h.ServeHTTP(w, detachRequest(r)) + })) +} + +func detachRequest(r *http.Request) *http.Request { + d := r.Clone(r.Context()) + d.Method = strings.Clone(r.Method) + d.Proto = strings.Clone(r.Proto) + d.Host = strings.Clone(r.Host) + d.RequestURI = strings.Clone(r.RequestURI) + u := *r.URL + u.Scheme = strings.Clone(u.Scheme) + u.Host = strings.Clone(u.Host) + u.Path = strings.Clone(u.Path) + u.RawPath = strings.Clone(u.RawPath) + u.RawQuery = strings.Clone(u.RawQuery) + u.Fragment = strings.Clone(u.Fragment) + u.User = nil + if r.URL.User != nil { + if p, ok := r.URL.User.Password(); ok { + u.User = url.UserPassword(strings.Clone(r.URL.User.Username()), strings.Clone(p)) + } else { + u.User = url.User(strings.Clone(r.URL.User.Username())) + } + } + d.URL = &u + h := make(http.Header, len(r.Header)) + for k, vs := range r.Header { + cp := make([]string, len(vs)) + for i, v := range vs { + cp[i] = strings.Clone(v) + } + h[strings.Clone(k)] = cp + } + d.Header = h + return d +} diff --git a/pkg/app/detach_test.go b/pkg/app/detach_test.go new file mode 100644 index 0000000..2eb4d93 --- /dev/null +++ b/pkg/app/detach_test.go @@ -0,0 +1,37 @@ +package app + +import ( + "net/http" + "net/url" + "testing" + "unsafe" +) + +// The adaptor's strings point into a buffer fasthttp reuses; a detached request must not see later writes. +func TestDetachRequestSurvivesBufferReuse(t *testing.T) { + buf := []byte("GET/npm/lodash/-/lodash-1.0.0.tgzq=1proxy.lanBearer secret") + alias := func(from, to int) string { return unsafe.String(&buf[from], to-from) } // #nosec G103 -- reproduces the adaptor's aliasing + r := &http.Request{ + Method: alias(0, 3), + Host: alias(36, 45), + URL: &url.URL{Path: alias(3, 33), RawQuery: alias(33, 36), User: url.UserPassword(alias(36, 41), alias(52, 58))}, + Header: http.Header{"Authorization": {alias(45, 58)}}, + } + + d := detachRequest(r) + for i := range buf { + buf[i] = 'x' + } + + pw, _ := d.URL.User.Password() + got := []string{d.Method, d.Host, d.URL.Path, d.URL.RawQuery, d.Header.Get("Authorization"), d.URL.User.Username(), pw} + want := []string{"GET", "proxy.lan", "/npm/lodash/-/lodash-1.0.0.tgz", "q=1", "Bearer secret", "proxy", "secret"} + for i := range want { + if got[i] != want[i] { + t.Fatalf("field %d = %q after the buffer changed, want %q", i, got[i], want[i]) + } + } + if r.URL == d.URL { + t.Fatal("detached request shares the URL with the original") + } +}