Search in sources :

Example 36 with FilterDef

use of org.apache.tomcat.util.descriptor.web.FilterDef in project tomcat by apache.

the class TestRemoteIpFilter method testInvokeAllProxiesAreTrustedEmptyInternal.

@Test
public void testInvokeAllProxiesAreTrustedEmptyInternal() throws Exception {
    // PREPARE
    RemoteIpFilter remoteIpFilter = new RemoteIpFilter();
    FilterDef filterDef = new FilterDef();
    filterDef.addInitParameter("internalProxies", "");
    filterDef.addInitParameter("trustedProxies", "proxy1|proxy2|proxy3");
    filterDef.addInitParameter("remoteIpHeader", "x-forwarded-for");
    filterDef.addInitParameter("proxiesHeader", "x-forwarded-by");
    filterDef.setFilter(remoteIpFilter);
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setRemoteAddr("proxy3");
    request.setRemoteHost("remote-host-original-value");
    request.setHeader("x-forwarded-for", "140.211.11.130, proxy1, proxy2");
    // TEST
    HttpServletRequest actualRequest = testRemoteIpFilter(filterDef, request).getRequest();
    // VERIFY
    String actualXForwardedFor = actualRequest.getHeader("x-forwarded-for");
    Assert.assertNull("all proxies are trusted, x-forwarded-for must be null", actualXForwardedFor);
    String actualXForwardedBy = actualRequest.getHeader("x-forwarded-by");
    Assert.assertEquals("all proxies are trusted, they must appear in x-forwarded-by", "proxy1, proxy2, proxy3", actualXForwardedBy);
    String actualRemoteAddr = actualRequest.getRemoteAddr();
    Assert.assertEquals("remoteAddr", "140.211.11.130", actualRemoteAddr);
    String actualRemoteHost = actualRequest.getRemoteHost();
    Assert.assertEquals("remoteHost", "140.211.11.130", actualRemoteHost);
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) FilterDef(org.apache.tomcat.util.descriptor.web.FilterDef) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 37 with FilterDef

use of org.apache.tomcat.util.descriptor.web.FilterDef in project tomcat by apache.

the class TestRemoteIpFilter method testWithTomcatServer.

/*
     * Test {@link RemoteIpFilter} in Tomcat standalone server
     */
@Test
public void testWithTomcatServer() throws Exception {
    // mostly default configuration : enable "x-forwarded-proto"
    Map<String, String> remoteIpFilterParameter = new HashMap<>();
    remoteIpFilterParameter.put("protocolHeader", "x-forwarded-proto");
    // SETUP
    Tomcat tomcat = getTomcatInstance();
    Context root = tomcat.addContext("", TEMP_DIR);
    FilterDef filterDef = new FilterDef();
    filterDef.getParameterMap().putAll(remoteIpFilterParameter);
    filterDef.setFilterClass(RemoteIpFilter.class.getName());
    filterDef.setFilterName(RemoteIpFilter.class.getName());
    root.addFilterDef(filterDef);
    FilterMap filterMap = new FilterMap();
    filterMap.setFilterName(RemoteIpFilter.class.getName());
    filterMap.addURLPatternDecoded("*");
    root.addFilterMap(filterMap);
    MockHttpServlet mockServlet = new MockHttpServlet();
    Tomcat.addServlet(root, mockServlet.getClass().getName(), mockServlet);
    root.addServletMappingDecoded("/test", mockServlet.getClass().getName());
    getTomcatInstance().start();
    // TEST
    HttpURLConnection httpURLConnection = (HttpURLConnection) new URL("http://localhost:" + tomcat.getConnector().getLocalPort() + "/test").openConnection();
    String expectedRemoteAddr = "my-remote-addr";
    httpURLConnection.addRequestProperty("x-forwarded-for", expectedRemoteAddr);
    httpURLConnection.addRequestProperty("x-forwarded-proto", "https");
    // VALIDATE
    Assert.assertEquals(HttpURLConnection.HTTP_OK, httpURLConnection.getResponseCode());
    HttpServletRequest request = mockServlet.getRequest();
    Assert.assertNotNull(request);
    // VALIDATE X-FORWARDED-FOR
    Assert.assertEquals(expectedRemoteAddr, request.getRemoteAddr());
    Assert.assertEquals(expectedRemoteAddr, request.getRemoteHost());
    // VALIDATE X-FORWARDED-PROTO
    Assert.assertTrue(request.isSecure());
    Assert.assertEquals("https", request.getScheme());
    Assert.assertEquals(443, request.getServerPort());
}
Also used : Context(org.apache.catalina.Context) TesterContext(org.apache.tomcat.unittest.TesterContext) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) Tomcat(org.apache.catalina.startup.Tomcat) HttpURLConnection(java.net.HttpURLConnection) FilterDef(org.apache.tomcat.util.descriptor.web.FilterDef) HashMap(java.util.HashMap) FilterMap(org.apache.tomcat.util.descriptor.web.FilterMap) URL(java.net.URL) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 38 with FilterDef

