プロキシの背後では、私有アドレスからの CF-Connecting-IP を信用する

このコミットが含まれているのは:
routersys committed 2026-11-15 11:35:47 +09:00
1 親 90a089d56f
コミット a32a404ee7
2ファイル変更
+12 -2

検索条件に一致するファイルはありません

+3 -2
ファイルの表示
@@ -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
}
+9
ファイルの表示
@@ -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)
}
}