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

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


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

commit 201f6ed1f8a301eab0034191a81eb8e5a73cc282
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  | 34 ++++++++++++++++------
 webapps/docs/changelog.xml                         |  6 ++++
 3 files changed, 33 insertions(+), 9 deletions(-)

diff --git a/java/org/apache/catalina/valves/rewrite/RewriteValve.java 
b/java/org/apache/catalina/valves/rewrite/RewriteValve.java
index 2d6138e9c8..8edfc47aae 100644
--- a/java/org/apache/catalina/valves/rewrite/RewriteValve.java
+++ b/java/org/apache/catalina/valves/rewrite/RewriteValve.java
@@ -561,6 +561,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 9611b87196..d01c6dd1f8 100644
--- a/test/org/apache/catalina/valves/rewrite/TestRewriteValve.java
+++ b/test/org/apache/catalina/valves/rewrite/TestRewriteValve.java
@@ -303,6 +303,30 @@ 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/" + 
"a/".repeat(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/" + 
"a/".repeat(1100) + "secret"));
+    }
+
+    private int doTestNextRounds(String config, String path) throws Exception {
         Tomcat tomcat = getTomcatInstance();
         Context ctx = getProgrammaticRootContext();
 
@@ -316,16 +340,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 ccef424be5..369b643f04 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