set engine.TrustedProxies For items that don't use gin.RUN (#2692)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by yiranzai · committed by GitHub · parent 328d0b8 · Browse files · 3 files·+109 added−28 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- Co-authored-by: Bo-Yi Wu <appleboy.tw@gmail.com>
Diff stats
3 files+109 added−28 removed
- other:+109 added−28 removed
Provenance
- Commit
b5ca989- Tree
8957a64- Author
yiranzai <wuqingdzx@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
328d0b8
3 files changed+109 added−28 removed
modified:context_test.go+11 added-25 removed
1390 unmodified lines1391 assert.Equal(t, abortIndex, c.index)1392 assert.True(t, c.IsAborted())1393 }1394 1395-func resetTrustedCIDRs(c *Context) {1396- c.engine.trustedCIDRs, _ = c.engine.prepareTrustedCIDRs()1397-}1398-1395 func TestContextClientIP(t *testing.T) {1396 c, _ := CreateTestContext(httptest.NewRecorder())1397 c.Request, _ = http.NewRequest("POST", "/", nil)1402- resetTrustedCIDRs(c)1398+ c.engine.trustedCIDRs, _ = c.engine.prepareTrustedCIDRs()1399 resetContextForClientIPTests(c)1400 1401 // Legacy tests (validating that the defaults don't break the1402 // (insecure!) old behaviour)20 unmodified lines1423 // Tests exercising the TrustedProxies functionality1424 resetContextForClientIPTests(c)1425 1426 // No trusted proxies1431- c.engine.TrustedProxies = []string{}1432- resetTrustedCIDRs(c)1427+ _ = c.engine.SetTrustedProxies([]string{})1428 c.engine.RemoteIPHeaders = []string{"X-Forwarded-For"}1429 assert.Equal(t, "40.40.40.40", c.ClientIP())1430 1431 // Last proxy is trusted, but the RemoteAddr is not1437- c.engine.TrustedProxies = []string{"30.30.30.30"}1438- resetTrustedCIDRs(c)1432+ _ = c.engine.SetTrustedProxies([]string{"30.30.30.30"})1433 assert.Equal(t, "40.40.40.40", c.ClientIP())1434 1435 // Only trust RemoteAddr1442- c.engine.TrustedProxies = []string{"40.40.40.40"}1443- resetTrustedCIDRs(c)1436+ _ = c.engine.SetTrustedProxies([]string{"40.40.40.40"})1437 assert.Equal(t, "20.20.20.20", c.ClientIP())1438 1439 // All steps are trusted1447- c.engine.TrustedProxies = []string{"40.40.40.40", "30.30.30.30", "20.20.20.20"}1448- resetTrustedCIDRs(c)1440+ _ = c.engine.SetTrustedProxies([]string{"40.40.40.40", "30.30.30.30", "20.20.20.20"})1441 assert.Equal(t, "20.20.20.20", c.ClientIP())1442 1443 // Use CIDR1452- c.engine.TrustedProxies = []string{"40.40.25.25/16", "30.30.30.30"}1453- resetTrustedCIDRs(c)1444+ _ = c.engine.SetTrustedProxies([]string{"40.40.25.25/16", "30.30.30.30"})1445 assert.Equal(t, "20.20.20.20", c.ClientIP())1446 1447 // Use hostname that resolves to all the proxies1457- c.engine.TrustedProxies = []string{"foo"}1458- resetTrustedCIDRs(c)1448+ _ = c.engine.SetTrustedProxies([]string{"foo"})1449 assert.Equal(t, "40.40.40.40", c.ClientIP())1450 1451 // Use hostname that returns an error1462- c.engine.TrustedProxies = []string{"bar"}1463- resetTrustedCIDRs(c)1452+ _ = c.engine.SetTrustedProxies([]string{"bar"})1453 assert.Equal(t, "40.40.40.40", c.ClientIP())1454 1455 // X-Forwarded-For has a non-IP element1467- c.engine.TrustedProxies = []string{"40.40.40.40"}1468- resetTrustedCIDRs(c)1456+ _ = c.engine.SetTrustedProxies([]string{"40.40.40.40"})1457 c.Request.Header.Set("X-Forwarded-For", " blah ")1458 assert.Equal(t, "40.40.40.40", c.ClientIP())1459 1460 // Result from LookupHost has non-IP element. This should never1461 // happen, but we should test it to make sure we handle it1462 // gracefully.1475- c.engine.TrustedProxies = []string{"baz"}1476- resetTrustedCIDRs(c)1463+ _ = c.engine.SetTrustedProxies([]string{"baz"})1464 c.Request.Header.Set("X-Forwarded-For", " 30.30.30.30 ")1465 assert.Equal(t, "40.40.40.40", c.ClientIP())1466 1480- c.engine.TrustedProxies = []string{"40.40.40.40"}1481- resetTrustedCIDRs(c)1467+ _ = c.engine.SetTrustedProxies([]string{"40.40.40.40"})1468 c.Request.Header.Del("X-Forwarded-For")1469 c.engine.RemoteIPHeaders = []string{"X-Forwarded-For", "X-Real-IP"}1470 assert.Equal(t, "10.10.10.10", c.ClientIP())1471 571 unmodified linesmodified:gin_integration_test.go+62 added-1 removed
53 unmodified lines54 assert.Error(t, router.Run(":8080"))55 testRequest(t, "http://localhost:8080/example")56 }57 58-func TestTrustedCIDRsForRun(t *testing.T) {58+func TestBadTrustedCIDRsForRun(t *testing.T) {59 os.Setenv("PORT", "")60 router := New()61 router.TrustedProxies = []string{"hello/world"}62 assert.Error(t, router.Run(":8080"))63 }64 65+func TestBadTrustedCIDRsForRunUnix(t *testing.T) {66+ router := New()67+ router.TrustedProxies = []string{"hello/world"}68+69+ unixTestSocket := filepath.Join(os.TempDir(), "unix_unit_test")70+71+ defer os.Remove(unixTestSocket)72+73+ go func() {74+ router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") })75+ assert.Error(t, router.RunUnix(unixTestSocket))76+ }()77+ // have to wait for the goroutine to start and run the server78+ // otherwise the main thread will complete79+ time.Sleep(5 * time.Millisecond)80+}81+82+func TestBadTrustedCIDRsForRunFd(t *testing.T) {83+ router := New()84+ router.TrustedProxies = []string{"hello/world"}85+86+ addr, err := net.ResolveTCPAddr("tcp", "localhost:0")87+ assert.NoError(t, err)88+ listener, err := net.ListenTCP("tcp", addr)89+ assert.NoError(t, err)90+ socketFile, err := listener.File()91+ assert.NoError(t, err)92+93+ go func() {94+ router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") })95+ assert.Error(t, router.RunFd(int(socketFile.Fd())))96+ }()97+ // have to wait for the goroutine to start and run the server98+ // otherwise the main thread will complete99+ time.Sleep(5 * time.Millisecond)100+}101+102+func TestBadTrustedCIDRsForRunListener(t *testing.T) {103+ router := New()104+ router.TrustedProxies = []string{"hello/world"}105+106+ addr, err := net.ResolveTCPAddr("tcp", "localhost:0")107+ assert.NoError(t, err)108+ listener, err := net.ListenTCP("tcp", addr)109+ assert.NoError(t, err)110+ go func() {111+ router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") })112+ assert.Error(t, router.RunListener(listener))113+ }()114+ // have to wait for the goroutine to start and run the server115+ // otherwise the main thread will complete116+ time.Sleep(5 * time.Millisecond)117+}118+119+func TestBadTrustedCIDRsForRunTLS(t *testing.T) {120+ os.Setenv("PORT", "")121+ router := New()122+ router.TrustedProxies = []string{"hello/world"}123+ assert.Error(t, router.RunTLS(":8080", "./testdata/certificate/cert.pem", "./testdata/certificate/key.pem"))124+}125+126 func TestRunTLS(t *testing.T) {127 router := New()128 go func() {129 router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") })246 unmodified linesmodified:gin.go+36 added-2 removed
324 unmodified lines325 // Note: this method will block the calling goroutine indefinitely unless an error happens.326 func (engine *Engine) Run(addr ...string) (err error) {327 defer func() { debugPrintError(err) }()328 329- trustedCIDRs, err := engine.prepareTrustedCIDRs()329+ err = engine.parseTrustedProxies()330 if err != nil {331 return err332 }333- engine.trustedCIDRs = trustedCIDRs333+334 address := resolveAddress(addr)335 debugPrint("Listening and serving HTTP on %s\n", address)336 err = http.ListenAndServe(address, engine)337 return27 unmodified lines365 }366 return cidr, nil367 }368 369+// SetTrustedProxies set Engine.TrustedProxies370+func (engine *Engine) SetTrustedProxies(trustedProxies []string) error {371+ engine.TrustedProxies = trustedProxies372+ return engine.parseTrustedProxies()373+}374+375+// parseTrustedProxies parse Engine.TrustedProxies to Engine.trustedCIDRs376+func (engine *Engine) parseTrustedProxies() error {377+ trustedCIDRs, err := engine.prepareTrustedCIDRs()378+ engine.trustedCIDRs = trustedCIDRs379+ return err380+}381+382 // parseIP parse a string representation of an IP and returns a net.IP with the383 // minimum byte representation or nil if input is invalid.384 func parseIP(ip string) net.IP {385 parsedIP := net.ParseIP(ip)13 unmodified lines399 func (engine *Engine) RunTLS(addr, certFile, keyFile string) (err error) {400 debugPrint("Listening and serving HTTPS on %s\n", addr)401 defer func() { debugPrintError(err) }()402 403+ err = engine.parseTrustedProxies()404+ if err != nil {405+ return err406+ }407+408 err = http.ListenAndServeTLS(addr, certFile, keyFile, engine)409 return410 }411 3 unmodified lines415 func (engine *Engine) RunUnix(file string) (err error) {416 debugPrint("Listening and serving HTTP on unix:/%s", file)417 defer func() { debugPrintError(err) }()418 419+ err = engine.parseTrustedProxies()420+ if err != nil {421+ return err422+ }423+424 listener, err := net.Listen("unix", file)425 if err != nil {426 return427 }10 unmodified lines438 func (engine *Engine) RunFd(fd int) (err error) {439 debugPrint("Listening and serving HTTP on fd@%d", fd)440 defer func() { debugPrintError(err) }()441 442+ err = engine.parseTrustedProxies()443+ if err != nil {444+ return err445+ }446+447 f := os.NewFile(uintptr(fd), fmt.Sprintf("fd@%d", fd))448 listener, err := net.FileListener(f)449 if err != nil {450 return7 unmodified lines458 // through the specified net.Listener459 func (engine *Engine) RunListener(listener net.Listener) (err error) {460 debugPrint("Listening and serving HTTP on listener what's bind with address@%s", listener.Addr())461 defer func() { debugPrintError(err) }()462+463+ err = engine.parseTrustedProxies()464+ if err != nil {465+ return err466+ }467+468 err = http.Serve(listener, engine)469 return470 }471 140 unmodified lines