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

Set the downstream addresses early to prevent a logging segfault. #2790

Merged
merged 2 commits into from
Mar 14, 2018
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
14 changes: 9 additions & 5 deletions source/common/http/conn_manager_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,14 @@ ConnectionManagerImpl::ActiveStream::ActiveStream(ConnectionManagerImpl& connect
} else {
connection_manager_.stats_.named_.downstream_rq_http1_total_.inc();
}
request_info_.downstream_local_address_ =
connection_manager_.read_callbacks_->connection().localAddress();
// Initially, the downstream remote address is the source address of the
// downstream connection. That can change later in the request's lifecycle,
// based on XFF processing, but setting the downstream remote address here
// prevents surprises for logging code in edge cases.
request_info_.downstream_remote_address_ =
connection_manager_.read_callbacks_->connection().remoteAddress();
}

ConnectionManagerImpl::ActiveStream::~ActiveStream() {
Expand Down Expand Up @@ -519,11 +527,7 @@ void ConnectionManagerImpl::ActiveStream::decodeHeaders(HeaderMapPtr&& headers,
state_.saw_connection_close_ = true;
}

// TODO(mattklein123): We should set downstream_local_address_ and downstream_remote_address_
// as early as possible in this function since there are various places where we can return and
// still log before we get here.
request_info_.downstream_local_address_ =
connection_manager_.read_callbacks_->connection().localAddress();
// Modify the downstream remote address depending on configuration and headers.
request_info_.downstream_remote_address_ = ConnectionManagerUtility::mutateRequestHeaders(
*request_headers_, protocol, connection_manager_.read_callbacks_->connection(),
connection_manager_.config_, *snapped_route_config_, connection_manager_.random_generator_,
Expand Down
37 changes: 37 additions & 0 deletions test/common/http/conn_manager_impl_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -914,6 +914,43 @@ TEST_F(HttpConnectionManagerImplTest, TestAccessLog) {
conn_manager_->onData(fake_input, false);
}

TEST_F(HttpConnectionManagerImplTest, TestAccessLogWithInvalidRequest) {
setup(false, "");

std::shared_ptr<MockStreamDecoderFilter> filter(new NiceMock<MockStreamDecoderFilter>());
std::shared_ptr<AccessLog::MockInstance> handler(new NiceMock<AccessLog::MockInstance>());

EXPECT_CALL(filter_factory_, createFilterChain(_))
.WillOnce(Invoke([&](FilterChainFactoryCallbacks& callbacks) -> void {
callbacks.addStreamDecoderFilter(filter);
callbacks.addAccessLogHandler(handler);
}));

EXPECT_CALL(*handler, log(_, _, _))
.WillOnce(Invoke(
[](const HeaderMap*, const HeaderMap*, const RequestInfo::RequestInfo& request_info) {
EXPECT_TRUE(request_info.responseCode().valid());
EXPECT_EQ(request_info.responseCode().value(), uint32_t(400));
EXPECT_NE(nullptr, request_info.downstreamLocalAddress());
EXPECT_NE(nullptr, request_info.downstreamRemoteAddress());
EXPECT_EQ(nullptr, request_info.routeEntry());
}));

StreamDecoder* decoder = nullptr;
NiceMock<MockStreamEncoder> encoder;
EXPECT_CALL(*codec_, dispatch(_)).WillRepeatedly(Invoke([&](Buffer::Instance& data) -> void {
decoder = &conn_manager_->newStream(encoder);

// These request headers are missing the necessary ":host"
HeaderMapPtr headers{new TestHeaderMapImpl{{":method", "GET"}, {":path", "/"}}};
decoder->decodeHeaders(std::move(headers), true);
data.drain(0);
}));

Buffer::OwnedImpl fake_input;
conn_manager_->onData(fake_input, false);
}

TEST_F(HttpConnectionManagerImplTest, DoNotStartSpanIfTracingIsNotEnabled) {
setup(false, "");

Expand Down