From a32a404ee778bb56e7a45a9fbd69bbfe5a9ad5a9 Mon Sep 17 00:00:00 2001 From: routersys Date: Sun, 15 Nov 2026 11:35:46 +0900 Subject: [PATCH] =?UTF-8?q?=E3=83=97=E3=83=AD=E3=82=AD=E3=82=B7=E3=81=AE?= =?UTF-8?q?=E8=83=8C=E5=BE=8C=E3=81=A7=E3=81=AF=E3=80=81=E7=A7=81=E6=9C=89?= =?UTF-8?q?=E3=82=A2=E3=83=89=E3=83=AC=E3=82=B9=E3=81=8B=E3=82=89=E3=81=AE?= =?UTF-8?q?=20CF-Connecting-IP=20=E3=82=92=E4=BF=A1=E7=94=A8=E3=81=99?= =?UTF-8?q?=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/site/middleware.go | 5 +++-- internal/site/site_test.go | 9 +++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) 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) + } +}