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

lua: reset downstream_ssl_connection in StreamInfoWrapper when object is marked dead by Lua GC #14092

5 changes: 4 additions & 1 deletion source/extensions/filters/http/lua/wrappers.h
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,10 @@ class StreamInfoWrapper : public Filters::Common::Lua::BaseLuaObject<StreamInfoW
DECLARE_LUA_FUNCTION(StreamInfoWrapper, luaDownstreamDirectRemoteAddress);

// Envoy::Lua::BaseLuaObject
void onMarkDead() override { dynamic_metadata_wrapper_.reset(); }
void onMarkDead() override {
dynamic_metadata_wrapper_.reset();
downstream_ssl_connection_.reset();
}

StreamInfo::StreamInfo& stream_info_;
Filters::Common::Lua::LuaDeathRef<DynamicMetadataMapWrapper> dynamic_metadata_wrapper_;
Expand Down
29 changes: 29 additions & 0 deletions test/extensions/filters/http/lua/lua_filter_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1992,6 +1992,35 @@ TEST_F(LuaHttpFilterTest, InspectStreamInfoDowstreamSslConnectionOnPlainConnecti
EXPECT_EQ(Http::FilterHeadersStatus::Continue, filter_->decodeHeaders(request_headers, true));
}

// Should survive from multiple streamInfo():downstreamSslConnection() calls.
// This is a regression test for #14091.
TEST_F(LuaHttpFilterTest, SurviveMultipleDownstreamSslConnectionCalls) {
const std::string SCRIPT{R"EOF(
function envoy_on_request(request_handle)
if request_handle:streamInfo():downstreamSslConnection() ~= nil then
request_handle:logTrace("downstreamSslConnection is present")
end
end
)EOF"};

setup(SCRIPT);

auto connection_info = std::make_shared<Ssl::MockConnectionInfo>();
Copy link
Member

Choose a reason for hiding this comment

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

Nit. use const? And since you're here probably the above one too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for a review. I will fix it. Just to be sure - by "above one" you mean the other instance of auto connection_info = std::make_shared<Ssl::MockConnectionInfo>(); in this file?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, you're correct. Thank you so much!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

EXPECT_CALL(decoder_callbacks_, streamInfo()).WillRepeatedly(ReturnRef(stream_info_));
EXPECT_CALL(stream_info_, downstreamSslConnection()).WillRepeatedly(Return(connection_info));

for (uint64_t i = 0; i < 200; i++) {
EXPECT_CALL(*filter_,
scriptLog(spdlog::level::trace, StrEq("downstreamSslConnection is present")));

Http::TestRequestHeaderMapImpl request_headers{{":path", "/"}};
EXPECT_EQ(Http::FilterHeadersStatus::Continue, filter_->decodeHeaders(request_headers, true));

filter_->onDestroy();
setupFilter();
}
}

TEST_F(LuaHttpFilterTest, ImportPublicKey) {
const std::string SCRIPT{R"EOF(
function string.fromhex(str)
Expand Down