Author: remm Date: Wed Sep 19 14:25:24 2018 New Revision: 1841347 URL: http://svn.apache.org/viewvc?rev=1841347&view=rev Log: 62737: Fix rewrite substitutions parsing of {} nesting.
Modified: tomcat/trunk/java/org/apache/catalina/valves/rewrite/Substitution.java tomcat/trunk/test/org/apache/catalina/valves/rewrite/TestRewriteValve.java tomcat/trunk/webapps/docs/changelog.xml Modified: tomcat/trunk/java/org/apache/catalina/valves/rewrite/Substitution.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/valves/rewrite/Substitution.java?rev=1841347&r1=1841346&r2=1841347&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/catalina/valves/rewrite/Substitution.java (original) +++ tomcat/trunk/java/org/apache/catalina/valves/rewrite/Substitution.java Wed Sep 19 14:25:24 2018 @@ -176,9 +176,9 @@ public class Substitution { // $: map lookup as ${mapname:key|default} MapElement newElement = new MapElement(); int open = sub.indexOf('{', dollarPos); - int colon = sub.indexOf(':', dollarPos); - int def = sub.indexOf('|', dollarPos); - int close = sub.indexOf('}', dollarPos); + int colon = findMatchingColonOrBar(true, sub, open); + int def = findMatchingColonOrBar(false, sub, open); + int close = findMatchingBrace(sub, open); if (!(-1 < open && open < colon && colon < close)) { throw new IllegalArgumentException(sub); } @@ -228,8 +228,8 @@ public class Substitution { // %: server variable as %{variable} SubstitutionElement newElement = null; int open = sub.indexOf('{', percentPos); - int colon = sub.indexOf(':', percentPos); - int close = sub.indexOf('}', percentPos); + int colon = findMatchingColonOrBar(true, sub, open); + int close = findMatchingBrace(sub, open); if (!(-1 < open && open < close)) { throw new IllegalArgumentException(sub); } @@ -263,6 +263,45 @@ public class Substitution { } + private static int findMatchingBrace(String sub, int start) { + int nesting = 1; + for (int i = start + 1; i < sub.length(); i++) { + char c = sub.charAt(i); + if (c == '{') { + char previousChar = sub.charAt(i-1); + if (previousChar == '$' || previousChar == '%') { + nesting++; + } + } else if (c == '}') { + nesting--; + if (nesting == 0) { + return i; + } + } + } + return -1; + } + + private static int findMatchingColonOrBar(boolean colon, String sub, int start) { + int nesting = 0; + for (int i = start + 1; i < sub.length(); i++) { + char c = sub.charAt(i); + if (c == '{') { + char previousChar = sub.charAt(i-1); + if (previousChar == '$' || previousChar == '%') { + nesting++; + } + } else if (c == '}') { + nesting--; + } else if (colon ? c == ':' : c =='|') { + if (nesting == 0) { + return i; + } + } + } + return -1; + } + /** * Evaluate the substitution based on the context. * @param rule corresponding matched rule Modified: tomcat/trunk/test/org/apache/catalina/valves/rewrite/TestRewriteValve.java URL: http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/catalina/valves/rewrite/TestRewriteValve.java?rev=1841347&r1=1841346&r2=1841347&view=diff ============================================================================== --- tomcat/trunk/test/org/apache/catalina/valves/rewrite/TestRewriteValve.java (original) +++ tomcat/trunk/test/org/apache/catalina/valves/rewrite/TestRewriteValve.java Wed Sep 19 14:25:24 2018 @@ -101,6 +101,12 @@ public class TestRewriteValve extends To } @Test + public void testRewriteMap06() throws Exception { + doTestRewrite("RewriteMap mapa org.apache.catalina.valves.rewrite.TesterRewriteMapA\n" + + "RewriteRule /b/.* /c/${mapa:${mapa:a}}", "/b/a.html", "/c/aaaa"); + } + + @Test public void testRewriteServerVar() throws Exception { doTestRewrite("RewriteRule /b/(.*).html$ /c%{SERVLET_PATH}", "/b/x.html", "/c/b/x.html"); } Modified: tomcat/trunk/webapps/docs/changelog.xml URL: http://svn.apache.org/viewvc/tomcat/trunk/webapps/docs/changelog.xml?rev=1841347&r1=1841346&r2=1841347&view=diff ============================================================================== --- tomcat/trunk/webapps/docs/changelog.xml (original) +++ tomcat/trunk/webapps/docs/changelog.xml Wed Sep 19 14:25:24 2018 @@ -63,6 +63,10 @@ <bug>62687</bug>: Expose content length information for resources when using a compressed war. (remm) </fix> + <fix> + <bug>62737</bug>: Fix rewrite substitutions parsing of {} nesting. + (remm) + </fix> </changelog> </subsection> <subsection name="Coyote"> --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org