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
commit 2ff21c1c64e75a51e494d2247fdd7aec510ffb1e Author: Mark Thomas <[email protected]> AuthorDate: Mon Nov 23 16:53:24 2026 +0000 Close the AJP connection after a request has ended in error recycle() resets the error state so service() carried on reading messages after a request had been rejected. A body packet that had already been sent but not read was then processed as the start of a new AJP request. Co-Authored-By: Claude Sonnet 5.5 <[email protected]> --- java/org/apache/coyote/ajp/AjpProcessor.java | 13 ++- test/org/apache/coyote/ajp/TestAjpProcessor.java | 124 +++++++++++++++++++++++ webapps/docs/changelog.xml | 6 ++ 3 files changed, 142 insertions(+), 1 deletion(-) diff --git a/java/org/apache/coyote/ajp/AjpProcessor.java b/java/org/apache/coyote/ajp/AjpProcessor.java index 61d6f64d72..7a5fad43c2 100644 --- a/java/org/apache/coyote/ajp/AjpProcessor.java +++ b/java/org/apache/coyote/ajp/AjpProcessor.java @@ -342,6 +342,8 @@ public class AjpProcessor extends AbstractProcessor { // Expected to block on the first read as there should be at least one // AJP message to read. boolean firstRead = true; + // recycle() resets the error state so track separately if the connection must be closed + boolean closeConnection = false; while (!getErrorState().isError() && !protocol.isPaused()) { // Parsing the request header @@ -468,12 +470,21 @@ public class AjpProcessor extends AbstractProcessor { // Set keep alive timeout for next request socketWrapper.setReadTimeout(protocol.getKeepAliveTimeout()); + /* + * Recycling resets the error state. If the request ended in error, the connection must be closed without + * reading any further messages. Any unread body packet is still on the connection and must not be parsed + * as the start of the next request. + */ + closeConnection = getErrorState().isError(); recycle(); + if (closeConnection) { + break; + } } rp.setStage(org.apache.coyote.Constants.STAGE_ENDED); - if (getErrorState().isError() || protocol.isPaused()) { + if (closeConnection || getErrorState().isError() || protocol.isPaused()) { return SocketState.CLOSED; } else { if (isAsync()) { diff --git a/test/org/apache/coyote/ajp/TestAjpProcessor.java b/test/org/apache/coyote/ajp/TestAjpProcessor.java new file mode 100644 index 0000000000..dd5cdb0fbb --- /dev/null +++ b/test/org/apache/coyote/ajp/TestAjpProcessor.java @@ -0,0 +1,124 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.coyote.ajp; + +import java.io.IOException; +import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; + +import jakarta.servlet.http.HttpServlet; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; + +import org.junit.Assert; +import org.junit.Test; + +import org.apache.catalina.Context; +import org.apache.catalina.startup.Tomcat; + +public class TestAjpProcessor extends AjpBaseTest { + + /* + * When a request is rejected before the request body is read, the body packet that the proxy has already sent must + * not be processed as the start of a new AJP request. + */ + @Test + public void testUnreadBodyPacketAfterRejectedRequest() throws Exception { + + Tomcat tomcat = getTomcatInstance(); + + // No file system docBase required + Context ctx = getProgrammaticRootContext(); + + RecordingServlet servlet = new RecordingServlet(); + Tomcat.addServlet(ctx, "recording", servlet); + ctx.addServletMapping("/", "recording"); + + tomcat.start(); + + SimpleAjpClient ajpClient = new SimpleAjpClient(); + ajpClient.setPort(getPort()); + ajpClient.connect(); + + /* + * Build the body of the first body packet so that, if it is read as the next AJP message, it is a valid forward + * request for /injected. The two byte chunk length (514 = 0x0202) is read as the message type (FORWARD_REQUEST) + * and the method (GET) and the data that follows is read as the remainder of the forward request. + */ + int bodyLength = 514; + ajpClient.setMethod("GET"); + ajpClient.setUri("/injected"); + TesterAjpMessage injectedMessage = ajpClient.createForwardMessage(); + injectedMessage.end(); + byte[] injectedBuffer = injectedMessage.getBuffer(); + // Skip the 4 byte header, the message type and the method + int injectedLength = injectedMessage.getLen() - 6; + Assert.assertTrue(injectedLength <= bodyLength); + byte[] body = new byte[bodyLength]; + System.arraycopy(injectedBuffer, 6, body, 0, injectedLength); + + // The request is rejected because of the invalid host name + ajpClient.setMethod("POST"); + ajpClient.setUri("/rejected"); + TesterAjpMessage forwardMessage = ajpClient.createForwardMessage(); + forwardMessage.addHeader("Host", "invalid_host"); + forwardMessage.addHeader("Content-Length", Integer.toString(bodyLength)); + forwardMessage.end(); + + TesterAjpMessage responseHeaders = ajpClient.sendMessage(forwardMessage, ajpClient.createBodyMessage(body)); + TestAbstractAjpProcessor.validateResponseHeaders(responseHeaders, 400, "400"); + // Response body + ajpClient.readMessage(); + // End of response. The connection is not to be re-used. + TesterAjpMessage endMessage = ajpClient.readMessage(); + Assert.assertEquals(0x05, endMessage.getByte()); + Assert.assertEquals(0x00, endMessage.getByte()); + + // The connection should now be closed without any further messages + try { + ajpClient.readMessage(); + Assert.fail("Unexpected message received. The connection should have been closed"); + } catch (IOException expected) { + // Expected + } + + Assert.assertFalse("The unread body packet was processed as a request: " + servlet.getUris(), + servlet.getUris().contains("/injected")); + + ajpClient.disconnect(); + } + + + static final class RecordingServlet extends HttpServlet { + + private static final long serialVersionUID = 1L; + + private final List<String> uris = new CopyOnWriteArrayList<>(); + + @Override + protected void service(HttpServletRequest req, HttpServletResponse resp) throws IOException { + uris.add(req.getRequestURI()); + resp.setContentType("text/plain"); + resp.getWriter().print("OK"); + } + + List<String> getUris() { + return uris; + } + } + +} diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml index 7193483ac7..d8ca54f432 100644 --- a/webapps/docs/changelog.xml +++ b/webapps/docs/changelog.xml @@ -384,6 +384,12 @@ get body chunk message or flush message so that the messages cannot be written part way through a response body message. (markt) </fix> + <fix> + Fix the AJP connector so that it closes the connection, rather than + reading further messages, once a request has ended in error. + Previously, a body packet that had not been read could be processed as + the start of a new AJP request. (markt) + </fix> </changelog> </subsection> <subsection name="Jasper"> --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
