Skip to content

Commit

Permalink
fix(jwt) handle empty string claims (Kong#5123)
Browse files Browse the repository at this point in the history
Fixes jwt plugin from throwing a 500, and throws a 401 instead.
  • Loading branch information
jeremyjpj0916 authored and hishamhm committed Oct 14, 2019
1 parent a517117 commit b825a6d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
4 changes: 3 additions & 1 deletion kong/plugins/jwt/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ local function set_consumer(consumer, credential, token)

if credential then
kong.ctx.shared.authenticated_jwt_token = token -- TODO: wrap in a PDK function?
ngx.ctx.authenticated_jwt_token = token -- backward compatibilty only
ngx.ctx.authenticated_jwt_token = token -- backward compatibility only

if credential.username then
set_header(constants.HEADERS.CREDENTIAL_USERNAME, credential.username)
Expand Down Expand Up @@ -149,6 +149,8 @@ local function do_authentication(conf)
local jwt_secret_key = claims[conf.key_claim_name] or header[conf.key_claim_name]
if not jwt_secret_key then
return false, { status = 401, message = "No mandatory '" .. conf.key_claim_name .. "' in claims" }
elseif jwt_secret_key == "" then
return false, { status = 401, message = "Invalid '" .. conf.key_claim_name .. "' in claims" }
end

-- Retrieve the secret
Expand Down
16 changes: 16 additions & 0 deletions spec/03-plugins/16-jwt/03-access_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,22 @@ for _, strategy in helpers.each_strategy() do
local json = cjson.decode(body)
assert.same({ message = "No mandatory 'iss' in claims" }, json)
end)
it("returns 401 if the claims do not contain a valid key to identify a secret", function()
PAYLOAD.iss = ""
local jwt = jwt_encoder.encode(PAYLOAD, "foo")
local authorization = "Bearer " .. jwt
local res = assert(proxy_client:send {
method = "GET",
path = "/request",
headers = {
["Authorization"] = authorization,
["Host"] = "jwt1.com",
}
})
local body = assert.res_status(401, res)
local json = cjson.decode(body)
assert.same({ message = "Invalid 'iss' in claims" }, json)
end)
it("returns 401 Unauthorized if the iss does not match a credential", function()
PAYLOAD.iss = "123456789"
local jwt = jwt_encoder.encode(PAYLOAD, jwt_secret.secret)
Expand Down

0 comments on commit b825a6d

Please sign in to comment.