fix: Sentry.Tunnel overwrites the X-Forwarded-For#4375
Merged
jamescrosswell merged 5 commits intomainfrom Jul 25, 2025
Merged
Conversation
bruno-garcia
approved these changes
Jul 24, 2025
Flash0ver
reviewed
Jul 24, 2025
|
|
||
| return string.IsNullOrEmpty(existingForwardedFor) | ||
| ? clientIp | ||
| : $"{existingForwardedFor}, {clientIp}"; |
There was a problem hiding this comment.
Bug: Header Malformation with Empty Values
The CreateXForwardedForHeader method malforms X-Forwarded-For headers when the incoming header contains only empty string values. This occurs because StringValues.ToString() returns a comma-only string (e.g., ',') for empty values, which string.IsNullOrEmpty() incorrectly evaluates as non-empty. This results in malformed headers like ', 192.168.1.1' or ', , 192.168.1.1' instead of only the client IP.
Collaborator
Author
There was a problem hiding this comment.
In context:
sentry-dotnet/src/Sentry.AspNetCore/SentryTunnelMiddleware.cs
Lines 134 to 141 in b331224
So existingForwardedFor will never be empty when this codepath is executed.
This was referenced Jul 28, 2025
This was referenced Aug 27, 2025
This was referenced Sep 8, 2025
This was referenced Sep 15, 2025
This was referenced Oct 2, 2025
This was referenced Oct 15, 2025
This was referenced Nov 3, 2025
This was referenced Dec 8, 2025
This was referenced Dec 15, 2025
This was referenced Jan 18, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Resolves #1309:
Sentry.Tunneldoesn't includeX-Forwarded-To#1309Summary
The
X-Forwarded-Forheader is supposed to preserve a chain of IP addresses that the message was forwarded via (see docs).So for example it might contain:
Which is a specific instance of:
Previously, the
SentryTunnelMiddlewarewas overwriting this value so that it only included the IP address fromDefaultHttpContext.Connection.RemoteIpAddress... which was fine if the request didn't include anyX-Forwarded-Forbut, if the request was proxied (e.g. by CloudFlare) then effectively by doing that we delete the upstream IP addresses so we no longer know where the message came from (or the User's real IP address).Solution
This PR changes the behaviour of the
SentryTunnelMiddlewareso that theRemoteIpAddressis appended to theX-Forwarded-Forso that the client IP address is retained in the left most address from the list in theX-Forwarded-Forheader.