This is an automated email from the ASF dual-hosted git repository.

markt-asf pushed a commit to branch 9.0.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git


The following commit(s) were added to refs/heads/9.0.x by this push:
     new 5c1e333c7a Fail closed when the rewrite [N] round limit is reached
5c1e333c7a is described below

commit 5c1e333c7abfa83b78b3856942fc32c1e15b6099
Author: Mark Thomas <[email protected]>
AuthorDate: Wed Nov 25 13:18:41 2026 +0000

    Fail closed when the rewrite [N] round limit is reached
    
    When the limit on rounds triggered by the next flag was reached, the
    rewriting stopped and the partly rewritten URL was dispatched without
    the remaining rules, such as a forbidden rule, having been applied to
    it. A request repeating the pattern of a converging next rule more than
    the limit could therefore reach a resource that the configuration
    forbids.
    
    Reject the request with a 500 response instead. Update the existing
    test and add tests that cover a forbidden rule following a converging
    next rule, within and beyond the limit.
    
    Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
---
 .../catalina/valves/rewrite/RewriteValve.java      |  2 ++
 .../catalina/valves/rewrite/TestRewriteValve.java  | 42 +++++++++++++++++-----
 webapps/docs/changelog.xml                         |  6 ++++
 3 files changed, 41 insertions(+), 9 deletions(-)

diff --git a/java/org/apache/catalina/valves/rewrite/RewriteValve.java 
b/java/org/apache/catalina/valves/rewrite/RewriteValve.java
index 200a90cc5e..84d8a893a1 100644
--- a/java/org/apache/catalina/valves/rewrite/RewriteValve.java
+++ b/java/org/apache/catalina/valves/rewrite/RewriteValve.java
@@ -563,6 +563,8 @@ public class RewriteValve extends ValveBase {
                     if (rounds >= MAX_REWRITE_ROUNDS) {
                         
containerLog.warn(sm.getString("rewriteValve.maxRounds",
                                 Integer.valueOf(MAX_REWRITE_ROUNDS), 
request.getRequestURI()));
+                        
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
+                        done = true;
                         break;
                     }
                     rounds++;
diff --git a/test/org/apache/catalina/valves/rewrite/TestRewriteValve.java 
b/test/org/apache/catalina/valves/rewrite/TestRewriteValve.java
index f2f1222296..85ed033b82 100644
--- a/test/org/apache/catalina/valves/rewrite/TestRewriteValve.java
+++ b/test/org/apache/catalina/valves/rewrite/TestRewriteValve.java
@@ -303,6 +303,38 @@ public class TestRewriteValve extends TomcatBaseTest {
         // state at which rewriting stops by itself
         String config = "RewriteRule ^/c/x$ /c/y [N]\n" + "RewriteRule ^/c/y$ 
/c/x [N]\n";
 
+        // Rewriting must stop after a bounded number of rounds and the
+        // request must complete. It must fail closed rather than be
+        // dispatched with a URL that the remaining rules have not seen.
+        Assert.assertEquals(500, doTestNextRounds(config, "/c/x"));
+    }
+
+    private static final String NEXT_THEN_FORBIDDEN =
+            "RewriteRule ^/c/a/(.*)$ /c/$1 [N]\n" + "RewriteRule ^/c/secret$ - 
[F]\n";
+
+    @Test(timeout = 30000)
+    public void testRewriteRuleNextWithinBoundThenForbidden() throws Exception 
{
+        // The next rule converges, needing one round per path segment, and
+        // the later forbidden rule applies to the final URL
+        Assert.assertEquals(403, doTestNextRounds(NEXT_THEN_FORBIDDEN, "/c/" + 
repeat("a/", 10) + "secret"));
+    }
+
+    @Test(timeout = 30000)
+    public void testRewriteRuleNextBeyondBoundDoesNotBypassForbidden() throws 
Exception {
+        // Beyond the bound the request must not reach the servlet with a
+        // URL that the forbidden rule has not been applied to
+        Assert.assertEquals(500, doTestNextRounds(NEXT_THEN_FORBIDDEN, "/c/" + 
repeat("a/", 1100) + "secret"));
+    }
+
+    private static String repeat(String s, int count) {
+        StringBuilder sb = new StringBuilder(s.length() * count);
+        for (int i = 0; i < count; i++) {
+            sb.append(s);
+        }
+        return sb.toString();
+    }
+
+    private int doTestNextRounds(String config, String path) throws Exception {
         Tomcat tomcat = getTomcatInstance();
         Context ctx = getProgrammaticRootContext();
 
@@ -316,16 +348,8 @@ public class TestRewriteValve extends TomcatBaseTest {
         tomcat.start();
 
         ByteChunk res = new ByteChunk();
-        int rc = methodUrl("http://localhost:"; + getPort() + "/c/x", res, 
DEFAULT_CLIENT_TIMEOUT_MS, null, null,
+        return methodUrl("http://localhost:"; + getPort() + path, res, 
DEFAULT_CLIENT_TIMEOUT_MS, null, null,
                 Method.GET, true);
-        res.setCharset(StandardCharsets.UTF_8);
-
-        // Rewriting must stop after a bounded number of rounds and the
-        // request must complete, mapped to the servlet thanks to the last
-        // rewrite that was applied
-        Assert.assertEquals(200, rc);
-        String requestURI = 
SnoopResult.parse(res.toString()).getRequestInfo("REQUEST-URI");
-        Assert.assertTrue(requestURI, "/c/x".equals(requestURI) || 
"/c/y".equals(requestURI));
     }
 
     @Test
diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml
index fe8d6ebbc5..8450aeba97 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -148,6 +148,12 @@
         Make handling of <code>allowSpecialPaths</code> within the WebDAV
         servlet consistent for all WebDAV methods. (markt)
       </fix>
+      <fix>
+        When the rewrite valve reaches the limit on the number of rounds
+        triggered by the <code>[N]</code> (next) flag, reject the request with 
a
+        500 response rather than processing it using a URL that the remaining
+        rewrite rules have not been applied to. (markt)
+      </fix>
     </changelog>
   </subsection>
   <subsection name="Coyote">


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to