polish and formatting

This commit is contained in:
spencergibb
2021-11-02 13:14:12 -04:00
parent 607392a658
commit 9bb21eafc9
3 changed files with 34 additions and 24 deletions

View File

@@ -23,7 +23,8 @@ import org.springframework.web.server.ServerWebExchange;
/**
* See https://datatracker.ietf.org/doc/html/rfc7230#section-3.3.3 for details.
*/
public class TransferEncodingNormalizationHeadersFilter implements HttpHeadersFilter, Ordered {
public class TransferEncodingNormalizationHeadersFilter
implements HttpHeadersFilter, Ordered {
@Override
public int getOrder() {
@@ -33,7 +34,8 @@ public class TransferEncodingNormalizationHeadersFilter implements HttpHeadersFi
@Override
public HttpHeaders filter(HttpHeaders input, ServerWebExchange exchange) {
String transferEncoding = input.getFirst(HttpHeaders.TRANSFER_ENCODING);
if (transferEncoding != null && "chunked".equalsIgnoreCase(transferEncoding.trim())
if (transferEncoding != null
&& "chunked".equalsIgnoreCase(transferEncoding.trim())
&& input.containsKey(HttpHeaders.CONTENT_LENGTH)) {
HttpHeaders filtered = new HttpHeaders();

View File

@@ -53,9 +53,10 @@ import static org.springframework.boot.test.context.SpringBootTest.WebEnvironmen
@SpringBootTest(properties = {}, webEnvironment = RANDOM_PORT)
@ActiveProfiles("transferencoding")
public class TransferEncodingNormalizationHeardsFilterIntegrationTests {
public class TransferEncodingNormalizationHeadersFilterIntegrationTests {
private static final Log log = LogFactory.getLog(TransferEncodingNormalizationHeardsFilterIntegrationTests.class);
private static final Log log = LogFactory
.getLog(TransferEncodingNormalizationHeadersFilterIntegrationTests.class);
@LocalServerPort
private int port;
@@ -65,17 +66,18 @@ public class TransferEncodingNormalizationHeardsFilterIntegrationTests {
final ClassLoader classLoader = this.getClass().getClassLoader();
// Issue a crafted request with smuggling attempt
assert200With("Should Fail",
StreamUtils.copyToByteArray(classLoader.getResourceAsStream("transfer-encoding/invalid-request.bin")));
assert200With("Should Fail", StreamUtils.copyToByteArray(classLoader
.getResourceAsStream("transfer-encoding/invalid-request.bin")));
// Issue a legit request, which should not fail
assert200With("Should Not Fail",
StreamUtils.copyToByteArray(classLoader.getResourceAsStream("transfer-encoding/valid-request.bin")));
assert200With("Should Not Fail", StreamUtils.copyToByteArray(
classLoader.getResourceAsStream("transfer-encoding/valid-request.bin")));
}
private void assert200With(String name, byte[] payload) throws Exception {
final String response = execute("localhost", port, payload);
log.info(LogMessage.format("Request to localhost:%d %s\n%s", port, name, new String(payload)));
log.info(LogMessage.format("Request to localhost:%d %s\n%s", port, name,
new String(payload)));
assertThat(response).isNotNull();
log.info(LogMessage.format("Response %s\n%s", name, response));
assertThat(response).matches("HTTP/1.\\d 200 OK");
@@ -85,7 +87,8 @@ public class TransferEncodingNormalizationHeardsFilterIntegrationTests {
final Socket socket = new Socket(target, port);
final OutputStream out = socket.getOutputStream();
final BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
final BufferedReader in = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
out.write(payload);
@@ -111,9 +114,8 @@ public class TransferEncodingNormalizationHeardsFilterIntegrationTests {
@Bean
public RouteLocator routeLocator(RouteLocatorBuilder builder) {
return builder.routes()
.route("echo", r -> r.path("/route/echo").filters(f -> f.stripPrefix(1)).uri("lb://xferenc"))
.build();
return builder.routes().route("echo", r -> r.path("/route/echo")
.filters(f -> f.stripPrefix(1)).uri("lb://xferenc")).build();
}
}
@@ -144,7 +146,8 @@ public class TransferEncodingNormalizationHeardsFilterIntegrationTests {
@Bean
public ServiceInstanceListSupplier staticServiceInstanceListSupplier() {
return ServiceInstanceListSuppliers.from("xferenc",
new DefaultServiceInstance("xferenc" + "-1", "xferenc", "localhost", port, false));
new DefaultServiceInstance("xferenc" + "-1", "xferenc", "localhost",
port, false));
}
}

View File

@@ -27,33 +27,38 @@ import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Spencer Gibb
*/
public class TransferEncodingMarmalizationHeadersFilterTests {
public class TransferEncodingNormalizationHeadersFilterTests {
@Test
public void noTransferEncodingWithContentLength() {
MockServerHttpRequest.BaseBuilder<?> builder = MockServerHttpRequest.post("http://localhost/post")
.header(HttpHeaders.CONTENT_LENGTH, "6");
MockServerHttpRequest.BaseBuilder<?> builder = MockServerHttpRequest
.post("http://localhost/post").header(HttpHeaders.CONTENT_LENGTH, "6");
HttpHeaders headers = testFilter(MockServerWebExchange.from(builder));
assertThat(headers).containsKey(HttpHeaders.CONTENT_LENGTH).doesNotContainKey(HttpHeaders.TRANSFER_ENCODING);
assertThat(headers).containsKey(HttpHeaders.CONTENT_LENGTH)
.doesNotContainKey(HttpHeaders.TRANSFER_ENCODING);
}
@Test
public void transferEncodingWithContentLength() {
MockServerHttpRequest.BaseBuilder<?> builder = MockServerHttpRequest.post("http://localhost/post")
.header(HttpHeaders.CONTENT_LENGTH, "6").header(HttpHeaders.TRANSFER_ENCODING, "chunked");
MockServerHttpRequest.BaseBuilder<?> builder = MockServerHttpRequest
.post("http://localhost/post").header(HttpHeaders.CONTENT_LENGTH, "6")
.header(HttpHeaders.TRANSFER_ENCODING, "chunked");
HttpHeaders headers = testFilter(MockServerWebExchange.from(builder));
assertThat(headers).doesNotContainKey(HttpHeaders.CONTENT_LENGTH).containsKey(HttpHeaders.TRANSFER_ENCODING);
assertThat(headers).doesNotContainKey(HttpHeaders.CONTENT_LENGTH)
.containsKey(HttpHeaders.TRANSFER_ENCODING);
}
@Test
public void transferEncodingCaseInsensitiveWithContentLength() {
MockServerHttpRequest.BaseBuilder<?> builder = MockServerHttpRequest.post("http://localhost/post")
.header(HttpHeaders.CONTENT_LENGTH, "6").header(HttpHeaders.TRANSFER_ENCODING, "Chunked ");
MockServerHttpRequest.BaseBuilder<?> builder = MockServerHttpRequest
.post("http://localhost/post").header(HttpHeaders.CONTENT_LENGTH, "6")
.header(HttpHeaders.TRANSFER_ENCODING, "Chunked ");
HttpHeaders headers = testFilter(MockServerWebExchange.from(builder));
assertThat(headers).doesNotContainKey(HttpHeaders.CONTENT_LENGTH).containsKey(HttpHeaders.TRANSFER_ENCODING);
assertThat(headers).doesNotContainKey(HttpHeaders.CONTENT_LENGTH)
.containsKey(HttpHeaders.TRANSFER_ENCODING);
}
private HttpHeaders testFilter(MockServerWebExchange exchange) {