-
Notifications
You must be signed in to change notification settings - Fork 2
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
some proxies may add port number to X-Forwarded-For header. #533
Conversation
WalkthroughThe recent update enhances the HTTP handler's ability to accurately determine a client's IP address. It now interprets the Changes
Poem
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (invoked as PR comments)
Additionally, you can add CodeRabbit Configration File (
|
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.
Review Status
Actionable comments generated: 1
Configuration used: CodeRabbit UI
Files selected for processing (2)
- xrayhttp/handler.go (1 hunks)
- xrayhttp/handler_test.go (1 hunks)
Additional comments: 2
xrayhttp/handler.go (1)
- 196-203: The
getHost
function correctly splits the host and port from a given string and returns just the host part if the split is successful. This is a useful utility function for handling IP addresses that may include port numbers.xrayhttp/handler_test.go (1)
- 684-716: The tests added for the
TestClientIP
function cover the new scenarios introduced by the changes inhandler.go
. They include tests for handlingX-Forwarded-For
headers with IPv6 addresses, multiple IP addresses, and IP addresses with ports. This ensures that theclientIP
function is properly tested for the new parsing logic.
// clientIP returns the client IP address from the request. | ||
// | ||
// If the request passes through a proxy, clientIP parses | ||
// the Forwarded header and the X-Forwarded-For header to | ||
// find the client IP address. | ||
// The clientIP always trusts the values of the Forwarded header | ||
// and the X-Forwarded-For header. Please note | ||
// that if these headers have been altered by an attacker, | ||
// there is a possibility of returning incorrect results. | ||
func clientIP(r *http.Request) (string, bool) { | ||
// Parse Forwarded header. | ||
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Forwarded | ||
forwarded, err := forwardedheader.Parse(r.Header.Values("Forwarded")) | ||
if err == nil && len(forwarded) > 0 { | ||
ip := forwarded[0].For.IP | ||
if ip.IsValid() { | ||
return ip.String(), true | ||
} | ||
} | ||
|
||
// Parse X-Forwarded-For header. | ||
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-For | ||
forwardedFor := r.Header.Get("X-Forwarded-For") | ||
if forwardedFor != "" { | ||
if idx := strings.IndexByte(forwardedFor, ','); idx > 0 { | ||
forwardedFor = forwardedFor[:idx] | ||
} | ||
return strings.TrimSpace(forwardedFor), true | ||
forwardedFor = strings.TrimSpace(forwardedFor) | ||
|
||
// some proxies may add port number to X-Forwarded-For header. | ||
// e.g. AWS ALB: https://docs.aws.amazon.com/elasticloadbalancing/latest/application/x-forwarded-headers.html | ||
return getHost(forwardedFor), true | ||
} | ||
ip, _, err := net.SplitHostPort(r.RemoteAddr) | ||
return getHost(r.RemoteAddr), false | ||
} |
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.
The clientIP
function has been updated to handle cases where the X-Forwarded-For
header includes port numbers. However, there is a potential issue with the getHost
function call at line 191. It assumes that the X-Forwarded-For
header will contain an IP address followed by a port, but this is not always the case. The getHost
function should handle cases where there is no port number present.
Additionally, there is no validation to ensure that the IP address extracted is a valid format before returning it. This could lead to potential security issues if the header is manipulated.
func clientIP(r *http.Request) (string, bool) {
// Parse Forwarded header.
forwarded, err := forwardedheader.Parse(r.Header.Values("Forwarded"))
if err == nil && len(forwarded) > 0 {
ip := forwarded[0].For.IP
if ip.IsValid() {
return ip.String(), true
}
}
// Parse X-Forwarded-For header.
forwardedFor := r.Header.Get("X-Forwarded-For")
if forwardedFor != "" {
if idx := strings.IndexByte(forwardedFor, ','); idx > 0 {
forwardedFor = forwardedFor[:idx]
}
forwardedFor = strings.TrimSpace(forwardedFor)
// some proxies may add port number to X-Forwarded-For header.
// e.g. AWS ALB: https://docs.aws.amazon.com/elasticloadbalancing/latest/application/x-forwarded-headers.html
- return getHost(forwardedFor), true
+ host, _ := getHost(forwardedFor)
+ if host == "" {
+ return "", false // or handle the error appropriately
+ }
+ return host, true
}
return getHost(r.RemoteAddr), false
}
Committable suggestion
❗ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.
// clientIP returns the client IP address from the request. | |
// | |
// If the request passes through a proxy, clientIP parses | |
// the Forwarded header and the X-Forwarded-For header to | |
// find the client IP address. | |
// The clientIP always trusts the values of the Forwarded header | |
// and the X-Forwarded-For header. Please note | |
// that if these headers have been altered by an attacker, | |
// there is a possibility of returning incorrect results. | |
func clientIP(r *http.Request) (string, bool) { | |
// Parse Forwarded header. | |
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Forwarded | |
forwarded, err := forwardedheader.Parse(r.Header.Values("Forwarded")) | |
if err == nil && len(forwarded) > 0 { | |
ip := forwarded[0].For.IP | |
if ip.IsValid() { | |
return ip.String(), true | |
} | |
} | |
// Parse X-Forwarded-For header. | |
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-For | |
forwardedFor := r.Header.Get("X-Forwarded-For") | |
if forwardedFor != "" { | |
if idx := strings.IndexByte(forwardedFor, ','); idx > 0 { | |
forwardedFor = forwardedFor[:idx] | |
} | |
return strings.TrimSpace(forwardedFor), true | |
forwardedFor = strings.TrimSpace(forwardedFor) | |
// some proxies may add port number to X-Forwarded-For header. | |
// e.g. AWS ALB: https://docs.aws.amazon.com/elasticloadbalancing/latest/application/x-forwarded-headers.html | |
return getHost(forwardedFor), true | |
} | |
ip, _, err := net.SplitHostPort(r.RemoteAddr) | |
return getHost(r.RemoteAddr), false | |
} | |
// clientIP returns the client IP address from the request. | |
// | |
// If the request passes through a proxy, clientIP parses | |
// the Forwarded header and the X-Forwarded-For header to | |
// find the client IP address. | |
// The clientIP always trusts the values of the Forwarded header | |
// and the X-Forwarded-For header. Please note | |
// that if these headers have been altered by an attacker, | |
// there is a possibility of returning incorrect results. | |
func clientIP(r *http.Request) (string, bool) { | |
// Parse Forwarded header. | |
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Forwarded | |
forwarded, err := forwardedheader.Parse(r.Header.Values("Forwarded")) | |
if err == nil && len(forwarded) > 0 { | |
ip := forwarded[0].For.IP | |
if ip.IsValid() { | |
return ip.String(), true | |
} | |
} | |
// Parse X-Forwarded-For header. | |
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-For | |
forwardedFor := r.Header.Get("X-Forwarded-For") | |
if forwardedFor != "" { | |
if idx := strings.IndexByte(forwardedFor, ','); idx > 0 { | |
forwardedFor = forwardedFor[:idx] | |
} | |
forwardedFor = strings.TrimSpace(forwardedFor) | |
// some proxies may add port number to X-Forwarded-For header. | |
// e.g. AWS ALB: https://docs.aws.amazon.com/elasticloadbalancing/latest/application/x-forwarded-headers.html | |
host, _ := getHost(forwardedFor) | |
if host == "" { | |
return "", false // or handle the error appropriately | |
} | |
return host, true | |
} | |
return getHost(r.RemoteAddr), false | |
} |
Summary by CodeRabbit
New Features
Bug Fixes
Tests