Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/middleware/proxy.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ app.Use(proxy.Balancer(proxy.Config{
"http://localhost:3003",
},
ModifyRequest: func(c fiber.Ctx) error {
c.Request().Header.Add("X-Real-IP", c.IP())
c.Request().Header.Set("X-Real-IP", c.IP())
return nil
},
ModifyResponse: func(c fiber.Ctx) error {
Expand Down
2 changes: 1 addition & 1 deletion middleware/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ func BalancerForward(servers []string, clients ...*fasthttp.Client) fiber.Handle
if !strings.HasPrefix(server, "http") {
server = "http://" + server
}
c.Request().Header.Add("X-Real-IP", c.IP())
c.Request().Header.Set("X-Real-IP", c.IP())
return Do(c, server+c.OriginalURL(), clients...)
}
}
26 changes: 26 additions & 0 deletions middleware/proxy/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -985,6 +985,32 @@ func Test_Proxy_Balancer_Forward_Local(t *testing.T) {
require.Equal(t, "forwarded", string(b))
}

func Test_Proxy_Balancer_Forward_OverwritesXRealIP(t *testing.T) {
t.Parallel()

const (
spoofedIP = "10.0.0.1"
appTestClientIP = "0.0.0.0"
)

_, addr := createProxyTestServerIPv4(t, func(c fiber.Ctx) error {
value := c.Get("X-Real-IP")
require.Equal(t, appTestClientIP, value)
require.NotEqual(t, spoofedIP, value)
return c.SendStatus(fiber.StatusOK)
})

app := fiber.New()
app.Use(BalancerForward([]string{addr}))

req := httptest.NewRequest(fiber.MethodGet, "/", http.NoBody)
req.Header.Set("X-Real-IP", spoofedIP)

resp, err := app.Test(req)
require.NoError(t, err)
require.Equal(t, fiber.StatusOK, resp.StatusCode)
}

func Test_Proxy_Immutable(t *testing.T) {
t.Parallel()

Expand Down
Loading