Author: fschumacher Date: Wed Feb 10 20:56:55 2016 New Revision: 1729730 URL: http://svn.apache.org/viewvc?rev=1729730&view=rev Log: Fix https://bz.apache.org/bugzilla/show_bug.cgi?id=58988 Add quotation of special characters in substitutions of the RewriteValve.
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 tomcat/trunk/webapps/docs/rewrite.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=1729730&r1=1729729&r2=1729730&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/catalina/valves/rewrite/Substitution.java (original) +++ tomcat/trunk/java/org/apache/catalina/valves/rewrite/Substitution.java Wed Feb 10 20:56:55 2016 @@ -111,17 +111,27 @@ public class Substitution { int pos = 0; int percentPos = 0; int dollarPos = 0; + int backslashPos = 0; while (pos < sub.length()) { percentPos = sub.indexOf('%', pos); dollarPos = sub.indexOf('$', pos); - if (percentPos == -1 && dollarPos == -1) { + backslashPos = sub.indexOf('\\', pos); + if (percentPos == -1 && dollarPos == -1 && backslashPos == -1) { // Static text StaticElement newElement = new StaticElement(); newElement.value = sub.substring(pos, sub.length()); pos = sub.length(); elements.add(newElement); - } else if (percentPos == -1 || ((dollarPos != -1) && (dollarPos < percentPos))) { + } else if (isFirstPos(backslashPos, dollarPos, percentPos)) { + if (backslashPos + 1 == sub.length()) { + throw new IllegalArgumentException(sub); + } + StaticElement newElement = new StaticElement(); + newElement.value = sub.substring(pos, backslashPos) + sub.substring(backslashPos + 1, backslashPos + 2); + pos = backslashPos + 2; + elements.add(newElement); + } else if (isFirstPos(dollarPos, percentPos)) { // $: back reference to rule or map lookup if (dollarPos + 1 == sub.length()) { throw new IllegalArgumentException(sub); @@ -240,4 +250,28 @@ public class Substitution { } return buf.toString(); } + + /** + * Checks whether the first int is non negative and smaller than any non negative other int + * given with {@code others}. + * + * @param testPos + * integer to test against + * @param others + * list of integers that are paired against {@code testPos}. Any + * negative integer will be ignored. + * @return {@code true} if {@code testPos} is not negative and is less then any given other + * integer, {@code false} otherwise + */ + private boolean isFirstPos(int testPos, int... others) { + if (testPos < 0) { + return false; + } + for (int other : others) { + if (other >= 0 && other < testPos) { + return false; + } + } + return true; + } } 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=1729730&r1=1729729&r2=1729730&view=diff ============================================================================== --- tomcat/trunk/test/org/apache/catalina/valves/rewrite/TestRewriteValve.java (original) +++ tomcat/trunk/test/org/apache/catalina/valves/rewrite/TestRewriteValve.java Wed Feb 10 20:56:55 2016 @@ -33,6 +33,11 @@ public class TestRewriteValve extends To } @Test + public void testBackslashPercentSign() throws Exception { + doTestRewrite("RewriteRule ^(.*) /a/\\%5A", "/", "/a/%255A"); + } + + @Test public void testNoopRewrite() throws Exception { doTestRewrite("RewriteRule ^(.*) $1", "/a/%255A", "/a/%255A"); } Modified: tomcat/trunk/webapps/docs/changelog.xml URL: http://svn.apache.org/viewvc/tomcat/trunk/webapps/docs/changelog.xml?rev=1729730&r1=1729729&r2=1729730&view=diff ============================================================================== --- tomcat/trunk/webapps/docs/changelog.xml (original) +++ tomcat/trunk/webapps/docs/changelog.xml Wed Feb 10 20:56:55 2016 @@ -70,6 +70,10 @@ WAR into the <code>appBase</code> and using the newly created expanded directory as the <code>docBase</code>. (markt) </fix> + <add> + <bug>58988</bug>: Special characters in the substitutions for the RewriteValve + can now be quoted with a backslash. (fschumacher) + </add> </changelog> </subsection> <subsection name="Coyote"> Modified: tomcat/trunk/webapps/docs/rewrite.xml URL: http://svn.apache.org/viewvc/tomcat/trunk/webapps/docs/rewrite.xml?rev=1729730&r1=1729729&r2=1729730&view=diff ============================================================================== --- tomcat/trunk/webapps/docs/rewrite.xml (original) +++ tomcat/trunk/webapps/docs/rewrite.xml Wed Feb 10 20:56:55 2016 @@ -520,6 +520,10 @@ cannot use <code>$N</code> in the substi or it is explicitly terminated by a <code><strong>L</strong></code> flag.</p> + <p>The special characters <code>$</code> and <code>%</code> can + be quoted by prepending them with a backslash character + <code>\</code>.</p> + <p>There is a special substitution string named '<code>-</code>' which means: <strong>NO substitution</strong>! This is useful in providing --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org