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

fix(aws-lambda): add null handling for multiValueHeaders #13533

Merged
merged 3 commits into from
Sep 5, 2024
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
@@ -0,0 +1,3 @@
message: "**AWS-Lambda**: Fixed an issue in proxy integration mode that caused internal server error when the `multiValueHeaders` is null."
type: bugfix
scope: Plugin
10 changes: 6 additions & 4 deletions kong/plugins/aws-lambda/request-util.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
local kong = kong
local ngx_encode_base64 = ngx.encode_base64
local ngx_decode_base64 = ngx.decode_base64
local null = ngx.null
local cjson = require "cjson.safe"

local date = require("date")
Expand Down Expand Up @@ -89,6 +90,10 @@ local function validate_custom_response(response)
return nil, "body must be a string"
end

if response.isBase64Encoded ~= nil and type(response.isBase64Encoded) ~= "boolean" then
return nil, "isBase64Encoded must be a boolean"
end

return true
end

Expand Down Expand Up @@ -118,13 +123,10 @@ local function extract_proxy_response(content)
local isBase64Encoded = serialized_content.isBase64Encoded
if isBase64Encoded == true then
body = ngx_decode_base64(body)

elseif isBase64Encoded ~= false and isBase64Encoded ~= nil then
return nil, "isBase64Encoded must be a boolean"
end

local multiValueHeaders = serialized_content.multiValueHeaders
if multiValueHeaders then
if multiValueHeaders and multiValueHeaders ~= null then
for header, values in pairs(multiValueHeaders) do
headers[header] = values
end
Expand Down
38 changes: 38 additions & 0 deletions spec/03-plugins/27-aws-lambda/99-access_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,12 @@ for _, strategy in helpers.each_strategy() do
service = null,
}

local route29 = bp.routes:insert {
hosts = { "lambda29.test" },
protocols = { "http", "https" },
service = null,
}

bp.plugins:insert {
name = "aws-lambda",
route = { id = route1.id },
Expand Down Expand Up @@ -580,6 +586,19 @@ for _, strategy in helpers.each_strategy() do
}
}

bp.plugins:insert {
name = "aws-lambda",
route = { id = route29.id },
config = {
port = 10001,
aws_key = "mock-key",
aws_secret = "mock-secret",
aws_region = "us-east-1",
function_name = "functionWithNullMultiValueHeaders",
is_proxy_integration = true,
}
}

fixtures.dns_mock:A({
name = "custom.lambda.endpoint",
address = "127.0.0.1",
Expand Down Expand Up @@ -1154,6 +1173,25 @@ for _, strategy in helpers.each_strategy() do
assert.equal("Bad Gateway", b.message)
end)

it("do not throw error when 'multiValueHeaders' is JSON null", function ()
local res = assert(proxy_client:send {
method = "POST",
path = "/post",
headers = {
["Host"] = "lambda11.test",
["Content-Type"] = "application/json",
},
body = {
statusCode = 201,
body = "test",
multiValueHeaders = cjson.null,
}
})

local body = assert.res_status(201, res)
assert.same(body, "test")
end)

it("returns HTTP 502 with when response from lambda is not valid JSON", function()
local res = assert(proxy_client:send {
method = "POST",
Expand Down
Loading