Skip to content
Myr

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

3 files+109 added−28 removed

modified:context_test.go+11 added-25 removed
1390 unmodified lines
1391 assert.Equal(t, abortIndex, c.index)1392 assert.True(t, c.IsAborted())1393}13941395func resetTrustedCIDRs(c *Context) {1396 c.engine.trustedCIDRs, _ = c.engine.prepareTrustedCIDRs()1397}13981395func 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)14001401 // Legacy tests (validating that the defaults don't break the1402 // (insecure!) old behaviour)
20 unmodified lines
1423 // Tests exercising the TrustedProxies functionality1424 resetContextForClientIPTests(c)14251426 // 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())14301431 // 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())14341435 // 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())14381439 // 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())14421443 // 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())14461447 // 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())14501451 // 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())14541455 // 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())14591460 // 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())14661480 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 lines
modified:gin_integration_test.go+62 added-1 removed
53 unmodified lines
54 assert.Error(t, router.Run(":8080"))55 testRequest(t, "http://localhost:8080/example")56}5758func TestTrustedCIDRsForRun(t *testing.T) {58func 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}6465func TestBadTrustedCIDRsForRunUnix(t *testing.T) {66 router := New()67 router.TrustedProxies = []string{"hello/world"}6869 unixTestSocket := filepath.Join(os.TempDir(), "unix_unit_test")7071 defer os.Remove(unixTestSocket)7273 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}8182func TestBadTrustedCIDRsForRunFd(t *testing.T) {83 router := New()84 router.TrustedProxies = []string{"hello/world"}8586 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)9293 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}101102func TestBadTrustedCIDRsForRunListener(t *testing.T) {103 router := New()104 router.TrustedProxies = []string{"hello/world"}105106 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}118119func 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}125126func 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 lines
modified:gin.go+36 added-2 removed
324 unmodified lines
325// Note: this method will block the calling goroutine indefinitely unless an error happens.326func (engine *Engine) Run(addr ...string) (err error) {327 defer func() { debugPrintError(err) }()328329 trustedCIDRs, err := engine.prepareTrustedCIDRs()329 err = engine.parseTrustedProxies()330 if err != nil {331 return err332 }333 engine.trustedCIDRs = trustedCIDRs333334 address := resolveAddress(addr)335 debugPrint("Listening and serving HTTP on %s\n", address)336 err = http.ListenAndServe(address, engine)337 return
27 unmodified lines
365 }366 return cidr, nil367}368369// SetTrustedProxies set Engine.TrustedProxies370func (engine *Engine) SetTrustedProxies(trustedProxies []string) error {371 engine.TrustedProxies = trustedProxies372 return engine.parseTrustedProxies()373}374375// parseTrustedProxies parse Engine.TrustedProxies to Engine.trustedCIDRs376func (engine *Engine) parseTrustedProxies() error {377 trustedCIDRs, err := engine.prepareTrustedCIDRs()378 engine.trustedCIDRs = trustedCIDRs379 return err380}381382// parseIP parse a string representation of an IP and returns a net.IP with the383// minimum byte representation or nil if input is invalid.384func parseIP(ip string) net.IP {385 parsedIP := net.ParseIP(ip)
13 unmodified lines
399func (engine *Engine) RunTLS(addr, certFile, keyFile string) (err error) {400 debugPrint("Listening and serving HTTPS on %s\n", addr)401 defer func() { debugPrintError(err) }()402403 err = engine.parseTrustedProxies()404 if err != nil {405 return err406 }407408 err = http.ListenAndServeTLS(addr, certFile, keyFile, engine)409 return410}411
3 unmodified lines
415func (engine *Engine) RunUnix(file string) (err error) {416 debugPrint("Listening and serving HTTP on unix:/%s", file)417 defer func() { debugPrintError(err) }()418419 err = engine.parseTrustedProxies()420 if err != nil {421 return err422 }423424 listener, err := net.Listen("unix", file)425 if err != nil {426 return427 }
10 unmodified lines
438func (engine *Engine) RunFd(fd int) (err error) {439 debugPrint("Listening and serving HTTP on fd@%d", fd)440 defer func() { debugPrintError(err) }()441442 err = engine.parseTrustedProxies()443 if err != nil {444 return err445 }446447 f := os.NewFile(uintptr(fd), fmt.Sprintf("fd@%d", fd))448 listener, err := net.FileListener(f)449 if err != nil {450 return
7 unmodified lines
458// through the specified net.Listener459func (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) }()462463 err = engine.parseTrustedProxies()464 if err != nil {465 return err466 }467468 err = http.Serve(listener, engine)469 return470}471
140 unmodified lines