Skip to content

Commit

Permalink
fix(key-auth) return 401 Unauthorized on empty keys
Browse files Browse the repository at this point in the history
Fix #4405
From #4418

Signed-off-by: Thibault Charbonnier <thibaultcha@me.com>
  • Loading branch information
hbagdi authored and thibaultcha committed Mar 22, 2019
1 parent 497f145 commit 600b12b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion kong/plugins/key-auth/handler.lua
Expand Up @@ -143,7 +143,7 @@ local function do_authentication(conf)
end

-- this request is missing an API key, HTTP 401
if not key then
if not key or key == "" then
kong.response.set_header("WWW-Authenticate", _realm)
return nil, { status = 401, message = "No API key found in request" }
end
Expand Down
13 changes: 13 additions & 0 deletions spec/03-plugins/09-key-auth/02-access_spec.lua
Expand Up @@ -169,6 +169,19 @@ for _, strategy in helpers.each_strategy() do
local json = cjson.decode(body)
assert.same({ message = "No API key found in request" }, json)
end)
it("returns Unauthorized on empty key header", function()
local res = assert(proxy_client:send {
method = "GET",
path = "/status/200",
headers = {
["Host"] = "key-auth1.com",
["apikey"] = "",
}
})
local body = assert.res_status(401, res)
local json = cjson.decode(body)
assert.same({ message = "No API key found in request" }, json)
end)
it("returns WWW-Authenticate header on missing credentials", function()
local res = assert(proxy_client:send {
method = "GET",
Expand Down

0 comments on commit 600b12b

Please sign in to comment.