Skip to content

Commit af5e796

Browse files
committed
consolidate IF checks
1 parent 38609d7 commit af5e796

1 file changed

Lines changed: 6 additions & 10 deletions

File tree

request/real_ip.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,28 +28,24 @@ func RealIP(r *http.Request) string {
2828
xffs := strings.Split(fw, ",")
2929

3030
// Get depth from config, default to 0 (first IP in chain)
31-
depth := 0
31+
var depth int
3232
if Global != nil {
3333
depth = Global().HttpServerOptions.XFFDepth
3434
}
3535

36+
// The following check for invalid depth configs.
3637
// It's more secure to return empty if depth is invalid.
37-
// Returning the first IP in the case of an incorrect depth is a security risk.
38-
// and a buried failure.
39-
if depth < 0 {
40-
return ""
41-
}
42-
43-
// If depth exceeds available IPs, return empty
44-
if depth > len(xffs) {
38+
// Defaulting to an IP from the request would be
39+
// burying a configuration failure.
40+
if depth < 0 || depth > len(xffs) {
4541
return ""
4642
}
4743

4844
// Choose the appropriate IP based on depth
4945
// depth=0 means first IP (leftmost), depth=1 means last IP, depth=2 means second to last, etc.
5046
// Negative depth is invalid and treated same as 0/unset.
5147
var ip string
52-
if depth <= 0 {
48+
if depth == 0 {
5349
ip = strings.TrimSpace(xffs[0])
5450
} else {
5551
ip = strings.TrimSpace(xffs[len(xffs)-depth])

0 commit comments

Comments
 (0)