Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport for CVE-2023-46589 #36

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions java/org/apache/catalina/connector/ClientAbortException.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@
*/
package org.apache.catalina.connector;

import java.io.IOException;
import org.apache.coyote.BadRequestException;

/**
* Extend IOException to identify it as being caused by an abort of a request by
* a remote client.
*
* @author Glenn L. Nielsen
*/
public final class ClientAbortException extends IOException {
public final class ClientAbortException extends BadRequestException {

private static final long serialVersionUID = 1L;

Expand Down
31 changes: 23 additions & 8 deletions java/org/apache/catalina/connector/InputBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@
import java.util.concurrent.ConcurrentHashMap;

import jakarta.servlet.ReadListener;
import jakarta.servlet.RequestDispatcher;

import org.apache.catalina.security.SecurityUtil;
import org.apache.coyote.ActionCode;
import org.apache.coyote.Request;
import org.apache.coyote.BadRequestException;
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
import org.apache.tomcat.util.buf.B2CConverter;
Expand Down Expand Up @@ -108,7 +109,7 @@ public class InputBuffer extends Reader
/**
* Associated Coyote request.
*/
private Request coyoteRequest;
private org.apache.coyote.Request coyoteRequest;


/**
Expand Down Expand Up @@ -167,7 +168,7 @@ public InputBuffer(int size) {
*
* @param coyoteRequest Associated Coyote request
*/
public void setRequest(Request coyoteRequest) {
public void setRequest(org.apache.coyote.Request coyoteRequest) {
this.coyoteRequest = coyoteRequest;
}

Expand Down Expand Up @@ -215,8 +216,7 @@ public void close() throws IOException {
public int available() {
int available = availableInThisBuffer();
if (available == 0) {
coyoteRequest.action(ActionCode.AVAILABLE,
Boolean.valueOf(coyoteRequest.getReadListener() != null));
coyoteRequest.action(ActionCode.AVAILABLE, Boolean.valueOf(coyoteRequest.getReadListener() != null));
available = (coyoteRequest.getAvailable() > 0) ? 1 : 0;
}
return available;
Expand Down Expand Up @@ -314,15 +314,30 @@ public int realReadBytes() throws IOException {

try {
return coyoteRequest.doRead(this);
} catch (BadRequestException bre) {
// Make the exception visible to the application
handleReadException(bre);
throw bre;
} catch (IOException ioe) {
coyoteRequest.setErrorException(ioe);
// An IOException on a read is almost always due to
// the remote client aborting the request.
handleReadException(ioe);
// Any other IOException on a read is almost always due to the remote client aborting the request.
// Make the exception visible to the application
throw new ClientAbortException(ioe);
}
}


private void handleReadException(Exception e) throws IOException {
// Set flag used by asynchronous processing to detect errors on non-container threads
coyoteRequest.setErrorException(e);
// In synchronous processing, this exception may be swallowed by the application so set error flags here.
Request request = (Request) coyoteRequest.getNote(CoyoteAdapter.ADAPTER_NOTES);
Response response = request.getResponse();
request.setAttribute(RequestDispatcher.ERROR_EXCEPTION, e);
response.sendError(400);
}


public int readByte() throws IOException {
throwIfClosed();

Expand Down
10 changes: 5 additions & 5 deletions java/org/apache/catalina/core/ApplicationDispatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@
import org.apache.catalina.Context;
import org.apache.catalina.Globals;
import org.apache.catalina.Wrapper;
import org.apache.catalina.connector.ClientAbortException;
import org.apache.catalina.connector.Request;
import org.apache.catalina.connector.RequestFacade;
import org.apache.catalina.connector.Response;
import org.apache.catalina.connector.ResponseFacade;
import org.apache.coyote.BadRequestException;
import org.apache.tomcat.util.ExceptionUtils;
import org.apache.tomcat.util.res.StringManager;

Expand Down Expand Up @@ -691,7 +691,7 @@ private void invoke(ServletRequest request, ServletResponse response,
filterChain.doFilter(request, response);
}
// Servlet Service Method is called by the FilterChain
} catch (ClientAbortException e) {
} catch (BadRequestException e) {
ioException = e;
} catch (IOException e) {
wrapper.getLogger().error(sm.getString("applicationDispatcher.serviceException",
Expand All @@ -704,9 +704,9 @@ private void invoke(ServletRequest request, ServletResponse response,
wrapper.unavailable(e);
} catch (ServletException e) {
Throwable rootCause = StandardWrapper.getRootCause(e);
if (!(rootCause instanceof ClientAbortException)) {
wrapper.getLogger().error(sm.getString("applicationDispatcher.serviceException",
wrapper.getName()), rootCause);
if (!(rootCause instanceof BadRequestException)) {
wrapper.getLogger().error(sm.getString("applicationDispatcher.serviceException", wrapper.getName()),
rootCause);
}
servletException = e;
} catch (RuntimeException e) {
Expand Down
13 changes: 8 additions & 5 deletions java/org/apache/catalina/core/StandardHostValve.java
Original file line number Diff line number Diff line change
Expand Up @@ -317,11 +317,14 @@ protected void throwable(Request request, Response response,
}
}
} else {
// A custom error-page has not been defined for the exception
// that was thrown during request processing. Check if an
// error-page for error code 500 was specified and if so,
// send that page back as the response.
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
/*
* A custom error-page has not been defined for the exception that was thrown during request processing.
* Set the status to 500 if an error status has not already been set and check for custom error-page for
* the status.
*/
if (response.getStatus() < HttpServletResponse.SC_BAD_REQUEST) {
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
// The response is an error
response.setError();

Expand Down
Loading
Loading