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

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


The following commit(s) were added to refs/heads/main by this push:
     new 0280377251 Reject a chunk header line if parsing resumes after a bare 
CR
0280377251 is described below

commit 0280377251f13782579152e7b3c5f3cc101cad03
Author: Mark Thomas <[email protected]>
AuthorDate: Wed Nov 25 10:51:40 2026 +0000

    Reject a chunk header line if parsing resumes after a bare CR
    
    If parsing of a chunk header was resumed after the CR had been consumed
    (available() pre-parsing the header, or a non-blocking read ending after
    the CR) the following characters were parsed as part of the chunk size and
    a bare LF was accepted using the stale CR flag. Once a CR has been
    consumed, only an LF may follow.
    
    Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
---
 .../coyote/http11/filters/ChunkedInputFilter.java  |  22 ++++
 .../http11/filters/TestChunkedInputFilter.java     | 134 +++++++++++++++++++++
 webapps/docs/changelog.xml                         |   4 +
 3 files changed, 160 insertions(+)

diff --git a/java/org/apache/coyote/http11/filters/ChunkedInputFilter.java 
b/java/org/apache/coyote/http11/filters/ChunkedInputFilter.java
index 42a4f53ad2..1013800cb9 100644
--- a/java/org/apache/coyote/http11/filters/ChunkedInputFilter.java
+++ b/java/org/apache/coyote/http11/filters/ChunkedInputFilter.java
@@ -368,6 +368,17 @@ public class ChunkedInputFilter implements InputFilter, 
ApplicationBufferHandler
 
         boolean eol = false;
 
