@@ -212,6 +212,44 @@ func TestSkipProxyHeaderForCIDR(t *testing.T) {
212212 }
213213}
214214
215+ func TestTrustProxyHeaderFrom (t * testing.T ) {
216+ upstream , err := net .ResolveTCPAddr ("tcp" , "10.0.0.3:45738" )
217+ if err != nil {
218+ t .Fatalf ("err: %v" , err )
219+ }
220+
221+ var cases = []struct {
222+ name string
223+ policy ConnPolicyFunc
224+ upstreamAddr net.Addr
225+ expectedPolicy Policy
226+ expectError bool
227+ }{
228+ {"reject header from untrusted source" , TrustProxyHeaderFrom (net .ParseIP ("192.0.2.1" )), upstream , REJECT , false },
229+ {"use header from trusted load balancer" , TrustProxyHeaderFrom (net .ParseIP ("10.0.0.3" )), upstream , USE , false },
230+ {"use header when source matches any trusted IP" , TrustProxyHeaderFrom (net .ParseIP ("192.0.2.1" ), net .ParseIP ("10.0.0.3" )), upstream , USE , false },
231+ {"invalid address should return error" , TrustProxyHeaderFrom (net .ParseIP ("10.0.0.3" )), failingAddr {}, REJECT , true },
232+ }
233+
234+ for _ , tc := range cases {
235+ t .Run (tc .name , func (t * testing.T ) {
236+ policy , err := tc .policy (ConnPolicyOptions {
237+ Upstream : tc .upstreamAddr ,
238+ })
239+ if ! tc .expectError && err != nil {
240+ t .Fatalf ("err: %v" , err )
241+ }
242+ if tc .expectError && err == nil {
243+ t .Fatal ("Expected error, got none" )
244+ }
245+
246+ if policy != tc .expectedPolicy {
247+ t .Fatalf ("Expected policy %v, got %v" , tc .expectedPolicy , policy )
248+ }
249+ })
250+ }
251+ }
252+
215253func TestIgnoreProxyHeaderNotOnInterface (t * testing.T ) {
216254 downstream , err := net .ResolveTCPAddr ("tcp" , "10.0.0.3:45738" )
217255 if err != nil {
@@ -225,7 +263,7 @@ func TestIgnoreProxyHeaderNotOnInterface(t *testing.T) {
225263 expectedPolicy Policy
226264 expectError bool
227265 }{
228- {"ignore header for requests non on interface" , IgnoreProxyHeaderNotOnInterface (net .ParseIP ("192.0.2.1" )), downstream , IGNORE , false },
266+ {"ignore header for requests not on interface" , IgnoreProxyHeaderNotOnInterface (net .ParseIP ("192.0.2.1" )), downstream , IGNORE , false },
229267 {"use headers for requests on interface" , IgnoreProxyHeaderNotOnInterface (net .ParseIP ("10.0.0.3" )), downstream , USE , false },
230268 {"invalid address should return error" , IgnoreProxyHeaderNotOnInterface (net .ParseIP ("10.0.0.3" )), failingAddr {}, REJECT , true },
231269 }
@@ -247,5 +285,4 @@ func TestIgnoreProxyHeaderNotOnInterface(t *testing.T) {
247285 }
248286 })
249287 }
250-
251288}
0 commit comments