🐛 bug: Add X-Real-IP protection to Forward and DomainForward variants#4261
Conversation
Agent-Logs-Url: https://github.com/gofiber/fiber/sessions/41de633c-2c4a-4c0d-8ee6-1c286b1bb7ca Co-authored-by: gaby <835733+gaby@users.noreply.github.com>
Agent-Logs-Url: https://github.com/gofiber/fiber/sessions/41de633c-2c4a-4c0d-8ee6-1c286b1bb7ca Co-authored-by: gaby <835733+gaby@users.noreply.github.com>
Agent-Logs-Url: https://github.com/gofiber/fiber/sessions/41de633c-2c4a-4c0d-8ee6-1c286b1bb7ca Co-authored-by: gaby <835733+gaby@users.noreply.github.com>
Agent-Logs-Url: https://github.com/gofiber/fiber/sessions/41de633c-2c4a-4c0d-8ee6-1c286b1bb7ca Co-authored-by: gaby <835733+gaby@users.noreply.github.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #4261 +/- ##
=======================================
Coverage 91.18% 91.18%
=======================================
Files 129 129
Lines 12749 12757 +8
=======================================
+ Hits 11625 11633 +8
Misses 709 709
Partials 415 415
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull request overview
This PR closes a security inconsistency in the proxy middleware by ensuring all “Forward” variants consistently prevent client X-Real-IP spoofing by overwriting the header with c.IP() before proxying upstream.
Changes:
- Add automatic
X-Real-IPoverwriting toForwardandDomainForward(matchingBalancerForwardbehavior). - Add unit tests covering X-Real-IP overwrite behavior for
ForwardandDomainForward. - Document the automatic
X-Real-IPbehavior and clarify which proxy helpers do/don’t set it.
Reviewed changes
Copilot reviewed 13 out of 13 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| prefork.go | Formatting-only change (multiline append call). |
| middleware/proxy/proxy.go | Set X-Real-IP from c.IP() in Forward and DomainForward for spoofing protection consistency. |
| middleware/proxy/proxy_test.go | Add tests asserting spoofed X-Real-IP is overwritten for Forward and DomainForward. |
| middleware/logger/default_logger.go | Formatting-only change (multiline fmt.Fprintf). |
| middleware/envvar/envvar_test.go | Formatting-only change (multiline json.Marshal). |
| middleware/compress/compress.go | Formatting-only change (multiline compression handler calls). |
| helpers.go | Formatting-only change (multiline append). |
| helpers_test.go | Formatting-only change (multiline require.Equal). |
| domain_test.go | Formatting-only change (multiline .Get(...) registrations). |
| docs/middleware/proxy.md | Add Security section documenting automatic X-Real-IP overwrite behavior and guidance for other proxy helpers. |
| ctx_test.go | Formatting-only changes (multiline assertions / route registrations). |
| client/request_test.go | Formatting-only changes (multiline helper calls / SendString). |
| client/client_test.go | Formatting-only changes (multiline SendString). |
- Note in the Security section that DomainForward only rewrites X-Real-IP when the request host matches the configured hostname. - Replace hard-coded "0.0.0.0" with net.IPv4zero.String() and drop the redundant NotEqual assertion in the new Forward/DomainForward tests.
|
Congrats on merging your first pull request! 🎉 We here at Fiber are proud of you! If you need help or want to chat with us, join us on Discord https://gofiber.io/discord |
|
Description
Following PR #4260, only
BalancerForwardwas automatically setting theX-Real-IPheader for IP spoofing protection. TheForwardandDomainForwardvariants left the header untouched, creating a security inconsistency where migrating fromBalancerForwardto other Forward variants would lose spoofing protection.This PR adds automatic
X-Real-IPheader setting toForwardandDomainForward, ensuring all three Forward variants consistently protect against IP spoofing attacks.Changes introduced
Security Fix: All Forward variants (
Forward,DomainForward,BalancerForward) now automatically overwrite any incomingX-Real-IPheader with the actual client IP fromc.IP(). This prevents malicious clients from forging their IP address.Documentation Update:
/docs/middleware/proxy.mdexplaining the automatic X-Real-IP protection behaviorTests: Added comprehensive tests for Forward and DomainForward X-Real-IP protection:
Test_Proxy_Forward_OverwritesXRealIPTest_Proxy_DomainForward_OverwritesXRealIPExamples:
Type of change
Checklist
/docs/directory for Fiber's documentation.