Skip to content
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

ClientIP() using X-Forwarded-For and X-Real-Ip should be opt-in #1684

Closed
wodim opened this issue Dec 5, 2018 · 3 comments
Closed

ClientIP() using X-Forwarded-For and X-Real-Ip should be opt-in #1684

wodim opened this issue Dec 5, 2018 · 3 comments

Comments

@wodim
Copy link

wodim commented Dec 5, 2018

ClientIP() using X-Forwarded-For and X-Real-Ip by default without any kind of warning is appalling security-wise.

It is trivial for an attacker to spoof any IP address if the app is listening directly on a public port without a reverse proxy or if the reverse proxy is not properly configured. For example, if the reverse proxy is configured to use X-Real-Ip, it will seemingly work correctly, but X-Forwarded-For takes precedence so the remote IP address can still be spoofed.

@billyplus
Copy link

There is ForwardedByClientIP bool to check if clientip is forwarded.

r := gin.New()
r.ForwardedByClientIP = false
func (c *Context) ClientIP() string {
	if c.engine.ForwardedByClientIP {
		clientIP := c.requestHeader("X-Forwarded-For")
		clientIP = strings.TrimSpace(strings.Split(clientIP, ",")[0])
		if clientIP == "" {
			clientIP = strings.TrimSpace(c.requestHeader("X-Real-Ip"))
		}
		if clientIP != "" {
			return clientIP
		}
	}

@sorenisanerd
Copy link
Contributor

#2474 helps a bit. It's still opt-out, but you can specify trusted proxies.

@sorenisanerd
Copy link
Contributor

sorenisanerd commented Apr 7, 2021

Not fixed, ffs.

gin/gin.go

Lines 161 to 162 in bfc8ca2

RemoteIPHeaders: []string{"X-Forwarded-For", "X-Real-IP"},
TrustedProxies: []string{"0.0.0.0/0"},

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

No branches or pull requests

4 participants