Skip to content

Fix issue with header ordering during signing #31

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
5 changes: 3 additions & 2 deletions src/lua/api-gateway/aws/AwsService.lua
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,9 @@ function _M:getRequestArguments(actionName, parameters)
local urlencoded_args = "Action=" .. actionName
if parameters ~= nil then
for key, value in pairs(parameters) do
local proper_val = ngx.re.gsub(tostring(value), "&", "%26", "ijo")
urlencoded_args = urlencoded_args .. "&" .. key .. "=" .. (proper_val or "")
local t = {}
t[key] = tostring(value)
urlencoded_args = urlencoded_args .. "&" .. ngx.encode_args(t)
end
end
return urlencoded_args
Expand Down
21 changes: 6 additions & 15 deletions src/lua/api-gateway/aws/AwsV4Signature.lua
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,13 @@ end
local _sign = _sign_sha256_FFI
local _hash = _sha256_hex

local function get_hashed_canonical_request(method, uri, querystring, headers, requestPayload)
local function get_hashed_canonical_request(method, uri, querystring, host, amzDate, requestPayload)
local hash = method .. '\n' ..
uri .. '\n' ..
(querystring or "") .. '\n'
-- add canonicalHeaders
local canonicalHeaders = ""
local signedHeaders = ""
for h_n,h_v in pairs(headers) do
-- todo: trim and lowercase
canonicalHeaders = canonicalHeaders .. h_n .. ":" .. h_v .. "\n"
signedHeaders = signedHeaders .. h_n .. ";"
end
--remove the last ";" from the signedHeaders
signedHeaders = string.sub(signedHeaders, 1, -2)
-- add canonicalHeaders. Headers must be in alphabetical order
local canonicalHeaders = "host:" .. host .. "\n" .. "x-amz-date:" .. amzDate .. "\n"
local signedHeaders = "host;x-amz-date"

hash = hash .. canonicalHeaders .. "\n"
.. signedHeaders .. "\n"
Expand Down Expand Up @@ -157,9 +150,7 @@ function HmacAuthV4Handler:getSignature(http_method, request_uri, uri_arg_table,
local date1 = self.aws_date_short
local date2 = self.aws_date

local headers = {}
headers.host = self.aws_service .. "." .. self.aws_region .. ".amazonaws.com"
headers["x-amz-date"] = date2
local host = self.aws_service .. "." .. self.aws_region .. ".amazonaws.com"

local encoded_request_uri = request_uri
if (self.doubleUrlEncode == true) then
Expand All @@ -176,7 +167,7 @@ function HmacAuthV4Handler:getSignature(http_method, request_uri, uri_arg_table,
get_hashed_canonical_request(
http_method, encoded_request_uri,
uri_args,
headers, request_payload) ) )
host, date2, request_payload) ) )
return sign
end

Expand Down