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
Provenance
- Commit
03e5e05- Tree
e5697ef- Author
Xudong Cai <fifsky@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
d496f64
3 files changed+17 added−3 removed
modified:context_test.go+15 added-1 removed
1386 unmodified lines1387 assert.Equal(t, abortIndex, c.index)1388 assert.True(t, c.IsAborted())1389 }1390 1391+func resetTrustedCIDRs(c *Context) {1392+ c.engine.trustedCIDRs, _ = c.engine.prepareTrustedCIDRs()1393+}1394+1395 func TestContextClientIP(t *testing.T) {1396 c, _ := CreateTestContext(httptest.NewRecorder())1397 c.Request, _ = http.NewRequest("POST", "/", nil)1394-1398+ resetTrustedCIDRs(c)1399 resetContextForClientIPTests(c)1400 1401 // Legacy tests (validating that the defaults don't break the1402 // (insecure!) old behaviour)21 unmodified lines1424 resetContextForClientIPTests(c)1425 1426 // 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())1431 1432 // 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())1436 1437 // Only trust RemoteAddr1438 c.engine.TrustedProxies = []string{"40.40.40.40"}1439+ resetTrustedCIDRs(c)1440 assert.Equal(t, "20.20.20.20", c.ClientIP())1441 1442 // 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())1446 1447 // 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())1451 1452 // 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())1456 1457 // Use hostname that returns an error1458 c.engine.TrustedProxies = []string{"bar"}1459+ resetTrustedCIDRs(c)1460 assert.Equal(t, "40.40.40.40", c.ClientIP())1461 1462 // 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())1467 1468 // 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())1475 1476 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 linesmodified:context.go-2 removed
765 unmodified lines766 if remoteIP == nil {767 return nil, false768 }769 770- 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, true405 unmodified linesmodified:logger_test.go+2 added
183 unmodified lines184 var gotKeys map[string]interface{}185 buffer := new(bytes.Buffer)186 187 router := New()188+ router.engine.trustedCIDRs, _ = router.engine.prepareTrustedCIDRs()189+190 router.Use(LoggerWithConfig(LoggerConfig{191 Output: buffer,192 Formatter: func(param LogFormatterParams) string {193 // for assert test243 unmodified lines