Skip to content

Commit 40afc13

Browse files
committed
fix(honeypot/naive): implement better IP parsing logic
Signed-off-by: Xe Iaso <me@xeiaso.net>
1 parent 122e4bc commit 40afc13

2 files changed

Lines changed: 7 additions & 3 deletions

File tree

internal/headers.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ func XForwardedForToXRealIP(next http.Handler) http.Handler {
100100
ip := xff.Parse(xffHeader)
101101
slog.Debug("setting X-Real-Ip from X-Forwarded-For", "to", ip, "x-forwarded-for", xffHeader)
102102
r.Header.Set("X-Real-Ip", ip)
103+
if addr, err := netip.ParseAddr(ip); err == nil {
104+
r = r.WithContext(context.WithValue(r.Context(), realIPKey{}, addr))
105+
}
103106
}
104107

105108
next.ServeHTTP(w, r)

internal/honeypot/naive/naive.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import (
66
"fmt"
77
"log/slog"
88
"math/rand/v2"
9+
"net"
910
"net/http"
11+
"net/netip"
1012
"time"
1113

1214
"github.com/TecharoHQ/anubis/internal"
@@ -152,9 +154,8 @@ func (i *Impl) ServeHTTP(w http.ResponseWriter, r *http.Request) {
152154

153155
realIP, _ := internal.RealIP(r)
154156
if !realIP.IsValid() {
155-
lg.Error("the real IP is somehow invalid, bad middleware stack?")
156-
http.Error(w, "The cake is a lie", http.StatusTeapot)
157-
return
157+
host, _, _ := net.SplitHostPort(r.RemoteAddr)
158+
realIP = netip.MustParseAddr(host)
158159
}
159160

160161
network, ok := internal.ClampIP(realIP)

0 commit comments

Comments
 (0)