-
Notifications
You must be signed in to change notification settings - Fork 8.1k
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
set engine.TrustedProxies For items that don't use gin.RUN #2692
Conversation
The failed is not my reason https://travis-ci.org/github/gin-gonic/gin/jobs/767120791 |
Codecov Report
@@ Coverage Diff @@
## master #2692 +/- ##
==========================================
+ Coverage 98.68% 98.69% +0.01%
==========================================
Files 41 41
Lines 2054 2070 +16
==========================================
+ Hits 2027 2043 +16
Misses 15 15
Partials 12 12
Continue to review full report at Codecov.
|
@@ -366,6 +366,23 @@ func (engine *Engine) prepareTrustedCIDRs() ([]*net.IPNet, error) { | |||
return cidr, nil | |||
} | |||
|
|||
// SetTrustedProxies set Engine.TrustedProxies | |||
func (engine *Engine) SetTrustedProxies(trustedProxies []string) error { |
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.
please add some unit test, thanks!
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.
done
assert.Equal(t, "20.20.20.20", c.ClientIP()) | ||
|
||
// Use hostname that resolves to all the proxies | ||
c.engine.TrustedProxies = []string{"foo"} | ||
resetTrustedCIDRs(c) | ||
_ = c.engine.SetTrustedProxies([]string{"foo"}) | ||
assert.Equal(t, "40.40.40.40", c.ClientIP()) |
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.
I passed the previous unit tests, but I don't understand why the foo
bar
bar
would expect such a result
@@ -326,11 +326,11 @@ func iterate(path, method string, routes RoutesInfo, root *node) RoutesInfo { | |||
func (engine *Engine) Run(addr ...string) (err error) { | |||
defer func() { debugPrintError(err) }() | |||
|
|||
trustedCIDRs, err := engine.prepareTrustedCIDRs() | |||
err = engine.parseTrustedProxies() |
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.
Why not add this to all Run*
methods too?
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.
Because the other RUN*
didn't exist before.
My core idea was to be able to dynamically adjust the TurstedProxies.
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.
TrustedProxies is set with []string{"0.0.0.0/0"} by default, you'd better give trustedCIDRs a default value. So we don't need to call SetTrustedProxies when we don't need to change it. |
I haven't changed any of the previous logic, this is a PR that avoids #2675 bug in a new way. As for what you said, |
Your code is used to set custom TrustedProxies when don't use gin.Run. func New() *Engine {
...
engine.trustedCIDRs = []*net.IPNet{{IP: net.IP{0x0, 0x0, 0x0, 0x0}, Mask: net.IPMask{0x0, 0x0, 0x0, 0x0}}}
...
} Default value of trustedCIDRs is missing when gin.New() called, TrustedProxies is set to []string{"0.0.0.0/0"}, which means trustedCIDRs should also set a default value matchs TrustedProxies. |
@yiranzai what is the status on this change? I would love to use it. |
@evanfuller I don't know. @thinkerou @estroz reviewer? What else do I need to do? |
The easiest way: |
When this fix is planned for release? |
* Support Google App Engine * Output all request headers * PGLET_TRUSTED_PROXIES * Display remoteIP and trusted proxies * Temporary fix for content.ClientIP() gin-gonic/gin#2692 * Principal.IP fixed * Do not delete page if no clients connected * Support batched get
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.
lgtm, thanks!
This fix has 2 problems:
|
Is there any plan to release this change? |
Hello,
I couldn't agree more. Our software have been bumped to v1.7 because we follow security advisories, and you published GHSA-h395-qcrw-5vmq. But at this time, without the release of Thanks, |
Co-authored-by: Bo-Yi Wu <[email protected]>
…#2692) Co-authored-by: Bo-Yi Wu <[email protected]> (cherry picked from commit b5ca989)
…#2692) Co-authored-by: Bo-Yi Wu <[email protected]> (cherry picked from commit b5ca989)
…#2692) Co-authored-by: Bo-Yi Wu <[email protected]> (cherry picked from commit b5ca989)
…#2692) Co-authored-by: Bo-Yi Wu <[email protected]> (cherry picked from commit b5ca989)
Co-authored-by: Bo-Yi Wu <[email protected]>
…#2692) Co-authored-by: Bo-Yi Wu <[email protected]> (cherry picked from commit b5ca989)
Co-authored-by: Bo-Yi Wu <[email protected]>
Co-authored-by: Bo-Yi Wu <[email protected]>
gin 1.7.0 added a feature to accept/refuse proxy headers depending on the remote IP of the TCP connection. A fix of a data race in gin 1.7.1 made that feature inaccessible unless gin is started through gin.Run(), which we don't do in the fabric. A PR to fix this has not been approved for over a month (gin-gonic/gin#2692). Hence I implemented similar logic in our own helper, and consolidated with an existing helper (that we used in addition to the gin-specific functionality!). In addition, the client IP is cached in the gin context in order to improve performance (multiple middleware handlers use the client IP for logging, usage, geo lookups, redirect, etc.)
#2632
#2675
#2675 causes the
engine.prepareTrustedCIDRS
to never execute ifgin
is not started viagin.RUN()
.this pr set
engine.TrustedProxies
and executeengine.prepareTrustedCIDRS
For items that don't usegin.RUN
Our project uses
http.Handler
to start the service instead ofgin.Run