From eed94263926e6fba061ed4a56e961b2aab1b17df Mon Sep 17 00:00:00 2001 From: Christopher Schultz Date: Thu, 30 Jul 2020 17:25:05 -0400 Subject: [PATCH] Add test including port numbers in various places. --- .../catalina/valves/TestRemoteIpValve.java | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/test/org/apache/catalina/valves/TestRemoteIpValve.java b/test/org/apache/catalina/valves/TestRemoteIpValve.java index 5fc43f959aa6..036cf1a6f622 100644 --- a/test/org/apache/catalina/valves/TestRemoteIpValve.java +++ b/test/org/apache/catalina/valves/TestRemoteIpValve.java @@ -1133,6 +1133,62 @@ public void testRequestForwarded() throws Exception { request.getAttribute(Globals.REQUEST_FORWARDED_ATTRIBUTE)); } + @Test + public void testRequestForwardedForWithPortNumber() throws Exception { + + // PREPARE + RemoteIpValve remoteIpValve = new RemoteIpValve(); + RemoteAddrAndHostTrackerValve remoteAddrAndHostTrackerValve = new RemoteAddrAndHostTrackerValve(); + remoteIpValve.setNext(remoteAddrAndHostTrackerValve); + + Request request = new MockRequest(); + request.setCoyoteRequest(new org.apache.coyote.Request()); + // client ip + request.setRemoteAddr("192.168.0.10"); + request.setRemoteHost("192.168.0.10"); + request.getCoyoteRequest().getMimeHeaders().addValue("x-forwarded-for").setString("140.211.11.130:1234"); + // protocol + request.setServerPort(8080); + request.getCoyoteRequest().scheme().setString("http"); + + // TEST + remoteIpValve.invoke(request, null); + + // VERIFY + + Assert.assertEquals("140.211.11.130:1234", remoteAddrAndHostTrackerValve.getRemoteAddr()); + } + + @Test + public void testRequestForwardedForWithProxyPortNumber() throws Exception { + + // PREPARE + RemoteIpValve remoteIpValve = new RemoteIpValve(); + //remoteIpValve.setRemoteIpHeader("x-forwarded-for"); + //remoteIpValve.setProtocolHeader("x-forwarded-proto"); + RemoteAddrAndHostTrackerValve remoteAddrAndHostTrackerValve = new RemoteAddrAndHostTrackerValve(); + remoteIpValve.setNext(remoteAddrAndHostTrackerValve); + + Request request = new MockRequest(); + request.setCoyoteRequest(new org.apache.coyote.Request()); + // client ip + request.setRemoteAddr("192.168.0.10"); + request.setRemoteHost("192.168.0.10"); + // Trust c.d + remoteIpValve.setTrustedProxies("foo\\.bar:123"); + request.getCoyoteRequest().getMimeHeaders().addValue("x-forwarded-for").setString("140.211.11.130:1234, foo.bar:123"); + // protocol + request.setServerPort(8080); + request.getCoyoteRequest().scheme().setString("http"); + + // TEST + remoteIpValve.invoke(request, null); + + // VERIFY + + Assert.assertEquals("140.211.11.130:1234", remoteAddrAndHostTrackerValve.getRemoteAddr()); + } + private void assertArrayEquals(String[] expected, String[] actual) { if (expected == null) { Assert.assertNull(actual);