-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
http: adding response code details for downstream HTTP/1.1 codec errors #9286
Merged
+87
−20
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,8 +57,9 @@ class Http1ServerConnectionImplTest : public testing::Test { | |
|
||
void expectHeadersTest(Protocol p, bool allow_absolute_url, Buffer::OwnedImpl& buffer, | ||
TestHeaderMapImpl& expected_headers); | ||
void expect400(Protocol p, bool allow_absolute_url, Buffer::OwnedImpl& buffer); | ||
void testRequestHeadersExceedLimit(std::string header_string); | ||
void expect400(Protocol p, bool allow_absolute_url, Buffer::OwnedImpl& buffer, | ||
absl::string_view details = ""); | ||
void testRequestHeadersExceedLimit(std::string header_string, absl::string_view details = ""); | ||
void testRequestHeadersAccepted(std::string header_string); | ||
|
||
protected: | ||
|
@@ -68,7 +69,8 @@ class Http1ServerConnectionImplTest : public testing::Test { | |
}; | ||
|
||
void Http1ServerConnectionImplTest::expect400(Protocol p, bool allow_absolute_url, | ||
Buffer::OwnedImpl& buffer) { | ||
Buffer::OwnedImpl& buffer, | ||
absl::string_view details) { | ||
InSequence sequence; | ||
|
||
std::string output; | ||
|
@@ -82,11 +84,19 @@ void Http1ServerConnectionImplTest::expect400(Protocol p, bool allow_absolute_ur | |
} | ||
|
||
Http::MockStreamDecoder decoder; | ||
EXPECT_CALL(callbacks_, newStream(_, _)).WillOnce(ReturnRef(decoder)); | ||
Http::StreamEncoder* response_encoder = nullptr; | ||
EXPECT_CALL(callbacks_, newStream(_, _)) | ||
.WillOnce(Invoke([&](Http::StreamEncoder& encoder, bool) -> Http::StreamDecoder& { | ||
response_encoder = &encoder; | ||
return decoder; | ||
})); | ||
|
||
EXPECT_THROW(codec_->dispatch(buffer), CodecProtocolException); | ||
EXPECT_EQ("HTTP/1.1 400 Bad Request\r\ncontent-length: 0\r\nconnection: close\r\n\r\n", output); | ||
EXPECT_EQ(p, codec_->protocol()); | ||
if (!details.empty()) { | ||
EXPECT_EQ(details, response_encoder->getStream().responseDetails()); | ||
} | ||
} | ||
|
||
void Http1ServerConnectionImplTest::expectHeadersTest(Protocol p, bool allow_absolute_url, | ||
|
@@ -111,7 +121,8 @@ void Http1ServerConnectionImplTest::expectHeadersTest(Protocol p, bool allow_abs | |
EXPECT_EQ(p, codec_->protocol()); | ||
} | ||
|
||
void Http1ServerConnectionImplTest::testRequestHeadersExceedLimit(std::string header_string) { | ||
void Http1ServerConnectionImplTest::testRequestHeadersExceedLimit(std::string header_string, | ||
absl::string_view details) { | ||
initialize(); | ||
|
||
std::string exception_reason; | ||
|
@@ -127,6 +138,9 @@ void Http1ServerConnectionImplTest::testRequestHeadersExceedLimit(std::string he | |
codec_->dispatch(buffer); | ||
buffer = Buffer::OwnedImpl(header_string + "\r\n"); | ||
EXPECT_THROW_WITH_MESSAGE(codec_->dispatch(buffer), EnvoyException, "headers size exceeds limit"); | ||
if (!details.empty()) { | ||
EXPECT_EQ(details, response_encoder->getStream().responseDetails()); | ||
} | ||
} | ||
|
||
void Http1ServerConnectionImplTest::testRequestHeadersAccepted(std::string header_string) { | ||
|
@@ -287,7 +301,7 @@ TEST_F(Http1ServerConnectionImplTest, Http11InvalidRequest) { | |
|
||
// Invalid because www.somewhere.com is not an absolute path nor an absolute url | ||
Buffer::OwnedImpl buffer("GET www.somewhere.com HTTP/1.1\r\nHost: bah\r\n\r\n"); | ||
expect400(Protocol::Http11, true, buffer); | ||
expect400(Protocol::Http11, true, buffer, "http1.codec_error"); | ||
} | ||
|
||
TEST_F(Http1ServerConnectionImplTest, Http11AbsolutePathNoSlash) { | ||
|
@@ -303,7 +317,7 @@ TEST_F(Http1ServerConnectionImplTest, Http11AbsolutePathBad) { | |
initialize(); | ||
|
||
Buffer::OwnedImpl buffer("GET * HTTP/1.1\r\nHost: bah\r\n\r\n"); | ||
expect400(Protocol::Http11, true, buffer); | ||
expect400(Protocol::Http11, true, buffer, "http1.invalid_url"); | ||
} | ||
|
||
TEST_F(Http1ServerConnectionImplTest, Http11AbsolutePortTooLarge) { | ||
|
@@ -313,6 +327,14 @@ TEST_F(Http1ServerConnectionImplTest, Http11AbsolutePortTooLarge) { | |
expect400(Protocol::Http11, true, buffer); | ||
} | ||
|
||
TEST_F(Http1ServerConnectionImplTest, SketchyConnectionHeader) { | ||
initialize(); | ||
|
||
Buffer::OwnedImpl buffer( | ||
"GET / HTTP/1.1\r\nHost: bah\r\nConnection: a,b,c,d,e,f,g,h,i,j,k,l,m\r\n\r\n"); | ||
expect400(Protocol::Http11, true, buffer, "http1.connection_header_rejected"); | ||
} | ||
|
||
TEST_F(Http1ServerConnectionImplTest, Http11RelativeOnly) { | ||
initialize(); | ||
|
||
|
@@ -422,12 +444,17 @@ TEST_F(Http1ServerConnectionImplTest, HeaderInvalidCharsRejection) { | |
initialize(); | ||
|
||
Http::MockStreamDecoder decoder; | ||
EXPECT_CALL(callbacks_, newStream(_, _)).WillOnce(ReturnRef(decoder)); | ||
|
||
Http::StreamEncoder* response_encoder = nullptr; | ||
EXPECT_CALL(callbacks_, newStream(_, _)) | ||
.WillOnce(Invoke([&](Http::StreamEncoder& encoder, bool) -> Http::StreamDecoder& { | ||
response_encoder = &encoder; | ||
return decoder; | ||
})); | ||
Buffer::OwnedImpl buffer( | ||
absl::StrCat("GET / HTTP/1.1\r\nHOST: h.com\r\nfoo: ", std::string(1, 3), "\r\n")); | ||
EXPECT_THROW_WITH_MESSAGE(codec_->dispatch(buffer), CodecProtocolException, | ||
"http/1.1 protocol error: header value contains invalid chars"); | ||
EXPECT_EQ("http1.invalid_characters", response_encoder->getStream().responseDetails()); | ||
} | ||
|
||
// Regression test for http-parser allowing embedded NULs in header values, | ||
|
@@ -1323,7 +1350,7 @@ TEST_F(Http1ServerConnectionImplTest, LargeRequestHeadersRejected) { | |
// Tests that the default limit for the number of request headers is 100. | ||
TEST_F(Http1ServerConnectionImplTest, ManyRequestHeadersRejected) { | ||
// Send a request with 101 headers. | ||
testRequestHeadersExceedLimit(createHeaderFragment(101)); | ||
testRequestHeadersExceedLimit(createHeaderFragment(101), "http1.too_many_headers"); | ||
} | ||
|
||
TEST_F(Http1ServerConnectionImplTest, LargeRequestHeadersSplitRejected) { | ||
|
@@ -1349,6 +1376,7 @@ TEST_F(Http1ServerConnectionImplTest, LargeRequestHeadersSplitRejected) { | |
// the 60th 1kb header should induce overflow | ||
buffer = Buffer::OwnedImpl(fmt::format("big: {}\r\n", long_string)); | ||
EXPECT_THROW_WITH_MESSAGE(codec_->dispatch(buffer), EnvoyException, "headers size exceeds limit"); | ||
EXPECT_EQ("http1.headers_too_large", response_encoder->getStream().responseDetails()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we get tests that cover each of the new details? |
||
} | ||
|
||
// Tests that the 101th request header causes overflow with the default max number of request | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of returning this error this way, WDYT about just throwing this out with the
CodecProtocolException
? I think this would lead to clearer logic since in the HCM it can just be passed into the code that resets all of the streams?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So I started with that, but it didn't work the way I wanted to, because if one H2 stream has bad headers, I only wanted that particular stream to be tagged with bad headers - the others would maybe eventually get flagged with "you shared a connection with a bad stream" but shouldn't be tagged with the error that wasn't theirs. Only the codec knows which stream causes the problem so it has to be on a per-stream basis to be tagged correctly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK that makes sense. Can you leave a comment trail about that thought process?