|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.coyote.ajp; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.util.List; |
| 21 | +import java.util.concurrent.CopyOnWriteArrayList; |
| 22 | + |
| 23 | +import jakarta.servlet.http.HttpServlet; |
| 24 | +import jakarta.servlet.http.HttpServletRequest; |
| 25 | +import jakarta.servlet.http.HttpServletResponse; |
| 26 | + |
| 27 | +import org.junit.Assert; |
| 28 | +import org.junit.Test; |
| 29 | + |
| 30 | +import org.apache.catalina.Context; |
| 31 | +import org.apache.catalina.startup.Tomcat; |
| 32 | + |
| 33 | +public class TestAjpProcessor extends AjpBaseTest { |
| 34 | + |
| 35 | + /* |
| 36 | + * When a request is rejected before the request body is read, the body packet that the proxy has already sent must |
| 37 | + * not be processed as the start of a new AJP request. |
| 38 | + */ |
| 39 | + @Test |
| 40 | + public void testUnreadBodyPacketAfterRejectedRequest() throws Exception { |
| 41 | + |
| 42 | + Tomcat tomcat = getTomcatInstance(); |
| 43 | + |
| 44 | + // No file system docBase required |
| 45 | + Context ctx = getProgrammaticRootContext(); |
| 46 | + |
| 47 | + RecordingServlet servlet = new RecordingServlet(); |
| 48 | + Tomcat.addServlet(ctx, "recording", servlet); |
| 49 | + ctx.addServletMapping("/", "recording"); |
| 50 | + |
| 51 | + tomcat.start(); |
| 52 | + |
| 53 | + SimpleAjpClient ajpClient = new SimpleAjpClient(); |
| 54 | + ajpClient.setPort(getPort()); |
| 55 | + ajpClient.connect(); |
| 56 | + |
| 57 | + /* |
| 58 | + * Build the body of the first body packet so that, if it is read as the next AJP message, it is a valid forward |
| 59 | + * request for /injected. The two byte chunk length (514 = 0x0202) is read as the message type (FORWARD_REQUEST) |
| 60 | + * and the method (GET) and the data that follows is read as the remainder of the forward request. |
| 61 | + */ |
| 62 | + int bodyLength = 514; |
| 63 | + ajpClient.setMethod("GET"); |
| 64 | + ajpClient.setUri("/injected"); |
| 65 | + TesterAjpMessage injectedMessage = ajpClient.createForwardMessage(); |
| 66 | + injectedMessage.end(); |
| 67 | + byte[] injectedBuffer = injectedMessage.getBuffer(); |
| 68 | + // Skip the 4 byte header, the message type and the method |
| 69 | + int injectedLength = injectedMessage.getLen() - 6; |
| 70 | + Assert.assertTrue(injectedLength <= bodyLength); |
| 71 | + byte[] body = new byte[bodyLength]; |
| 72 | + System.arraycopy(injectedBuffer, 6, body, 0, injectedLength); |
| 73 | + |
| 74 | + // The request is rejected because of the invalid host name |
| 75 | + ajpClient.setMethod("POST"); |
| 76 | + ajpClient.setUri("/rejected"); |
| 77 | + TesterAjpMessage forwardMessage = ajpClient.createForwardMessage(); |
| 78 | + forwardMessage.addHeader("Host", "invalid_host"); |
| 79 | + forwardMessage.addHeader("Content-Length", Integer.toString(bodyLength)); |
| 80 | + forwardMessage.end(); |
| 81 | + |
| 82 | + TesterAjpMessage responseHeaders = ajpClient.sendMessage(forwardMessage, ajpClient.createBodyMessage(body)); |
| 83 | + TestAbstractAjpProcessor.validateResponseHeaders(responseHeaders, 400, "400"); |
| 84 | + // Response body |
| 85 | + ajpClient.readMessage(); |
| 86 | + // End of response. The connection is not to be re-used. |
| 87 | + TesterAjpMessage endMessage = ajpClient.readMessage(); |
| 88 | + Assert.assertEquals(0x05, endMessage.getByte()); |
| 89 | + Assert.assertEquals(0x00, endMessage.getByte()); |
| 90 | + |
| 91 | + // The connection should now be closed without any further messages |
| 92 | + try { |
| 93 | + ajpClient.readMessage(); |
| 94 | + Assert.fail("Unexpected message received. The connection should have been closed"); |
| 95 | + } catch (IOException expected) { |
| 96 | + // Expected |
| 97 | + } |
| 98 | + |
| 99 | + Assert.assertFalse("The unread body packet was processed as a request: " + servlet.getUris(), |
| 100 | + servlet.getUris().contains("/injected")); |
| 101 | + |
| 102 | + ajpClient.disconnect(); |
| 103 | + } |
| 104 | + |
| 105 | + |
| 106 | + static final class RecordingServlet extends HttpServlet { |
| 107 | + |
| 108 | + private static final long serialVersionUID = 1L; |
| 109 | + |
| 110 | + private final List<String> uris = new CopyOnWriteArrayList<>(); |
| 111 | + |
| 112 | + @Override |
| 113 | + protected void service(HttpServletRequest req, HttpServletResponse resp) throws IOException { |
| 114 | + uris.add(req.getRequestURI()); |
| 115 | + resp.setContentType("text/plain"); |
| 116 | + resp.getWriter().print("OK"); |
| 117 | + } |
| 118 | + |
| 119 | + List<String> getUris() { |
| 120 | + return uris; |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | +} |
0 commit comments