Skip to content
Merged
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: 2 additions & 0 deletions aspnetcore/src/Interface/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,11 @@
var clientId = httpContext.User?.Claims.FirstOrDefault(claim => claim.Type == "clientId")?.Value;
var organizationId = httpContext.User?.Claims.FirstOrDefault(claim => claim.Type == "organizationid")?.Value;
var queryString = httpContext.Request.QueryString.HasValue ? httpContext.Request.QueryString.Value : "";
var xForwardedFor = httpContext.Request.Headers["X-Forwarded-For"].FirstOrDefault();

context.Set("CorrelationId", correlationId);
context.Set("ClientId", clientId);
context.Set("ClientIp", !string.IsNullOrEmpty(xForwardedFor) ? xForwardedFor : httpContext.Connection.RemoteIpAddress?.ToString());
Copy link

Copilot AI Aug 13, 2025

Choose a reason for hiding this comment

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

The X-Forwarded-For header can contain multiple IP addresses separated by commas (client, proxy1, proxy2, etc.). Using the entire header value could log internal proxy IPs or allow header spoofing. Consider extracting only the first IP address using xForwardedFor.Split(',')[0].Trim() to get the original client IP.

Suggested change
context.Set("ClientIp", !string.IsNullOrEmpty(xForwardedFor) ? xForwardedFor : httpContext.Connection.RemoteIpAddress?.ToString());
var clientIp = !string.IsNullOrEmpty(xForwardedFor)
? xForwardedFor.Split(',')[0].Trim()
: httpContext.Connection.RemoteIpAddress?.ToString();
context.Set("CorrelationId", correlationId);
context.Set("ClientId", clientId);
context.Set("ClientIp", clientIp);

Copilot uses AI. Check for mistakes.
Copy link

Copilot AI Aug 13, 2025

Choose a reason for hiding this comment

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

The X-Forwarded-For header can be easily spoofed by clients. If this application is not behind a trusted reverse proxy, consider validating that the request actually came through your expected proxy infrastructure before trusting this header, or use Connection.RemoteIpAddress as the primary source.

Suggested change
context.Set("ClientIp", !string.IsNullOrEmpty(xForwardedFor) ? xForwardedFor : httpContext.Connection.RemoteIpAddress?.ToString());
context.Set("ClientIp", httpContext.Connection.RemoteIpAddress?.ToString());

Copilot uses AI. Check for mistakes.
context.Set("OrganizationId", organizationId);
context.Set("QueryString", queryString);
});
Expand Down
Loading