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 filter: support encoding binary data via base64Escape #13057

Merged
merged 3 commits into from
Sep 14, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 3 additions & 3 deletions source/extensions/filters/http/lua/lua_filter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -618,9 +618,9 @@ int StreamHandleWrapper::luaImportPublicKey(lua_State* state) {
}

int StreamHandleWrapper::luaBase64Escape(lua_State* state) {
// Get input string.
absl::string_view input = luaL_checkstring(state, 2);
auto output = absl::Base64Escape(input);
size_t input_size;
const char* input = luaL_checklstring(state, 2, &input_size);
auto output = absl::Base64Escape(absl::string_view(input, input_size));
lua_pushlstring(state, output.data(), output.length());

return 1;
Expand Down
17 changes: 13 additions & 4 deletions test/extensions/filters/http/lua/lua_filter_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2202,8 +2202,10 @@ TEST_F(LuaHttpFilterTest, LuaFilterBase64Escape) {
end

function envoy_on_response(response_handle)
local base64Encoded = response_handle:base64Escape("barfoo")
response_handle:logTrace(base64Encoded)
local resp_body_buf = response_handle:body()
local resp_body = resp_body_buf:getBytes(0, resp_body_buf:length())
local b64_resp_body = response_handle:base64Escape(resp_body)
response_handle:logTrace(b64_resp_body)
end
)EOF"};

Expand All @@ -2216,9 +2218,16 @@ TEST_F(LuaHttpFilterTest, LuaFilterBase64Escape) {
EXPECT_EQ(Http::FilterHeadersStatus::Continue, filter_->decodeHeaders(request_headers, true));

Http::TestResponseHeaderMapImpl response_headers{{":status", "200"}};
EXPECT_EQ(Http::FilterHeadersStatus::StopIteration,
filter_->encodeHeaders(response_headers, false));

EXPECT_CALL(*filter_, scriptLog(spdlog::level::trace, StrEq("YmFyZm9v")));
EXPECT_EQ(Http::FilterHeadersStatus::Continue, filter_->encodeHeaders(response_headers, true));
Copy link
Member

Choose a reason for hiding this comment

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

Should we keep a test with a simple string as input as well?

// base64 encoding should also work for binary data
Copy link
Member

Choose a reason for hiding this comment

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

Super nit.

// Base64 encoding should also work for binary data.

uint8_t buffer[34] = {31, 139, 8, 0, 0, 0, 0, 0, 0, 255, 202, 72, 205, 201, 201, 47, 207,
47, 202, 73, 1, 4, 0, 0, 255, 255, 173, 32, 235, 249, 10, 0, 0, 0};
Buffer::OwnedImpl response_body(buffer, 34);
EXPECT_CALL(*filter_, scriptLog(spdlog::level::trace,
StrEq("H4sIAAAAAAAA/8pIzcnJL88vykkBBAAA//+tIOv5CgAAAA==")));
EXPECT_EQ(Http::FilterDataStatus::Continue, filter_->encodeData(response_body, true));
}

} // namespace
Expand Down