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

Allow no SP after status-code for HTTP/1.x responses #2247

Merged
merged 1 commit into from
Jun 9, 2022
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 @@ -263,16 +263,26 @@ protected final void decode(final ChannelHandlerContext ctx, final ByteBuf buffe
}

final int bStart = aEnd + 1; // Expect a single WS
final int bEnd;
int bEnd;
try {
bEnd = buffer.forEachByte(bStart, nonControlIndex - bStart + 1,
isDecodingRequest() ? FIND_VCHAR_END : FIND_WS);
} catch (IllegalCharacterException cause) {
throw new StacklessDecoderException(
"Invalid start-line: HTTP request-target contains an illegal character", cause);
}
if (bEnd < 0 || bEnd == bStart) {
throw newStartLineError("second");
if (bEnd < 0) {
if (isDecodingRequest()) {
throw newStartLineError("second");
} else { // Response can be without a reason-phrase: "HTTP/1.1 200\r\n"
bEnd = nonControlIndex + 1;
}
}
if (bEnd == bStart) { // Happens when there are two SP next to each other
throw new DecoderException("Invalid start-line: incorrect number of components, cannot find the " +
(isDecodingRequest() ? "request-target" : "status-code") + ", expected: " +
(isDecodingRequest() ? "method SP request-target SP HTTP-version" :
"HTTP-version SP status-code SP reason-phrase"));
}

final int cStart = bEnd + 1; // Expect a single WS
Expand Down Expand Up @@ -908,7 +918,7 @@ private static int findLF(final ByteBuf buffer, final int fromIndex, final int t
}

private DecoderException newStartLineError(final String place) {
throw new DecoderException("Invalid start-line: incorrect number of components, cannot find the " + place +
return new DecoderException("Invalid start-line: incorrect number of components, cannot find the " + place +
" SP, expected: " + (isDecodingRequest() ? "method SP request-target SP HTTP-version" :
"HTTP-version SP status-code SP reason-phrase"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ void tearDown() throws Exception {

abstract EmbeddedChannel channelSpecException();

abstract boolean isDecodingRequest();

abstract String startLine();

abstract HttpMetaData assertStartLine(EmbeddedChannel channel);
Expand Down Expand Up @@ -255,11 +257,11 @@ private static ByteBuf content(int contentLength) {
return wrappedBuffer(content);
}

private EmbeddedChannel channel(boolean crlf) {
EmbeddedChannel channel(boolean crlf) {
return crlf ? channel() : channelSpecException();
}

private static String br(boolean crlf) {
static String br(boolean crlf) {
return crlf ? "\r\n" : "\n";
}

Expand Down Expand Up @@ -822,7 +824,7 @@ private void unexpectedTrailersAfterContentLength(boolean crlf) {
// https://tools.ietf.org/html/rfc7230#section-3.3
DecoderException e = assertThrows(DecoderException.class,
() -> writeMsg("TrailerStatus: good" + br + br, channel));
assertThat(e.getMessage(), startsWith("Invalid start-line"));
assertThat(e.getMessage(), startsWith(isDecodingRequest() ? "Invalid start-line" : "Invalid HTTP version"));
assertThat(channel.inboundMessages(), is(not(empty())));
}

Expand Down Expand Up @@ -851,7 +853,7 @@ private void smuggleZeroContentLength(boolean smuggleBeforeContentLength, boolea
"Smuggled: " + startLine() + br + br + "Content-Length: 0" + br :
"Content-Length: 0" + br + "Smuggled: " + startLine() + br + br) +
"Connection: keep-alive" + br + br, channel));
assertThat(e.getMessage(), startsWith("Invalid start-line"));
assertThat(e.getMessage(), startsWith(isDecodingRequest() ? "Invalid start-line" : "Invalid HTTP version"));

HttpMetaData metaData = assertStartLine(channel);
assertSingleHeaderValue(metaData.headers(), HOST, "servicetalk.io");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ EmbeddedChannel channelSpecException() {
return channelSpecException;
}

@Override
boolean isDecodingRequest() {
return true;
}

@Override
String startLine() {
return "GET / HTTP/1.1";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@

import io.netty.channel.embedded.EmbeddedChannel;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

import java.util.ArrayDeque;
import java.util.ArrayList;
Expand Down Expand Up @@ -104,6 +106,11 @@ EmbeddedChannel channelSpecException() {
return channelSpecException;
}

@Override
boolean isDecodingRequest() {
return false;
}

@Override
String startLine() {
return "HTTP/1.1 204 No Content";
Expand Down Expand Up @@ -131,19 +138,14 @@ void illegalPrefaceCharacter() {

@Test
void noVersion() {
assertDecoderException("200 OK" + "\r\n", "Invalid start-line");
assertDecoderException("200 OK" + "\r\n", "Invalid HTTP version");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🥇

}

@Test
void noStatusCode() {
assertDecoderException("HTTP/1.1 OK" + "\r\n", "Invalid start-line");
}

@Test
void noSpAfterStatusCode() {
assertDecoderException("HTTP/1.1 200" + "\r\n", "Invalid start-line");
}

@Test
void invalidStartLineOrder() {
assertDecoderException("HTTP/1.1 OK 200" + "\r\n", "Invalid start-line");
Expand Down Expand Up @@ -224,6 +226,18 @@ void validStartLineWithCustomHttpVersion() {
assertFalse(channel.finishAndReleaseAll());
}

@ParameterizedTest(name = "statusCode={0}, crlf={1}")
@CsvSource({"200,true", "200,false", "299,true", "299,false"})
void noSpAfterStatusCodeNoReasonPhrase(int statusCode, boolean crlf) {
HttpResponseStatus status = HttpResponseStatus.of(statusCode, "");
EmbeddedChannel channel = channel(crlf);
String br = br(crlf);
writeMsg("HTTP/1.1 " + status.code() /* no SP */ + br + br, channel);
assertResponseLine(HTTP_1_1, status, channel);
assertEmptyTrailers(channel);
assertFalse(channel.finishAndReleaseAll());
}

@Test
void emptyReasonPhrase() {
testReasonPhrase("");
Expand Down