Honour forwarded headers only from trusted proxies (closes #259)
check / check (push) Failing after 6s
check / check (push) Failing after 6s
A new ClientIP middleware sets request.RemoteAddr to the client's address before the request is logged. X-Forwarded-For and X-Real-IP count only when the peer is in TRUSTED_PROXIES, which defaults to the RFC 1918 ranges. The login rate limit, the session's IP address and hostname, and the request log all use that address. Model: opus-5-5
This commit is contained in:
1 parent
bc8aa6bb96
commit
51d9d774fd
10 files changed
+519
-42
No files matched your search
@@ -4281,6 +4281,67 @@ func TestLoginRateLimitExceeded(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoginRateLimitIgnoresForwardedHeaders(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
// The test client connects from 127.0.0.1, which is outside the
|
||||
// default trusted proxies, so the server must ignore these headers.
|
||||
tests := []struct {
|
||||
name string
|
||||
header string
|
||||
}{
|
||||
{name: "X-Forwarded-For", header: "X-Forwarded-For"},
|
||||
{name: "X-Real-IP", header: "X-Real-IP"},
|
||||
}
|
||||
|
||||
loginBody, err := json.Marshal(map[string]string{
|
||||
nickKey: "nosuchuser", passwordKey: "doesnotmatter",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tserver := newTestServer(t)
|
||||
|
||||
// The default burst is 5 attempts, so the sixth gets 429
|
||||
// although each claims a new address.
|
||||
var status int
|
||||
|
||||
for attempt := range 6 {
|
||||
request, rErr := http.NewRequestWithContext(
|
||||
t.Context(), http.MethodPost,
|
||||
tserver.url("/api/v1/login"),
|
||||
bytes.NewReader(loginBody),
|
||||
)
|
||||
if rErr != nil {
|
||||
t.Fatal(rErr)
|
||||
}
|
||||
|
||||
request.Header.Set("Content-Type", "application/json")
|
||||
request.Header.Set(
|
||||
test.header, fmt.Sprintf("203.0.113.%d", attempt+1),
|
||||
)
|
||||
|
||||
resp, dErr := http.DefaultClient.Do(request)
|
||||
if dErr != nil {
|
||||
t.Fatal(dErr)
|
||||
}
|
||||
|
||||
status = resp.StatusCode
|
||||
_ = resp.Body.Close()
|
||||
}
|
||||
|
||||
if status != http.StatusTooManyRequests {
|
||||
t.Fatalf("sixth attempt got %d, want 429", status)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoginRateLimitAllowsNormalUse(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
|
||||
Reference in new issue
Block a user