Skip to content

Commit

Permalink
fix(aws-lambda): add null handling for multiValueHeaders (Kong#13533)
Browse files Browse the repository at this point in the history
A small PR for adding a null handling code for the multiValueHeaders field. Also contains a small refactor to put isBase64Encode check into the validate function to keep consistency with other fields

FTI-6168
  • Loading branch information
windmgc authored and andrewgkew committed Sep 5, 2024
1 parent 89669d5 commit e7042d3
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
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

0 comments on commit e7042d3

Please sign in to comment.