|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.tomcat.security; |
| 19 | + |
| 20 | +import java.io.File; |
| 21 | +import java.io.FileWriter; |
| 22 | + |
| 23 | +import jakarta.servlet.http.HttpServletResponse; |
| 24 | + |
| 25 | +import org.junit.Assert; |
| 26 | +import org.junit.Test; |
| 27 | + |
| 28 | +import org.apache.catalina.Context; |
| 29 | +import org.apache.catalina.servlets.DefaultServlet; |
| 30 | +import org.apache.catalina.ssi.SSIFilter; |
| 31 | +import org.apache.catalina.ssi.SSIServlet; |
| 32 | +import org.apache.catalina.startup.Tomcat; |
| 33 | +import org.apache.catalina.startup.TomcatBaseTest; |
| 34 | +import org.apache.tomcat.util.buf.ByteChunk; |
| 35 | +import org.apache.tomcat.util.descriptor.web.FilterDef; |
| 36 | +import org.apache.tomcat.util.descriptor.web.FilterMap; |
| 37 | + |
| 38 | +public class TestSecurity2019 extends TomcatBaseTest { |
| 39 | + |
| 40 | + // https://www.cve.org/CVERecord?id=CVE-2019-0221 |
| 41 | + @Test |
| 42 | + public void testCVE_2019_0221_01() throws Exception { |
| 43 | + Tomcat tomcat = getTomcatInstance(); |
| 44 | + |
| 45 | + File appDir = new File(getTemporaryDirectory(), "ssitest"); |
| 46 | + Assert.assertTrue(appDir.mkdirs()); |
| 47 | + addDeleteOnTearDown(appDir); |
| 48 | + |
| 49 | + File shtml = new File(appDir, "printenv.shtml"); |
| 50 | + try (FileWriter fw = new FileWriter(shtml)) { |
| 51 | + fw.write("<!--#printenv -->"); |
| 52 | + } |
| 53 | + |
| 54 | + Context ctx = tomcat.addContext("", appDir.getAbsolutePath()); |
| 55 | + |
| 56 | + Tomcat.addServlet(ctx, "ssi", new SSIServlet()); |
| 57 | + ctx.addServletMappingDecoded("*.shtml", "ssi"); |
| 58 | + |
| 59 | + ctx.setPrivileged(true); |
| 60 | + |
| 61 | + tomcat.start(); |
| 62 | + |
| 63 | + ByteChunk res = new ByteChunk(); |
| 64 | + int rc = getUrl("http://localhost:" + getPort() + "/printenv.shtml?%3Ch1%3EXSS%3C/h1%3E", res, null); |
| 65 | + Assert.assertEquals(HttpServletResponse.SC_OK, rc); |
| 66 | + Assert.assertFalse("SSI printenv should not render unescaped HTML ", res.toString().contains("<h1>")); |
| 67 | + |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void testCVE_2019_0221_02() throws Exception { |
| 72 | + Tomcat tomcat = getTomcatInstance(); |
| 73 | + |
| 74 | + File appDir = new File(getTemporaryDirectory(), "ssitest"); |
| 75 | + Assert.assertTrue(appDir.mkdirs()); |
| 76 | + addDeleteOnTearDown(appDir); |
| 77 | + |
| 78 | + File shtml = new File(appDir, "printenv.shtml"); |
| 79 | + try (FileWriter fw = new FileWriter(shtml)) { |
| 80 | + fw.write("<!--#printenv -->"); |
| 81 | + } |
| 82 | + |
| 83 | + Context ctx = tomcat.addContext("", appDir.getAbsolutePath()); |
| 84 | + |
| 85 | + FilterDef filterDef = new FilterDef(); |
| 86 | + filterDef.setFilterClass(SSIFilter.class.getName()); |
| 87 | + filterDef.setFilterName("ssi"); |
| 88 | + ctx.addFilterDef(filterDef); |
| 89 | + |
| 90 | + FilterMap filterMap = new FilterMap(); |
| 91 | + filterMap.setFilterName("ssi"); |
| 92 | + filterMap.addURLPatternDecoded("*.shtml"); |
| 93 | + ctx.addFilterMap(filterMap); |
| 94 | + |
| 95 | + Tomcat.addServlet(ctx, "default", new DefaultServlet()); |
| 96 | + ctx.addServletMappingDecoded("/", "default"); |
| 97 | + ctx.addMimeMapping("shtml", "text/x-server-parsed-html"); |
| 98 | + |
| 99 | + ctx.setPrivileged(true); |
| 100 | + |
| 101 | + tomcat.start(); |
| 102 | + |
| 103 | + ByteChunk res = new ByteChunk(); |
| 104 | + int rc = getUrl("http://localhost:" + getPort() + "/printenv.shtml?%3Ch1%3EXSS%3C/h1%3E", res, null); |
| 105 | + Assert.assertEquals(HttpServletResponse.SC_OK, rc); |
| 106 | + Assert.assertFalse("SSI printenv should not render unescaped HTML ", res.toString().contains("<h1>")); |
| 107 | + } |
| 108 | +} |
0 commit comments