use of org.apache.tomcat.util.descriptor.web.FilterDef in project tomcat by apache.

the class TestStandardContext method configureTest46243Context.

private static void configureTest46243Context(Context context, boolean fail) {
    // Add a test filter that fails
    FilterDef filterDef = new FilterDef();
    filterDef.setFilterClass(Bug46243Filter.class.getName());
    filterDef.setFilterName("Bug46243");
    filterDef.addInitParameter("fail", Boolean.toString(fail));
    context.addFilterDef(filterDef);
    FilterMap filterMap = new FilterMap();
    filterMap.setFilterName("Bug46243");
    filterMap.addURLPatternDecoded("*");
    context.addFilterMap(filterMap);
    // Add a test servlet so there is something to generate a response if
    // it works (although it shouldn't)
    Tomcat.addServlet(context, "Bug46243", new HelloWorldServlet());
    context.addServletMappingDecoded("/", "Bug46243");
}
Also used : FilterDef(org.apache.tomcat.util.descriptor.web.FilterDef) FilterMap(org.apache.tomcat.util.descriptor.web.FilterMap)

Example 39 with FilterDef

use of org.apache.tomcat.util.descriptor.web.FilterDef in project tomcat by apache.

the class TestAddCharSetFilter method doTest.

private void doTest(String encoding, String expected, int mode, boolean useSetContentType) throws Exception {
    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    // Add the Servlet
    CharsetServlet servlet = new CharsetServlet(mode, useSetContentType);
    Tomcat.addServlet(ctx, "servlet", servlet);
    ctx.addServletMappingDecoded("/", "servlet");
    // Add the Filter
    FilterDef filterDef = new FilterDef();
    filterDef.setFilterClass(AddDefaultCharsetFilter.class.getName());
    filterDef.setFilterName("filter");
    if (encoding != null) {
        filterDef.addInitParameter("encoding", encoding);
    }
    ctx.addFilterDef(filterDef);
    FilterMap filterMap = new FilterMap();
    filterMap.setFilterName("filter");
    filterMap.addServletName("servlet");
    ctx.addFilterMap(filterMap);
    tomcat.start();
    Map<String, List<String>> headers = new HashMap<>();
    getUrl("http://localhost:" + getPort() + "/", new ByteChunk(), headers);
    String ct = getSingleHeader("Content-Type", headers).toLowerCase(Locale.ENGLISH);
    Assert.assertEquals("text/plain;charset=" + expected.toLowerCase(Locale.ENGLISH), ct);
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) FilterDef(org.apache.tomcat.util.descriptor.web.FilterDef) HashMap(java.util.HashMap) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) List(java.util.List) FilterMap(org.apache.tomcat.util.descriptor.web.FilterMap)

Example 40 with FilterDef

use of org.apache.tomcat.util.descriptor.web.FilterDef in project tomcat by apache.

the class TestExpiresFilter method testConfiguration.

