@@ -44,6 +44,7 @@ var _ = framework.DescribeSetting("use-proxy-protocol", func() {
44
44
f .NewEchoDeployment ()
45
45
f .UpdateNginxConfigMapData (setting , "false" )
46
46
})
47
+
47
48
//nolint:dupl // Ignore dupl errors for similar test case
48
49
ginkgo .It ("should respect port passed by the PROXY Protocol" , func () {
49
50
host := proxyProtocol
@@ -227,4 +228,86 @@ var _ = framework.DescribeSetting("use-proxy-protocol", func() {
227
228
assert .Nil (ginkgo .GinkgoT (), err , "obtaining nginx logs" )
228
229
assert .Contains (ginkgo .GinkgoT (), logs , `192.168.0.1` )
229
230
})
231
+
232
+ ginkgo .Context ("when use-forwarded-headers setting is true" , func () {
233
+ cmapData := map [string ]string {}
234
+
235
+ cmapData [setting ] = "true"
236
+ cmapData ["use-forwarded-headers" ] = "true"
237
+
238
+ ginkgo .It ("should not trust X-Forwarded headers when the client IP address is not trusted" , func () {
239
+ host := proxyProtocol
240
+
241
+ f .SetNginxConfigMapData (cmapData )
242
+
243
+ f .EnsureIngress (framework .NewSingleIngress (host , "/" , host , f .Namespace , framework .EchoService , 80 , nil ))
244
+
245
+ f .WaitForNginxServer (host ,
246
+ func (server string ) bool {
247
+ return strings .Contains (server , "server_name proxy-protocol" ) &&
248
+ strings .Contains (server , "listen 80 proxy_protocol" )
249
+ })
250
+
251
+ ip := f .GetNginxIP ()
252
+
253
+ conn , err := net .Dial ("tcp" , net .JoinHostPort (ip , "80" ))
254
+ assert .Nil (ginkgo .GinkgoT (), err , "unexpected error creating connection to %s:80" , ip )
255
+ defer conn .Close ()
256
+
257
+ header := "PROXY TCP4 192.168.0.1 192.168.0.11 56324 1234\r \n "
258
+ _ , err = conn .Write ([]byte (header ))
259
+ assert .Nil (ginkgo .GinkgoT (), err , "unexpected error writing header" )
260
+
261
+ _ , err = conn .Write ([]byte ("GET / HTTP/1.1\r \n Host: proxy-protocol\r \n X-Forwarded-For: 192.168.0.111\r \n \r \n " ))
262
+ assert .Nil (ginkgo .GinkgoT (), err , "unexpected error writing request" )
263
+
264
+ data , err := io .ReadAll (conn )
265
+ assert .Nil (ginkgo .GinkgoT (), err , "unexpected error reading connection data" )
266
+
267
+ body := string (data )
268
+ assert .Contains (ginkgo .GinkgoT (), body , fmt .Sprintf ("host=%v" , proxyProtocol ))
269
+ assert .Contains (ginkgo .GinkgoT (), body , "x-forwarded-port=1234" )
270
+ assert .Contains (ginkgo .GinkgoT (), body , "x-forwarded-proto=http" )
271
+ assert .Contains (ginkgo .GinkgoT (), body , "x-forwarded-for=192.168.0.1" )
272
+ })
273
+
274
+ ginkgo .It ("should trust X-Forwarded headers when the client IP address is trusted" , func () {
275
+ host := proxyProtocol
276
+
277
+ // Trust IPs from the private network CIDR block and the client IP address in the proxy protocol header
278
+ cmapData ["proxy-real-ip-cidr" ] = "10.0.0.0/8,192.168.0.1/32"
279
+
280
+ f .SetNginxConfigMapData (cmapData )
281
+
282
+ f .EnsureIngress (framework .NewSingleIngress (host , "/" , host , f .Namespace , framework .EchoService , 80 , nil ))
283
+
284
+ f .WaitForNginxServer (host ,
285
+ func (server string ) bool {
286
+ return strings .Contains (server , "server_name proxy-protocol" ) &&
287
+ strings .Contains (server , "listen 80 proxy_protocol" )
288
+ })
289
+
290
+ ip := f .GetNginxIP ()
291
+
292
+ conn , err := net .Dial ("tcp" , net .JoinHostPort (ip , "80" ))
293
+ assert .Nil (ginkgo .GinkgoT (), err , "unexpected error creating connection to %s:80" , ip )
294
+ defer conn .Close ()
295
+
296
+ header := "PROXY TCP4 192.168.0.1 192.168.0.11 56324 1234\r \n "
297
+ _ , err = conn .Write ([]byte (header ))
298
+ assert .Nil (ginkgo .GinkgoT (), err , "unexpected error writing header" )
299
+
300
+ _ , err = conn .Write ([]byte ("GET / HTTP/1.1\r \n Host: proxy-protocol\r \n X-Forwarded-For: 192.168.0.111\r \n \r \n " ))
301
+ assert .Nil (ginkgo .GinkgoT (), err , "unexpected error writing request" )
302
+
303
+ data , err := io .ReadAll (conn )
304
+ assert .Nil (ginkgo .GinkgoT (), err , "unexpected error reading connection data" )
305
+
306
+ body := string (data )
307
+ assert .Contains (ginkgo .GinkgoT (), body , fmt .Sprintf ("host=%v" , proxyProtocol ))
308
+ assert .Contains (ginkgo .GinkgoT (), body , "x-forwarded-port=1234" )
309
+ assert .Contains (ginkgo .GinkgoT (), body , "x-forwarded-proto=http" )
310
+ assert .Contains (ginkgo .GinkgoT (), body , "x-forwarded-for=192.168.0.111" )
311
+ })
312
+ })
230
313
})
0 commit comments