diff --git a/internal/site/middleware.go b/internal/site/middleware.go index 938429c..0d95b4e 100644 --- a/internal/site/middleware.go +++ b/internal/site/middleware.go @@ -77,14 +77,15 @@ func (s *Server) hostAllowed(host string) bool { func isStatic(p string) bool { return len(p) >= 8 && p[:8] == "/static/" } // clientKey names the visitor for rate limiting. The address Cloudflare saw is -// trusted only when the request comes from this machine (the tunnel). +// trusted only when the request comes from this machine or a private address +// (the tunnel, or the proxy in front of the container). func clientKey(r *http.Request) string { host, _, err := net.SplitHostPort(r.RemoteAddr) if err != nil { host = r.RemoteAddr } ip := net.ParseIP(host) - if ip != nil && ip.IsLoopback() { + if ip != nil && (ip.IsLoopback() || ip.IsPrivate()) { if fwd := net.ParseIP(r.Header.Get("CF-Connecting-IP")); fwd != nil { ip = fwd } diff --git a/internal/site/site_test.go b/internal/site/site_test.go index 14a336b..82d8300 100644 --- a/internal/site/site_test.go +++ b/internal/site/site_test.go @@ -410,3 +410,12 @@ func TestEveryFontFileComesWithItsLicenseText(t *testing.T) { } } } + +func TestClientKeyTrustsTheProxyOnAPrivateAddress(t *testing.T) { + r := httptest.NewRequest("GET", "/", nil) + r.RemoteAddr = "172.18.0.5:40000" + r.Header.Set("CF-Connecting-IP", "198.51.100.7") + if got := clientKey(r); got != "198.51.100.7" { + t.Errorf("behind the proxy: %q", got) + } +}