+        if (crFound) {
+            /*
+             * The CR at the end of the chunk header line has already been 
consumed. Only the LF may follow. Don't
+             * resume parsing the chunk size or extension.
+             */
+            if (!parseCRLF()) {
+                return false;
+            }
+            eol = true;
+        }
+
         while (!eol) {
             if (!fill()) {
                 return false;
@@ -446,6 +457,17 @@ public class ChunkedInputFilter implements InputFilter, 
ApplicationBufferHandler
 
         boolean eol = false;
 
+        if (crFound) {
+            /*
+             * The CR at the end of the chunk header line has already been 
consumed. Only the LF may follow. Don't
+             * resume parsing the chunk size or extension.
+             */
+            if (!skipCRLF()) {
+                return false;
+            }
+            eol = true;
+        }
+
         while (!eol) {
             if (readChunk == null || readChunk.position() >= 
readChunk.limit()) {
                 return false;
diff --git a/test/org/apache/coyote/http11/filters/TestChunkedInputFilter.java 
b/test/org/apache/coyote/http11/filters/TestChunkedInputFilter.java
index 4fe19cc53f..4e8d17f13e 100644
--- a/test/org/apache/coyote/http11/filters/TestChunkedInputFilter.java
+++ b/test/org/apache/coyote/http11/filters/TestChunkedInputFilter.java
@@ -745,6 +745,140 @@ public class TestChunkedInputFilter extends 
TomcatBaseTest {
         }
     }
 
+    /*
+     * A bare CR in the chunk header line must be rejected regardless of 
whether available() has been called to
+     * pre-parse the chunk header.
+     */
+    @Test
+    public void testChunkHeaderBareCrBareLf() throws Exception {
+        doTestChunkHeaderBareCr("2\r5\n", false);
+    }
+
+    @Test
+    public void testChunkHeaderBareCrBareLfAvailable() throws Exception {
+        doTestChunkHeaderBareCr("2\r5\n", true);
+    }
+
+    @Test
+    public void testChunkHeaderExtensionBareCrBareLf() throws Exception {
+        doTestChunkHeaderBareCr("2;a\r5\n", false);
+    }
+
+    @Test
+    public void testChunkHeaderExtensionBareCrBareLfAvailable() throws 
Exception {
+        doTestChunkHeaderBareCr("2;a\r5\n", true);
+    }
+
+    private void doTestChunkHeaderBareCr(String secondChunkHeader, boolean 
callAvailable) throws Exception {
+        Tomcat tomcat = getTomcatInstance();
+
+        // No file system docBase required
+        Context ctx = getProgrammaticRootContext();
+
+        AvailableBodyServlet servlet = new AvailableBodyServlet(callAvailable);
+        Tomcat.addServlet(ctx, "servlet", servlet);
+        ctx.addServletMapping("/", "servlet");
+
+        tomcat.start();
+
+        // The second chunk header is invalid. If it were accepted, the chunk 
size would be 0x25 (37)
+        // @formatter:off
+        String request =
+                "POST / HTTP/1.1" + CRLF +
+                "Host: any" + CRLF +
+                "Transfer-encoding: chunked" + CRLF +
+                "Content-Type: text/plain" + CRLF +
+                "Connection: close" + CRLF +
+                CRLF +
+                "3" + CRLF +
+                "abc" + CRLF +
+                secondChunkHeader +
+                "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" + CRLF +
+                "0" + CRLF +
+                CRLF;
+        // @formatter:on
+
+        TrailerClient client = new 
TrailerClient(tomcat.getConnector().getLocalPort());
+        client.setUseContentLength(true);
+        client.setRequest(new String[] { request });
+
+        Exception processException = null;
+        client.connect();
+        try {
+            client.processRequest();
+            client.disconnect();
+        } catch (Exception e) {
+            // Socket was probably closed before client had a chance to read 
response
+            processException = e;
+        }
+
+        if (processException == null) {
+            Assert.assertTrue(client.getResponseLine(), 
client.isResponse400());
+        }
+        Assert.assertTrue("Invalid chunk header was accepted", 
servlet.getExceptionDuringRead());
+        Assert.assertEquals("Data from the invalid chunk was read", 3, 
servlet.getCountRead());
+    }
+
+
+    private static class AvailableBodyServlet extends HttpServlet {
+        private static final long serialVersionUID = 1L;
+
+        private final boolean callAvailable;
+        private volatile boolean exceptionDuringRead = false;
+        private volatile int countRead = 0;
+
+        AvailableBodyServlet(boolean callAvailable) {
+            this.callAvailable = callAvailable;
+        }
+
+        @Override
+        protected void doPost(HttpServletRequest req, HttpServletResponse 
resp) throws ServletException, IOException {
+            resp.setContentType("text/plain");
+
+            InputStream is = req.getInputStream();
+            try {
+                // Read exactly the first chunk so the next read starts at the 
second chunk header
+                byte[] first = new byte[3];
+                int read = 0;
+                while (read < first.length) {
+                    int n = is.read(first, read, first.length - read);
+                    if (n < 0) {
+                        break;
+                    }
+                    read += n;
+                }
+                countRead = read;
+
+                if (callAvailable) {
+                    // Triggers pre-parsing of the next chunk header
+                    is.available();
+                }
+
+                byte[] buf = new byte[64];
+                int n;
+                while ((n = is.read(buf)) > 0) {
+                    countRead += n;
+                }
+            } catch (IOException ioe) {
+                exceptionDuringRead = true;
+                if (!resp.isCommitted()) {
+                    
resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
+                }
+                return;
+            }
+            resp.getWriter().write(Integer.toString(countRead));
+        }
+
+        public boolean getExceptionDuringRead() {
+            return exceptionDuringRead;
+        }
+
+        public int getCountRead() {
+            return countRead;
+        }
+    }
+
+
     private static class BodyReadServlet extends HttpServlet {
         private static final long serialVersionUID = 1L;
 
diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml
index 10e791e48a..36907d1e8d 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -401,6 +401,10 @@
         Previously, a body packet that had not been read could be processed as
         the start of a new AJP request. (markt)
       </fix>
+      <fix>
+        Fix the parsing of chunk header lines so that a CR that has been 
consumed
+        is always followed by an LF. (markt)
+      </fix>
     </changelog>
   </subsection>
   <subsection name="Jasper">


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

Reply via email to