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

FailFast if Content-Length header value is greater than configured max request size #59

Merged
merged 1 commit into from
Jun 3, 2017
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
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ public void initChannel(SocketChannel ch) {

// INBOUND - Add RoutingHandler to figure out which endpoint should handle the request and set it on our request
// state for later execution
p.addLast(ROUTING_HANDLER_NAME, new RoutingHandler(endpoints));
p.addLast(ROUTING_HANDLER_NAME, new RoutingHandler(endpoints, maxRequestSizeInBytes));

// INBOUND - Add SecurityValidationHandler to validate the RequestInfo object for the matching endpoint
p.addLast(SECURITY_VALIDATION_HANDLER_NAME, new SecurityValidationHandler(requestSecurityValidator));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.nike.riposte.server.error.exception.InvalidHttpRequestException;
import com.nike.riposte.server.handler.base.BaseInboundHandlerWithTracingAndMdcSupport;
import com.nike.riposte.server.handler.base.PipelineContinuationBehavior;
import com.nike.riposte.server.http.Endpoint;
import com.nike.riposte.server.http.HttpProcessingState;
import com.nike.riposte.server.http.RequestInfo;
import com.nike.riposte.server.http.impl.RequestInfoImpl;
Expand All @@ -19,6 +18,9 @@
import io.netty.handler.codec.http.HttpRequest;
import io.netty.util.ReferenceCountUtil;

import static com.nike.riposte.util.HttpUtils.getConfiguredMaxRequestSize;
import static com.nike.riposte.util.HttpUtils.isMaxRequestSizeValidationDisabled;

/**
* Monitors the incoming messages - when it sees a {@link HttpRequest} then it creates a new {@link RequestInfo} from it
* and sets it on the channel's current request state via {@link HttpProcessingState#setRequestInfo(RequestInfo)}. If
Expand Down Expand Up @@ -76,7 +78,7 @@ else if (msg instanceof HttpContent) {
}

int currentRequestLengthInBytes = requestInfo.addContentChunk(httpContentMsg);
int configuredMaxRequestSize = getConfiguredMaxRequestSize(state);
int configuredMaxRequestSize = getConfiguredMaxRequestSize(state.getEndpointForExecution(), globalConfiguredMaxRequestSizeInBytes);

if (!isMaxRequestSizeValidationDisabled(configuredMaxRequestSize)
&& currentRequestLengthInBytes > configuredMaxRequestSize) {
Expand Down Expand Up @@ -104,21 +106,6 @@ private void throwExceptionIfNotSuccessfullyDecoded(HttpObject httpObject) {
}
}

private boolean isMaxRequestSizeValidationDisabled(int configuredMaxRequestSize) {
return configuredMaxRequestSize <= 0;
}

private int getConfiguredMaxRequestSize(HttpProcessingState state) {
Endpoint<?> endpoint = state.getEndpointForExecution();

//if the endpoint is null or the endpoint is not overriding, we should return the globally configured value
if (endpoint == null || endpoint.maxRequestSizeInBytesOverride() == null) {
return globalConfiguredMaxRequestSizeInBytes;
}

return endpoint.maxRequestSizeInBytesOverride();
}

@Override
protected boolean argsAreEligibleForLinkingAndUnlinkingDistributedTracingInfo(
HandlerMethodToExecute methodToExecute, ChannelHandlerContext ctx, Object msgOrEvt, Throwable cause
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@
import java.util.Optional;

import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.TooLongFrameException;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpRequest;

import static com.nike.riposte.util.HttpUtils.getConfiguredMaxRequestSize;
import static com.nike.riposte.util.HttpUtils.isMaxRequestSizeValidationDisabled;

/**
* Handles the logic of determining which {@link Endpoint} matches the {@link RequestInfo} in the channel's current
* state ({@link com.nike.riposte.server.http.HttpProcessingState#getRequestInfo()}). It also performs some error
Expand All @@ -32,12 +37,14 @@
public class RoutingHandler extends BaseInboundHandlerWithTracingAndMdcSupport {

private final Collection<Endpoint<?>> endpoints;
private final int globalConfiguredMaxRequestSizeInBytes;

public RoutingHandler(Collection<Endpoint<?>> endpoints) {
public RoutingHandler(Collection<Endpoint<?>> endpoints, int globalMaxRequestSizeInBytes) {
if (endpoints == null || endpoints.isEmpty())
throw new IllegalArgumentException("endpoints cannot be empty");

this.endpoints = endpoints;
this.globalConfiguredMaxRequestSizeInBytes = globalMaxRequestSizeInBytes;
}

/**
Expand Down Expand Up @@ -104,6 +111,8 @@ public PipelineContinuationBehavior doChannelRead(ChannelHandlerContext ctx, Obj
RequestInfo request = state.getRequestInfo();
Pair<Endpoint<?>, String> endpointForExecution = findSingleEndpointForExecution(request);

throwExceptionIfContentLengthHeaderIsLargerThanConfiguredMaxRequestSize((HttpRequest) msg, endpointForExecution.getLeft());

request.setPathParamsBasedOnPathTemplate(endpointForExecution.getRight());

state.setEndpointForExecution(endpointForExecution.getLeft(), endpointForExecution.getRight());
Expand All @@ -112,6 +121,16 @@ public PipelineContinuationBehavior doChannelRead(ChannelHandlerContext ctx, Obj
return PipelineContinuationBehavior.CONTINUE;
}

private void throwExceptionIfContentLengthHeaderIsLargerThanConfiguredMaxRequestSize(HttpRequest msg, Endpoint<?> endpoint) {
int configuredMaxRequestSize = getConfiguredMaxRequestSize(endpoint, globalConfiguredMaxRequestSizeInBytes);

if (!isMaxRequestSizeValidationDisabled(configuredMaxRequestSize)
&& HttpHeaders.isContentLengthSet(msg)
&& HttpHeaders.getContentLength(msg) > configuredMaxRequestSize) {
throw new TooLongFrameException("Content-Length header value exceeded configured max request size of " + configuredMaxRequestSize);
}
}

@Override
protected boolean argsAreEligibleForLinkingAndUnlinkingDistributedTracingInfo(
HandlerMethodToExecute methodToExecute, ChannelHandlerContext ctx, Object msgOrEvt, Throwable cause
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.nike.internal.util.Pair;
import com.nike.riposte.server.http.*;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.handler.codec.http.DefaultFullHttpRequest;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpVersion;
import io.restassured.response.ExtractableResponse;
import com.nike.riposte.server.Server;
import com.nike.riposte.server.config.ServerConfig;
Expand All @@ -23,15 +30,19 @@
import java.util.concurrent.Executor;

import static com.nike.riposte.server.componenttest.VerifyRequestSizeValidationComponentTest.RequestSizeValidationConfig.GLOBAL_MAX_REQUEST_SIZE;
import static com.nike.riposte.server.testutils.ComponentTestUtils.executeRequest;
import static io.netty.handler.codec.http.HttpHeaders.Values.CHUNKED;
import static io.netty.util.CharsetUtil.UTF_8;
import static io.restassured.RestAssured.given;
import static org.assertj.core.api.Assertions.assertThat;

public class VerifyRequestSizeValidationComponentTest {


private static final String BASE_URI = "http://127.0.0.1";
private static Server server;
private static ServerConfig serverConfig;
private static ObjectMapper objectMapper;
private int incompleteCallTimeoutMillis = 2000;

@BeforeClass
public static void setUpClass() throws Exception {
Expand All @@ -47,10 +58,10 @@ public static void tearDown() throws Exception {
}

@Test
public void should_return_bad_request_when_request_exceeds_global_configured_max_request_size() throws IOException {
public void should_return_bad_request_when_ContentLength_header_exceeds_global_configured_max_request_size() throws IOException {
ExtractableResponse response =
given()
.baseUri("http://127.0.0.1")
.baseUri(BASE_URI)
.port(serverConfig.endpointsPort())
.basePath(BasicEndpoint.MATCHING_PATH)
.log().all()
Expand All @@ -62,21 +73,14 @@ public void should_return_bad_request_when_request_exceeds_global_configured_max
.extract();

assertThat(response.statusCode()).isEqualTo(HttpResponseStatus.BAD_REQUEST.code());
assertBadRequestErrorMessageAndMetadata(response);
}

private void assertBadRequestErrorMessageAndMetadata(ExtractableResponse response) throws IOException {
JsonNode error = objectMapper.readValue(response.asString(), JsonNode.class).get("errors").get(0);
assertThat(error.get("message").textValue()).isEqualTo("Malformed request");
assertThat(error.get("metadata").get("cause").textValue())
.isEqualTo("The request exceeded the maximum payload size allowed");
assertBadRequestErrorMessageAndMetadata(response.asString());
}

@Test
public void should_return_expected_response_when_not_exceeding_global_request_size() {
public void should_return_expected_response_when_ContentLength_header_not_exceeding_global_request_size() {
ExtractableResponse response =
given()
.baseUri("http://127.0.0.1")
.baseUri(BASE_URI)
.port(serverConfig.endpointsPort())
.basePath(BasicEndpoint.MATCHING_PATH)
.log().all()
Expand All @@ -92,10 +96,10 @@ public void should_return_expected_response_when_not_exceeding_global_request_si
}

@Test
public void should_return_bad_request_when_request_exceeds_endpoint_overridden_configured_max_request_size() throws IOException {
public void should_return_bad_request_when_ContentLength_header_exceeds_endpoint_overridden_configured_max_request_size() throws IOException {
ExtractableResponse response =
given()
.baseUri("http://127.0.0.1")
.baseUri(BASE_URI)
.port(serverConfig.endpointsPort())
.basePath(BasicEndpointWithRequestSizeValidationOverride.MATCHING_PATH)
.log().all()
Expand All @@ -107,14 +111,14 @@ public void should_return_bad_request_when_request_exceeds_endpoint_overridden_c
.extract();

assertThat(response.statusCode()).isEqualTo(HttpResponseStatus.BAD_REQUEST.code());
assertBadRequestErrorMessageAndMetadata(response);
assertBadRequestErrorMessageAndMetadata(response.asString());
}

@Test
public void should_return_expected_response_when_not_exceeding_endpoint_overridden_request_size() {
public void should_return_expected_response_when_ContentLength_header_not_exceeding_endpoint_overridden_request_size() {
ExtractableResponse response =
given()
.baseUri("http://127.0.0.1")
.baseUri(BASE_URI)
.port(serverConfig.endpointsPort())
.basePath(BasicEndpointWithRequestSizeValidationOverride.MATCHING_PATH)
.log().all()
Expand All @@ -130,10 +134,10 @@ public void should_return_expected_response_when_not_exceeding_endpoint_overridd
}

@Test
public void should_return_expected_response_when_endpoint_disabled_request_size_validation() {
public void should_return_expected_response_when_endpoint_disabled_ContentLength_header_above_global_size_validation() {
ExtractableResponse response =
given()
.baseUri("http://127.0.0.1")
.baseUri(BASE_URI)
.port(serverConfig.endpointsPort())
.basePath(BasicEndpointWithRequestSizeValidationDisabled.MATCHING_PATH)
.log().all()
Expand All @@ -148,6 +152,103 @@ public void should_return_expected_response_when_endpoint_disabled_request_size_
assertThat(response.asString()).isEqualTo(BasicEndpointWithRequestSizeValidationDisabled.RESPONSE_PAYLOAD);
}

@Test
public void should_return_bad_request_when_chunked_request_exceeds_global_configured_max_request_size() throws Exception {
HttpRequest request = new DefaultFullHttpRequest(
HttpVersion.HTTP_1_1, HttpMethod.POST, BasicEndpoint.MATCHING_PATH, Unpooled.wrappedBuffer(generatePayloadOfSizeInBytes(GLOBAL_MAX_REQUEST_SIZE + 1).getBytes(UTF_8))
);

request.headers().set(HttpHeaders.Names.HOST, "127.0.0.1");
request.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
request.headers().set(HttpHeaders.Names.TRANSFER_ENCODING, CHUNKED);

// when
Pair<Integer, String> serverResponse = executeRequest(request, serverConfig.endpointsPort(), incompleteCallTimeoutMillis);

// then
assertThat(serverResponse.getLeft()).isEqualTo(HttpResponseStatus.BAD_REQUEST.code());
assertBadRequestErrorMessageAndMetadata(serverResponse.getRight());
}

@Test
public void should_return_expected_response_when_chunked_request_not_exceeding_global_request_size() throws Exception {
HttpRequest request = new DefaultFullHttpRequest(
HttpVersion.HTTP_1_1, HttpMethod.POST, BasicEndpoint.MATCHING_PATH, Unpooled.wrappedBuffer(generatePayloadOfSizeInBytes(GLOBAL_MAX_REQUEST_SIZE).getBytes(UTF_8))
);

request.headers().set(HttpHeaders.Names.HOST, "127.0.0.1");
request.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
request.headers().set(HttpHeaders.Names.TRANSFER_ENCODING, CHUNKED);

// when
Pair<Integer, String> serverResponse = executeRequest(request, serverConfig.endpointsPort(), incompleteCallTimeoutMillis);

// then
assertThat(serverResponse.getLeft()).isEqualTo(HttpResponseStatus.OK.code());
assertThat(serverResponse.getRight()).isEqualTo(BasicEndpoint.RESPONSE_PAYLOAD);
}

@Test
public void should_return_bad_request_when_chunked_request_exceeds_endpoint_overridden_configured_max_request_size() throws Exception {
HttpRequest request = new DefaultFullHttpRequest(
HttpVersion.HTTP_1_1, HttpMethod.POST, BasicEndpointWithRequestSizeValidationOverride.MATCHING_PATH, Unpooled.wrappedBuffer(generatePayloadOfSizeInBytes(BasicEndpointWithRequestSizeValidationOverride.MAX_REQUEST_SIZE + 1).getBytes(UTF_8))
);

request.headers().set(HttpHeaders.Names.HOST, "127.0.0.1");
request.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
request.headers().set(HttpHeaders.Names.TRANSFER_ENCODING, CHUNKED);

// when
Pair<Integer, String> serverResponse = executeRequest(request, serverConfig.endpointsPort(), incompleteCallTimeoutMillis);

// then
assertThat(serverResponse.getLeft()).isEqualTo(HttpResponseStatus.BAD_REQUEST.code());
assertBadRequestErrorMessageAndMetadata(serverResponse.getRight());
}

@Test
public void should_return_expected_response_when_chunked_request_not_exceeding_endpoint_overridden_request_size() throws Exception {
HttpRequest request = new DefaultFullHttpRequest(
HttpVersion.HTTP_1_1, HttpMethod.POST, BasicEndpointWithRequestSizeValidationOverride.MATCHING_PATH, Unpooled.wrappedBuffer(generatePayloadOfSizeInBytes(BasicEndpointWithRequestSizeValidationOverride.MAX_REQUEST_SIZE).getBytes(UTF_8))
);

request.headers().set(HttpHeaders.Names.HOST, "127.0.0.1");
request.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
request.headers().set(HttpHeaders.Names.TRANSFER_ENCODING, CHUNKED);

// when
Pair<Integer, String> serverResponse = executeRequest(request, serverConfig.endpointsPort(), incompleteCallTimeoutMillis);

// then
assertThat(serverResponse.getLeft()).isEqualTo(HttpResponseStatus.OK.code());
assertThat(serverResponse.getRight()).isEqualTo(BasicEndpointWithRequestSizeValidationOverride.RESPONSE_PAYLOAD);
}

@Test
public void should_return_expected_response_when_endpoint_disabled_chunked_request_size_validation() throws Exception {
HttpRequest request = new DefaultFullHttpRequest(
HttpVersion.HTTP_1_1, HttpMethod.POST, BasicEndpointWithRequestSizeValidationDisabled.MATCHING_PATH, Unpooled.wrappedBuffer(generatePayloadOfSizeInBytes(GLOBAL_MAX_REQUEST_SIZE + 100).getBytes(UTF_8))
);

request.headers().set(HttpHeaders.Names.HOST, "127.0.0.1");
request.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
request.headers().set(HttpHeaders.Names.TRANSFER_ENCODING, CHUNKED);

// when
Pair<Integer, String> serverResponse = executeRequest(request, serverConfig.endpointsPort(), incompleteCallTimeoutMillis);

// then
assertThat(serverResponse.getLeft()).isEqualTo(HttpResponseStatus.OK.code());
assertThat(serverResponse.getRight()).isEqualTo(BasicEndpointWithRequestSizeValidationDisabled.RESPONSE_PAYLOAD);
}

private void assertBadRequestErrorMessageAndMetadata(String response) throws IOException {
JsonNode error = objectMapper.readValue(response, JsonNode.class).get("errors").get(0);
assertThat(error.get("message").textValue()).isEqualTo("Malformed request");
assertThat(error.get("metadata").get("cause").textValue())
.isEqualTo("The request exceeded the maximum payload size allowed");
}

private static String generatePayloadOfSizeInBytes(int length) {
StringBuilder sb = new StringBuilder(length);
for(int i = 0; i < length; i++) {
Expand Down
Loading