-
-
Notifications
You must be signed in to change notification settings - Fork 223
fix: Sentry.Tunnel overwrites the X-Forwarded-For #4375
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
66c34a5
fix: Sentry.Tunnel overwrites the X-Forwarded-For
jamescrosswell db3b9ec
Update CHANGELOG.md
jamescrosswell 27a2cd7
Merge branch 'main' into x-forward-for
jamescrosswell f413b79
Tidy tests
jamescrosswell b331224
Review feedback
jamescrosswell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.TestHost; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
|
@@ -9,6 +11,7 @@ public class IntegrationsTests : IDisposable | |
private readonly TestServer _server; | ||
private HttpClient _httpClient; | ||
private MockHttpMessageHandler _httpMessageHandler; | ||
private const string FakeRequestIp = "192.168.200.200"; | ||
|
||
public IntegrationsTests() | ||
{ | ||
|
@@ -22,7 +25,16 @@ public IntegrationsTests() | |
factory.CreateClient(Arg.Any<string>()).Returns(_httpClient); | ||
s.AddSingleton(factory); | ||
}) | ||
.Configure(app => { app.UseSentryTunneling(); }); | ||
.Configure(app => | ||
{ | ||
app.Use((context, next) => | ||
{ | ||
// The context doesn't get sent by TestServer automatically... so we fake a remote request here | ||
context.Connection.RemoteIpAddress = IPAddress.Parse(FakeRequestIp); | ||
return next(); | ||
}); | ||
app.UseSentryTunneling(); | ||
}); | ||
_server = new TestServer(builder); | ||
} | ||
|
||
|
@@ -35,9 +47,11 @@ public async Task TunnelMiddleware_CanForwardValidEnvelope(string host) | |
var requestMessage = new HttpRequestMessage(new HttpMethod("POST"), "/tunnel") | ||
{ | ||
Content = new StringContent( | ||
@"{""sent_at"":""2021-01-01T00:00:00.000Z"",""sdk"":{""name"":""sentry.javascript.browser"",""version"":""6.8.0""},""dsn"":""https://dns@" + host + @"/1""} | ||
{""type"":""session""} | ||
{""sid"":""fda00e933162466c849962eaea0cfaff""}") | ||
$$""" | ||
{"sent_at":"2021-01-01T00:00:00.000Z","sdk":{"name":"sentry.javascript.browser","version":"6.8.0"},"dsn":"https://dns@{{host}}/1"} | ||
{"type":"session"} | ||
{"sid":"fda00e933162466c849962eaea0cfaff"} | ||
""") | ||
}; | ||
await _server.CreateClient().SendAsync(requestMessage); | ||
|
||
|
@@ -49,9 +63,12 @@ public async Task TunnelMiddleware_DoesNotForwardEnvelopeWithoutDsn() | |
{ | ||
var requestMessage = new HttpRequestMessage(new HttpMethod("POST"), "/tunnel") | ||
{ | ||
Content = new StringContent(@"{} | ||
{""type"":""session""} | ||
{""sid"":""fda00e933162466c849962eaea0cfaff""}") | ||
Content = new StringContent( | ||
""" | ||
{} | ||
{"type":"session"} | ||
{"sid":"fda00e933162466c849962eaea0cfaff"} | ||
""") | ||
}; | ||
await _server.CreateClient().SendAsync(requestMessage); | ||
|
||
|
@@ -63,9 +80,11 @@ public async Task TunnelMiddleware_DoesNotForwardEnvelopeToArbitraryHost() | |
{ | ||
var requestMessage = new HttpRequestMessage(new HttpMethod("POST"), "/tunnel"); | ||
requestMessage.Content = new StringContent( | ||
@"{""sent_at"":""2021-01-01T00:00:00.000Z"",""sdk"":{""name"":""sentry.javascript.browser"",""version"":""6.8.0""},""dsn"":""https://[email protected]/1""} | ||
{""type"":""session""} | ||
{""sid"":""fda00e933162466c849962eaea0cfaff""}"); | ||
""" | ||
{"sent_at":"2021-01-01T00:00:00.000Z","sdk":{"name":"sentry.javascript.browser","version":"6.8.0"},"dsn":"https://[email protected]/1"} | ||
{"type":"session"} | ||
{"sid":"fda00e933162466c849962eaea0cfaff"} | ||
"""); | ||
await _server.CreateClient().SendAsync(requestMessage); | ||
|
||
Assert.Equal(0, _httpMessageHandler.NumberOfCalls); | ||
|
@@ -77,13 +96,46 @@ public async Task TunnelMiddleware_CanForwardEnvelopeToWhiteListedHost() | |
var requestMessage = new HttpRequestMessage(new HttpMethod("POST"), "/tunnel") | ||
{ | ||
Content = new StringContent( | ||
@"{""sent_at"":""2021-01-01T00:00:00.000Z"",""sdk"":{""name"":""sentry.javascript.browser"",""version"":""6.8.0""},""dsn"":""https://[email protected]/1""} | ||
{""type"":""session""} | ||
{""sid"":""fda00e933162466c849962eaea0cfaff""}") | ||
""" | ||
{"sent_at":"2021-01-01T00:00:00.000Z","sdk":{"name":"sentry.javascript.browser","version":"6.8.0"},"dsn":"https://[email protected]/1"} | ||
{"type":"session"} | ||
{"sid":"fda00e933162466c849962eaea0cfaff"} | ||
""") | ||
}; | ||
await _server.CreateClient().SendAsync(requestMessage); | ||
|
||
Assert.Equal(1, _httpMessageHandler.NumberOfCalls); | ||
} | ||
|
||
[Fact] | ||
public async Task TunnelMiddleware_XForwardedFor_RetainsOriginIp() | ||
{ | ||
// Arrange: Create a request with X-Forwarded-For header | ||
var requestMessage = new HttpRequestMessage(new HttpMethod("POST"), "/tunnel") | ||
{ | ||
Content = new StringContent( | ||
""" | ||
{"sent_at":"2021-01-01T00:00:00.000Z","sdk":{"name":"sentry.javascript.browser","version":"6.8.0"},"dsn":"https://[email protected]/1"} | ||
{"type":"session"} | ||
{"sid":"fda00e933162466c849962eaea0cfaff"} | ||
""") | ||
}; | ||
const string originalForwardedFor = "192.168.1.100, 10.0.0.1"; | ||
requestMessage.Headers.Add("X-Forwarded-For", originalForwardedFor); | ||
|
||
// Act | ||
await _server.CreateClient().SendAsync(requestMessage); | ||
|
||
// Assert | ||
Assert.Equal(1, _httpMessageHandler.NumberOfCalls); | ||
|
||
var forwardedRequest = _httpMessageHandler.LastRequest; | ||
Assert.NotNull(forwardedRequest); | ||
|
||
Assert.True(forwardedRequest.Headers.Contains("X-Forwarded-For")); | ||
var forwardedForHeader = forwardedRequest.Headers.GetValues("X-Forwarded-For").FirstOrDefault(); | ||
Assert.NotNull(forwardedForHeader); | ||
forwardedForHeader.Should().Be($"{originalForwardedFor}, {FakeRequestIp}"); | ||
} | ||
|
||
public void Dispose() | ||
|
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: Header Malformation with Empty Values
The
CreateXForwardedForHeader
method malformsX-Forwarded-For
headers when the incoming header contains only empty string values. This occurs becauseStringValues.ToString()
returns a comma-only string (e.g., ',') for empty values, whichstring.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.Locations (1)
src/Sentry.AspNetCore/SentryTunnelMiddleware.cs#L133-L141
Fix in Cursor • Fix in Web
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.