@Test
public void testConfiguration() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    Context root = tomcat.addContext("", TEMP_DIR);
    FilterDef filterDef = new FilterDef();
    filterDef.addInitParameter("ExpiresDefault", "access plus 1 month");
    filterDef.addInitParameter("ExpiresByType text/html", "access plus 1 month 15 days 2 hours");
    filterDef.addInitParameter("ExpiresByType image/gif", "modification plus 5 hours 3 minutes");
    filterDef.addInitParameter("ExpiresByType image/jpg", "A10000");
    filterDef.addInitParameter("ExpiresByType video/mpeg", "M20000");
    filterDef.addInitParameter("ExpiresExcludedResponseStatusCodes", "304, 503");
    ExpiresFilter expiresFilter = new ExpiresFilter();
    filterDef.setFilter(expiresFilter);
    filterDef.setFilterClass(ExpiresFilter.class.getName());
    filterDef.setFilterName(ExpiresFilter.class.getName());
    root.addFilterDef(filterDef);
    FilterMap filterMap = new FilterMap();
    filterMap.setFilterName(ExpiresFilter.class.getName());
    filterMap.addURLPatternDecoded("*");
    tomcat.start();
    try {
        // VERIFY EXCLUDED RESPONSE STATUS CODES
        int[] excludedResponseStatusCodes = expiresFilter.getExcludedResponseStatusCodesAsInts();
        Assert.assertEquals(2, excludedResponseStatusCodes.length);
        Assert.assertEquals(304, excludedResponseStatusCodes[0]);
        Assert.assertEquals(503, excludedResponseStatusCodes[1]);
        // VERIFY DEFAULT CONFIGURATION
        ExpiresConfiguration expiresConfigurationDefault = expiresFilter.getDefaultExpiresConfiguration();
        Assert.assertEquals(StartingPoint.ACCESS_TIME, expiresConfigurationDefault.getStartingPoint());
        Assert.assertEquals(1, expiresConfigurationDefault.getDurations().size());
        Assert.assertEquals(DurationUnit.MONTH, expiresConfigurationDefault.getDurations().get(0).getUnit());
        Assert.assertEquals(1, expiresConfigurationDefault.getDurations().get(0).getAmount());
        // VERIFY TEXT/HTML
        ExpiresConfiguration expiresConfigurationTextHtml = expiresFilter.getExpiresConfigurationByContentType().get("text/html");
        Assert.assertEquals(StartingPoint.ACCESS_TIME, expiresConfigurationTextHtml.getStartingPoint());
        Assert.assertEquals(3, expiresConfigurationTextHtml.getDurations().size());
        Duration oneMonth = expiresConfigurationTextHtml.getDurations().get(0);
        Assert.assertEquals(DurationUnit.MONTH, oneMonth.getUnit());
        Assert.assertEquals(1, oneMonth.getAmount());
        Duration fifteenDays = expiresConfigurationTextHtml.getDurations().get(1);
        Assert.assertEquals(DurationUnit.DAY, fifteenDays.getUnit());
        Assert.assertEquals(15, fifteenDays.getAmount());
        Duration twoHours = expiresConfigurationTextHtml.getDurations().get(2);
        Assert.assertEquals(DurationUnit.HOUR, twoHours.getUnit());
        Assert.assertEquals(2, twoHours.getAmount());
        // VERIFY IMAGE/GIF
        ExpiresConfiguration expiresConfigurationImageGif = expiresFilter.getExpiresConfigurationByContentType().get("image/gif");
        Assert.assertEquals(StartingPoint.LAST_MODIFICATION_TIME, expiresConfigurationImageGif.getStartingPoint());
        Assert.assertEquals(2, expiresConfigurationImageGif.getDurations().size());
        Duration fiveHours = expiresConfigurationImageGif.getDurations().get(0);
        Assert.assertEquals(DurationUnit.HOUR, fiveHours.getUnit());
        Assert.assertEquals(5, fiveHours.getAmount());
        Duration threeMinutes = expiresConfigurationImageGif.getDurations().get(1);
        Assert.assertEquals(DurationUnit.MINUTE, threeMinutes.getUnit());
        Assert.assertEquals(3, threeMinutes.getAmount());
        // VERIFY IMAGE/JPG
        ExpiresConfiguration expiresConfigurationImageJpg = expiresFilter.getExpiresConfigurationByContentType().get("image/jpg");
        Assert.assertEquals(StartingPoint.ACCESS_TIME, expiresConfigurationImageJpg.getStartingPoint());
        Assert.assertEquals(1, expiresConfigurationImageJpg.getDurations().size());
        Duration tenThousandSeconds = expiresConfigurationImageJpg.getDurations().get(0);
        Assert.assertEquals(DurationUnit.SECOND, tenThousandSeconds.getUnit());
        Assert.assertEquals(10000, tenThousandSeconds.getAmount());
        // VERIFY VIDEO/MPEG
        ExpiresConfiguration expiresConfiguration = expiresFilter.getExpiresConfigurationByContentType().get("video/mpeg");
        Assert.assertEquals(StartingPoint.LAST_MODIFICATION_TIME, expiresConfiguration.getStartingPoint());
        Assert.assertEquals(1, expiresConfiguration.getDurations().size());
        Duration twentyThousandSeconds = expiresConfiguration.getDurations().get(0);
        Assert.assertEquals(DurationUnit.SECOND, twentyThousandSeconds.getUnit());
        Assert.assertEquals(20000, twentyThousandSeconds.getAmount());
    } finally {
        tomcat.stop();
    }
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) FilterDef(org.apache.tomcat.util.descriptor.web.FilterDef) ExpiresConfiguration(org.apache.catalina.filters.ExpiresFilter.ExpiresConfiguration) Duration(org.apache.catalina.filters.ExpiresFilter.Duration) FilterMap(org.apache.tomcat.util.descriptor.web.FilterMap) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Aggregations

FilterDef (org.apache.tomcat.util.descriptor.web.FilterDef)45 Test (org.junit.Test)24 FilterMap (org.apache.tomcat.util.descriptor.web.FilterMap)22 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)21 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)16 Context (org.apache.catalina.Context)13 HashMap (java.util.HashMap)11 Tomcat (org.apache.catalina.startup.Tomcat)10 File (java.io.File)6 ByteChunk (org.apache.tomcat.util.buf.ByteChunk)6 List (java.util.List)5 SecurityConstraint (org.apache.tomcat.util.descriptor.web.SecurityConstraint)5 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 Container (org.apache.catalina.Container)3 JavaClassCacheEntry (org.apache.catalina.startup.ContextConfig.JavaClassCacheEntry)3 IOException (java.io.IOException)2 LinkedHashMap (java.util.LinkedHashMap)2 StringTokenizer (java.util.StringTokenizer)2