Skip to content

Commit e8edd53

Browse files
committed
Use remote IP if ProxyHeader is not set
1 parent afa3554 commit e8edd53

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

middleware/ip_address.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"net"
55
"slices"
66

7+
"github.com/ProjectLighthouseCAU/heimdall/config"
78
"github.com/gofiber/fiber/v2"
89
)
910

@@ -13,6 +14,9 @@ import (
1314
func AllowLoopbackAndPrivateIPs() fiber.Handler {
1415
return func(c *fiber.Ctx) error {
1516
clientIp := net.ParseIP(c.IP())
17+
if _, ok := c.GetReqHeaders()[config.ProxyHeader]; !ok {
18+
clientIp = c.Context().RemoteIP() // use remote IP if the ProxyHeader is not set
19+
}
1620
if clientIp.IsPrivate() || clientIp.IsLoopback() {
1721
return c.Next()
1822
}
@@ -23,6 +27,9 @@ func AllowLoopbackAndPrivateIPs() fiber.Handler {
2327
func AllowIPs(ips []net.IP) fiber.Handler {
2428
return func(c *fiber.Ctx) error {
2529
clientIp := net.ParseIP(c.IP())
30+
if _, ok := c.GetReqHeaders()[config.ProxyHeader]; !ok {
31+
clientIp = c.Context().RemoteIP() // use remote IP if the ProxyHeader is not set
32+
}
2633
if slices.ContainsFunc(ips, func(ip net.IP) bool {
2734
return slices.Equal(ip, clientIp)
2835
}) {
@@ -35,6 +42,9 @@ func AllowIPs(ips []net.IP) fiber.Handler {
3542
func AllowLoopbackAndPrivateIPsAnd(ips []net.IP) fiber.Handler {
3643
return func(c *fiber.Ctx) error {
3744
clientIp := net.ParseIP(c.IP())
45+
if _, ok := c.GetReqHeaders()[config.ProxyHeader]; !ok {
46+
clientIp = c.Context().RemoteIP() // use remote IP if the ProxyHeader is not set
47+
}
3848
if clientIp.IsPrivate() || clientIp.IsLoopback() {
3949
return c.Next()
4050
}

0 commit comments

Comments
 (0)