Skip to content

4.x: improvement of header parsing error handling #8831

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

Merged
merged 1 commit into from
Jun 3, 2024
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 @@ -460,6 +460,25 @@ public void request(String method, String path, String protocol, String host, It
}
}

/**
* Send raw data to the server.
*
* @param content content to send over the socket
*/
public void requestRaw(String content) {
if (socket == null) {
connect();
}

try {
PrintWriter pw = new PrintWriter(new OutputStreamWriter(socket.getOutputStream(), StandardCharsets.UTF_8));
pw.print(content);
pw.flush();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

/**
* Write raw proxy protocol header before a request.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, 2023 Oracle and/or its affiliates.
* Copyright (c) 2022, 2024 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -148,6 +148,39 @@ void testBadHeaderWhitespace() {
assertThat(response, containsString(CUSTOM_ENTITY));
}

@Test
void testCrWithoutLf() {
socketClient.requestRaw("GET / HTTP/1.1\r\nhost: localhost:8080\rcustom: value\r\n");

String response = socketClient.receive();
assertThat(response, containsString("400 " + CUSTOM_REASON_PHRASE));
assertThat(response, containsString(CUSTOM_ENTITY));
}

@Test
void testLfWithoutCr() {
socketClient.requestRaw("GET / HTTP/1.1\r\nhost: localhost:8080\ncustom: value\r\n");

String response = socketClient.receive();
assertThat(response, containsString("400 " + CUSTOM_REASON_PHRASE));
assertThat(response, containsString(CUSTOM_ENTITY));
}

@Test
void testKeepAliveAndMissingLf() {
socketClient.request(Method.GET, "/", null, List.of("Accept: text/plain", "Connection: keep-alive"));
String response = socketClient.receive();
assertThat(response, containsString("200 OK"));
assertThat(response, containsString("Hi"));

socketClient.requestRaw("GET / HTTP/1.1\r\nhost: localhost:8080\rcustom: value\r\n");

response = socketClient.receive();
assertThat(response, containsString("400 " + CUSTOM_REASON_PHRASE));
assertThat(response, containsString("Connection: close"));
assertThat(response, containsString(CUSTOM_ENTITY));
}

private static DirectHandler.TransportResponse badRequestHandler(DirectHandler.TransportRequest request,
DirectHandler.EventType eventType,
Status httpStatus,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, 2023 Oracle and/or its affiliates.
* Copyright (c) 2022, 2024 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -55,7 +55,7 @@ public Http1Headers(DataReader reader, int maxHeadersSize, boolean validateHeade
public WritableHeaders<?> readHeaders(HttpPrologue prologue) {
try {
return Http1HeadersParser.readHeaders(reader, maxHeadersSize, validateHeaders);
} catch (IllegalStateException | IllegalArgumentException e) {
} catch (IllegalStateException | IllegalArgumentException | DataReader.IncorrectNewLineException e) {
throw RequestException.builder()
.type(DirectHandler.EventType.BAD_REQUEST)
.request(DirectTransportRequest.create(prologue, WritableHeaders.create()))
Expand Down