refactor: rename trusted_proxies package to middleware and update function calls
This commit is contained in:
@@ -172,7 +172,7 @@ func main() {
|
|||||||
|
|
||||||
// Initialize HTTP server
|
// Initialize HTTP server
|
||||||
server := gin.New()
|
server := gin.New()
|
||||||
if err := configureTrustedProxies(server); err != nil {
|
if err := middleware.ConfigureTrustedProxies(server); err != nil {
|
||||||
common.FatalLog("failed to configure trusted proxies: " + err.Error())
|
common.FatalLog("failed to configure trusted proxies: " + err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
package main
|
package middleware
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
@@ -19,7 +19,7 @@ var defaultTrustedProxyCIDRs = []string{
|
|||||||
"fc00::/7",
|
"fc00::/7",
|
||||||
}
|
}
|
||||||
|
|
||||||
func configureTrustedProxies(engine *gin.Engine) error {
|
func ConfigureTrustedProxies(engine *gin.Engine) error {
|
||||||
rawTrustedProxies := strings.TrimSpace(os.Getenv("TRUSTED_PROXIES"))
|
rawTrustedProxies := strings.TrimSpace(os.Getenv("TRUSTED_PROXIES"))
|
||||||
if rawTrustedProxies == "" {
|
if rawTrustedProxies == "" {
|
||||||
log.Print("WARNING: TRUSTED_PROXIES is unset or blank; trusting loopback, RFC 1918, and IPv6 ULA proxy addresses for compatibility. Set TRUSTED_PROXIES=none to trust no proxies, or configure explicit proxy IPs/CIDRs to replace these defaults.")
|
log.Print("WARNING: TRUSTED_PROXIES is unset or blank; trusting loopback, RFC 1918, and IPv6 ULA proxy addresses for compatibility. Set TRUSTED_PROXIES=none to trust no proxies, or configure explicit proxy IPs/CIDRs to replace these defaults.")
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package main
|
package middleware
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -33,7 +33,7 @@ func TestConfigureTrustedProxiesDefaultsToLoopbackAndPrivateNetworks(t *testing.
|
|||||||
gin.SetMode(gin.TestMode)
|
gin.SetMode(gin.TestMode)
|
||||||
t.Setenv("TRUSTED_PROXIES", "")
|
t.Setenv("TRUSTED_PROXIES", "")
|
||||||
router := newClientIPRouter()
|
router := newClientIPRouter()
|
||||||
require.NoError(t, configureTrustedProxies(router))
|
require.NoError(t, ConfigureTrustedProxies(router))
|
||||||
|
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
name string
|
name string
|
||||||
@@ -59,7 +59,7 @@ func TestConfigureTrustedProxiesDefaultRejectsPublicPeerHeaders(t *testing.T) {
|
|||||||
gin.SetMode(gin.TestMode)
|
gin.SetMode(gin.TestMode)
|
||||||
t.Setenv("TRUSTED_PROXIES", " \t ")
|
t.Setenv("TRUSTED_PROXIES", " \t ")
|
||||||
router := newClientIPRouter()
|
router := newClientIPRouter()
|
||||||
require.NoError(t, configureTrustedProxies(router))
|
require.NoError(t, ConfigureTrustedProxies(router))
|
||||||
|
|
||||||
clientIP := requestClientIP(router, "198.51.100.10:12345", "203.0.113.10")
|
clientIP := requestClientIP(router, "198.51.100.10:12345", "203.0.113.10")
|
||||||
assert.Equal(t, "198.51.100.10", clientIP, "a public peer must not make a spoofed X-Forwarded-For authoritative")
|
assert.Equal(t, "198.51.100.10", clientIP, "a public peer must not make a spoofed X-Forwarded-For authoritative")
|
||||||
@@ -69,7 +69,7 @@ func TestConfigureTrustedProxiesDefaultStopsAtPublicClientInForwardedChain(t *te
|
|||||||
gin.SetMode(gin.TestMode)
|
gin.SetMode(gin.TestMode)
|
||||||
t.Setenv("TRUSTED_PROXIES", "")
|
t.Setenv("TRUSTED_PROXIES", "")
|
||||||
router := newClientIPRouter()
|
router := newClientIPRouter()
|
||||||
require.NoError(t, configureTrustedProxies(router))
|
require.NoError(t, ConfigureTrustedProxies(router))
|
||||||
|
|
||||||
clientIP := requestClientIP(router, "172.20.0.2:12345", "192.0.2.99, 203.0.113.10")
|
clientIP := requestClientIP(router, "172.20.0.2:12345", "192.0.2.99, 203.0.113.10")
|
||||||
assert.Equal(t, "203.0.113.10", clientIP, "the first public hop from the trusted proxy must win over a client-supplied prefix")
|
assert.Equal(t, "203.0.113.10", clientIP, "the first public hop from the trusted proxy must win over a client-supplied prefix")
|
||||||
@@ -79,7 +79,7 @@ func TestConfigureTrustedProxiesNoneDisablesForwardedHeaders(t *testing.T) {
|
|||||||
gin.SetMode(gin.TestMode)
|
gin.SetMode(gin.TestMode)
|
||||||
t.Setenv("TRUSTED_PROXIES", " NoNe ")
|
t.Setenv("TRUSTED_PROXIES", " NoNe ")
|
||||||
router := newClientIPRouter()
|
router := newClientIPRouter()
|
||||||
require.NoError(t, configureTrustedProxies(router))
|
require.NoError(t, ConfigureTrustedProxies(router))
|
||||||
|
|
||||||
clientIP := requestClientIP(router, "127.0.0.1:12345", "203.0.113.10")
|
clientIP := requestClientIP(router, "127.0.0.1:12345", "203.0.113.10")
|
||||||
assert.Equal(t, "127.0.0.1", clientIP)
|
assert.Equal(t, "127.0.0.1", clientIP)
|
||||||
@@ -89,7 +89,7 @@ func TestConfigureTrustedProxiesAcceptsTrimmedIPsAndCIDRs(t *testing.T) {
|
|||||||
gin.SetMode(gin.TestMode)
|
gin.SetMode(gin.TestMode)
|
||||||
t.Setenv("TRUSTED_PROXIES", " 192.0.2.0/24, 198.51.100.30 ")
|
t.Setenv("TRUSTED_PROXIES", " 192.0.2.0/24, 198.51.100.30 ")
|
||||||
router := newClientIPRouter()
|
router := newClientIPRouter()
|
||||||
require.NoError(t, configureTrustedProxies(router))
|
require.NoError(t, ConfigureTrustedProxies(router))
|
||||||
|
|
||||||
trustedClientIP := requestClientIP(router, "192.0.2.10:12345", "203.0.113.20")
|
trustedClientIP := requestClientIP(router, "192.0.2.10:12345", "203.0.113.20")
|
||||||
assert.Equal(t, "203.0.113.20", trustedClientIP)
|
assert.Equal(t, "203.0.113.20", trustedClientIP)
|
||||||
@@ -121,7 +121,7 @@ func TestConfigureTrustedProxiesRejectsInvalidConfiguration(t *testing.T) {
|
|||||||
t.Run(testCase.name, func(t *testing.T) {
|
t.Run(testCase.name, func(t *testing.T) {
|
||||||
t.Setenv("TRUSTED_PROXIES", testCase.value)
|
t.Setenv("TRUSTED_PROXIES", testCase.value)
|
||||||
router := newClientIPRouter()
|
router := newClientIPRouter()
|
||||||
assert.Error(t, configureTrustedProxies(router))
|
assert.Error(t, ConfigureTrustedProxies(router))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user