Skip to content
Myr

fix: data race with trustedCIDRs (#2674) (#2675)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Xudong Cai · committed by GitHub · parent d496f64 · Browse files · 3 files+17 added−3 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+17 added−3 removed

  • other:+17 added−3 removed

3 files+17 added−3 removed

modified:context_test.go+15 added-1 removed
1386 unmodified lines
1387 assert.Equal(t, abortIndex, c.index)1388 assert.True(t, c.IsAborted())1389}13901391func resetTrustedCIDRs(c *Context) {1392 c.engine.trustedCIDRs, _ = c.engine.prepareTrustedCIDRs()1393}13941395func TestContextClientIP(t *testing.T) {1396 c, _ := CreateTestContext(httptest.NewRecorder())1397 c.Request, _ = http.NewRequest("POST", "/", nil)13941398 resetTrustedCIDRs(c)1399 resetContextForClientIPTests(c)14001401 // Legacy tests (validating that the defaults don't break the1402 // (insecure!) old behaviour)
21 unmodified lines
1424 resetContextForClientIPTests(c)14251426 // No trusted proxies1427 c.engine.TrustedProxies = []string{}1428 resetTrustedCIDRs(c)1429 c.engine.RemoteIPHeaders = []string{"X-Forwarded-For"}1430 assert.Equal(t, "40.40.40.40", c.ClientIP())14311432 // Last proxy is trusted, but the RemoteAddr is not1433 c.engine.TrustedProxies = []string{"30.30.30.30"}1434 resetTrustedCIDRs(c)1435 assert.Equal(t, "40.40.40.40", c.ClientIP())14361437 // Only trust RemoteAddr1438 c.engine.TrustedProxies = []string{"40.40.40.40"}1439 resetTrustedCIDRs(c)1440 assert.Equal(t, "20.20.20.20", c.ClientIP())14411442 // All steps are trusted1443 c.engine.TrustedProxies = []string{"40.40.40.40", "30.30.30.30", "20.20.20.20"}1444 resetTrustedCIDRs(c)1445 assert.Equal(t, "20.20.20.20", c.ClientIP())14461447 // Use CIDR1448 c.engine.TrustedProxies = []string{"40.40.25.25/16", "30.30.30.30"}1449 resetTrustedCIDRs(c)1450 assert.Equal(t, "20.20.20.20", c.ClientIP())14511452 // Use hostname that resolves to all the proxies1453 c.engine.TrustedProxies = []string{"foo"}1454 resetTrustedCIDRs(c)1455 assert.Equal(t, "40.40.40.40", c.ClientIP())14561457 // Use hostname that returns an error1458 c.engine.TrustedProxies = []string{"bar"}1459 resetTrustedCIDRs(c)1460 assert.Equal(t, "40.40.40.40", c.ClientIP())14611462 // X-Forwarded-For has a non-IP element1463 c.engine.TrustedProxies = []string{"40.40.40.40"}1464 resetTrustedCIDRs(c)1465 c.Request.Header.Set("X-Forwarded-For", " blah ")1466 assert.Equal(t, "40.40.40.40", c.ClientIP())14671468 // Result from LookupHost has non-IP element. This should never1469 // happen, but we should test it to make sure we handle it1470 // gracefully.1471 c.engine.TrustedProxies = []string{"baz"}1472 resetTrustedCIDRs(c)1473 c.Request.Header.Set("X-Forwarded-For", " 30.30.30.30 ")1474 assert.Equal(t, "40.40.40.40", c.ClientIP())14751476 c.engine.TrustedProxies = []string{"40.40.40.40"}1477 resetTrustedCIDRs(c)1478 c.Request.Header.Del("X-Forwarded-For")1479 c.engine.RemoteIPHeaders = []string{"X-Forwarded-For", "X-Real-IP"}1480 assert.Equal(t, "10.10.10.10", c.ClientIP())1481
571 unmodified lines
modified:context.go-2 removed
765 unmodified lines
766 if remoteIP == nil {767 return nil, false768 }769770 trustedCIDRs, _ := c.engine.prepareTrustedCIDRs()771 c.engine.trustedCIDRs = trustedCIDRs770 if c.engine.trustedCIDRs != nil {771 for _, cidr := range c.engine.trustedCIDRs {772 if cidr.Contains(remoteIP) {773 return remoteIP, true
405 unmodified lines
modified:logger_test.go+2 added
183 unmodified lines
184 var gotKeys map[string]interface{}185 buffer := new(bytes.Buffer)186187 router := New()188 router.engine.trustedCIDRs, _ = router.engine.prepareTrustedCIDRs()189190 router.Use(LoggerWithConfig(LoggerConfig{191 Output: buffer,192 Formatter: func(param LogFormatterParams) string {193 // for assert test
243 unmodified lines