Skip to content

Conversation

@sarkikos
Copy link
Collaborator

No description provided.

Copy link

Copilot AI left a comment

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 adds client IP address logging by extracting the IP from the X-Forwarded-For header when available, falling back to the connection's remote IP address. This is commonly needed when applications are behind reverse proxies or load balancers.

  • Extracts client IP from X-Forwarded-For header with fallback to connection remote IP
  • Adds ClientIp to the logging context

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.


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.

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 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.
@sarkikos sarkikos merged commit 73bb8f6 into devel Aug 13, 2025
2 checks passed
@sarkikos sarkikos deleted the fix-in-logging-set-clientip-from-header-x-forwarded-for branch August 13, 2025 05:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants