Skip to content

Commit 07e6695

Browse files
authored
cmd/anubis: set X-Real-Ip based on X-Forwarded-For (#63)
This triggers a SHAME release[0]. [0]: https://pridever.org/
1 parent a9777a3 commit 07e6695

File tree

6 files changed

+27
-1
lines changed

6 files changed

+27
-1
lines changed

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.14.0
1+
1.14.1

cmd/anubis/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ func main() {
214214
var h http.Handler
215215
h = mux
216216
h = internal.DefaultXRealIP(*debugXRealIPDefault, h)
217+
h = internal.XForwardedForToXRealIP(h)
217218

218219
srv := http.Server{Handler: h}
219220
listener, url := setupListener(*bindNetwork, *bind)

docs/docs/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
## [Unreleased]
1313

14+
## v1.14.1
15+
16+
Livia sas Junius: Echo 1
17+
18+
- Set the `X-Real-Ip` header based on the contents of `X-Forwarded-For`
19+
[#62](https://github.com/TecharoHQ/anubis/issues/62)
20+
1421
## v1.14.0
1522

1623
Livia sas Junius

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ require (
3434
github.com/prometheus/client_model v0.6.1 // indirect
3535
github.com/prometheus/common v0.62.0 // indirect
3636
github.com/prometheus/procfs v0.15.1 // indirect
37+
github.com/sebest/xff v0.0.0-20210106013422-671bd2870b3a // indirect
3738
golang.org/x/mod v0.24.0 // indirect
3839
golang.org/x/net v0.37.0 // indirect
3940
golang.org/x/sync v0.12.0 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ github.com/prometheus/common v0.62.0 h1:xasJaQlnWAeyHdUBeGjXmutelfJHWMRr+Fg4QszZ
5959
github.com/prometheus/common v0.62.0/go.mod h1:vyBcEuLSvWos9B1+CyL7JZ2up+uFzXhkqml0W5zIY1I=
6060
github.com/prometheus/procfs v0.15.1 h1:YagwOFzUgYfKKHX6Dr+sHT7km/hxC76UB0learggepc=
6161
github.com/prometheus/procfs v0.15.1/go.mod h1:fB45yRUv8NstnjriLhBQLuOUt+WW4BsoGhij/e3PBqk=
62+
github.com/sebest/xff v0.0.0-20210106013422-671bd2870b3a h1:iLcLb5Fwwz7g/DLK89F+uQBDeAhHhwdzB5fSlVdhGcM=
63+
github.com/sebest/xff v0.0.0-20210106013422-671bd2870b3a/go.mod h1:wozgYq9WEBQBaIJe4YZ0qTSFAMxmcwBhQH0fO0R34Z0=
6264
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
6365
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
6466
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=

internal/headers.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"net/http"
66

77
"github.com/TecharoHQ/anubis"
8+
"github.com/sebest/xff"
89
)
910

1011
// UnchangingCache sets the Cache-Control header to cache a response for 1 year if
@@ -33,3 +34,17 @@ func DefaultXRealIP(defaultIP string, next http.Handler) http.Handler {
3334
next.ServeHTTP(w, r)
3435
})
3536
}
37+
38+
// XForwardedForToXRealIP sets the X-Real-Ip header based on the contents
39+
// of the X-Forwarded-For header.
40+
func XForwardedForToXRealIP(next http.Handler) http.Handler {
41+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
42+
if xffHeader := r.Header.Get("X-Forwarded-For"); r.Header.Get("X-Real-Ip") == "" && xffHeader != "" {
43+
ip := xff.Parse(xffHeader)
44+
slog.Debug("setting x-real-ip", "val", ip)
45+
r.Header.Set("X-Real-Ip", ip)
46+
}
47+
48+
next.ServeHTTP(w, r)
49+
})
50+
}

0 commit comments

Comments
 (0)