Skip to content

🐛 bug: Add X-Real-IP protection to Forward and DomainForward variants#4261

Merged
ReneWerner87 merged 5 commits into
mainfrom
claude/update-balancerforward-variant
May 9, 2026
Merged

🐛 bug: Add X-Real-IP protection to Forward and DomainForward variants#4261
ReneWerner87 merged 5 commits into
mainfrom
claude/update-balancerforward-variant

Conversation

@Claude

@Claude Claude AI commented May 8, 2026

Copy link
Copy Markdown
Contributor

Description

Following PR #4260, only BalancerForward was automatically setting the X-Real-IP header for IP spoofing protection. The Forward and DomainForward variants left the header untouched, creating a security inconsistency where migrating from BalancerForward to other Forward variants would lose spoofing protection.

This PR adds automatic X-Real-IP header setting to Forward and DomainForward, 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 incoming X-Real-IP header with the actual client IP from c.IP(). This prevents malicious clients from forging their IP address.

  • Documentation Update:

    • Added Security section to /docs/middleware/proxy.md explaining the automatic X-Real-IP protection behavior
    • Documented which functions set X-Real-IP automatically vs. which require manual setting
  • Tests: Added comprehensive tests for Forward and DomainForward X-Real-IP protection:

    • Test_Proxy_Forward_OverwritesXRealIP
    • Test_Proxy_DomainForward_OverwritesXRealIP
  • Examples:

// Before: Forward didn't protect against IP spoofing
app.Use(proxy.Forward("http://backend:8080"))
// A malicious client could set X-Real-IP: 1.2.3.4 and it would be passed through

// After: Forward automatically protects against IP spoofing
app.Use(proxy.Forward("http://backend:8080"))
// Any spoofed X-Real-IP header is overwritten with the real client IP

Type of change

  • Enhancement (improvement to existing features and functionality)
  • Documentation update (changes to documentation)
  • Code consistency (non-breaking change which improves code reliability and robustness)

Checklist

  • Conducted a self-review of the code and provided comments for complex or critical parts.
  • Updated the documentation in the /docs/ directory for Fiber's documentation.
  • Added or updated unit tests to validate the effectiveness of the changes or new features.
  • Ensured that new and existing unit tests pass locally with the changes.
  • Verified that any new dependencies are essential and have been agreed upon by the maintainers/community.
  • Aimed for optimal performance with minimal allocations in the new code.

@codecov

codecov Bot commented May 8, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.18%. Comparing base (1403cc8) to head (a346c15).
⚠️ Report is 2 commits behind head on main.

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           
Flag Coverage Δ
unittests 91.18% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@gaby gaby marked this pull request as ready for review May 8, 2026 12:54
@gaby gaby requested a review from a team as a code owner May 8, 2026 12:54

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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-IP overwriting to Forward and DomainForward (matching BalancerForward behavior).
  • Add unit tests covering X-Real-IP overwrite behavior for Forward and DomainForward.
  • Document the automatic X-Real-IP behavior 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).

Comment thread middleware/proxy/proxy_test.go Outdated
Comment thread middleware/proxy/proxy_test.go
- 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.
@ReneWerner87 ReneWerner87 merged commit 437fef9 into main May 9, 2026
21 checks passed
@welcome

welcome Bot commented May 9, 2026

Copy link
Copy Markdown

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

@ReneWerner87 ReneWerner87 deleted the claude/update-balancerforward-variant branch May 9, 2026 11:15
@github-project-automation github-project-automation Bot moved this to Done in v3 May 9, 2026
@gaby

gaby commented May 9, 2026

Copy link
Copy Markdown
Member

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

@ReneWerner87 🤣

@ReneWerner87 ReneWerner87 modified the milestones: v3, v3.3.0 May 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

4 participants