Files
gohoarder/pkg/app/detach_test.go
T
lukaszraczylo 48bd654839 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.
2026-11-24 02:11:32 +00:00

38 lines
1.2 KiB
Go

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")
}
}