Skip to content

Commit

Permalink
Merge pull request #62 from nemmerich/master
Browse files Browse the repository at this point in the history
Fix authentication bypass
  • Loading branch information
cdbattags authored Aug 2, 2023
2 parents b8b1f6e + d0d9f95 commit d1558e2
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
11 changes: 9 additions & 2 deletions lib/resty/jwt.lua
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ local function parse_jwe(self, preshared_key, encoded_header, encoded_encrypted_
local iv = _M:jwt_decode(encoded_iv)
local signature_or_tag = _M:jwt_decode(encoded_auth_tag)
local basic_jwe = {
typ = str_const.JWE,
internal = {
encoded_header = encoded_header,
cipher_text = cipher_text,
Expand Down Expand Up @@ -322,6 +323,7 @@ local function parse_jwt(encoded_header, encoded_payload, signature)
end

local basic_jwt = {
typ = str_const.JWT,
raw_header=encoded_header,
raw_payload=encoded_payload,
header=header,
Expand Down Expand Up @@ -549,7 +551,7 @@ function _M.sign(self, secret_key, jwt_obj)
end
end

if typ == str_const.JWE or jwt_obj.header.enc then
if jwt_obj.typ == str_const.JWE or (jwt_obj.typ == nil and (typ == str_const.JWE or jwt_obj.header.enc)) then
return sign_jwe(self, secret_key, jwt_obj)
end
-- header alg check
Expand Down Expand Up @@ -824,12 +826,17 @@ function _M.verify_jwt_obj(self, secret, jwt_obj, ...)
end

-- if jwe, invoked verify jwe
if jwt_obj[str_const.header][str_const.enc] then
if jwt_obj.typ == str_const.JWE or (jwt_obj.typ == nil and jwt_obj.internal ~= nil and jwt_obj[str_const.header][str_const.enc]) then
return verify_jwe_obj(jwt_obj)
end

local alg = jwt_obj[str_const.header][str_const.alg]

if alg == nil then
jwt_obj[str_const.reason] = "No algorithm supplied"
return jwt_obj
end

local jwt_str = string_format(str_const.regex_jwt_join_str, jwt_obj.raw_header , jwt_obj.raw_payload , jwt_obj.signature)

if self.alg_whitelist ~= nil then
Expand Down
52 changes: 52 additions & 0 deletions t/load-verify.t
Original file line number Diff line number Diff line change
Expand Up @@ -803,4 +803,56 @@ true
everything is awesome~ :p
test
--- no_error_log
[error]
=== TEST 26: Verify invalid JWT which looks like a JWE
--- http_config eval: $::HttpConfig
--- config
location /t {
content_by_lua '
local jwt = require "resty.jwt"
local jwt_str = "eyJ0eXAiOiJKV1QiLCJlbmMiOiJBMjU2R0NNIn0" ..
".eyJmb28iOiJiYXIifQ" ..
".signature"
local jwt_obj = jwt:load_jwt(jwt_str)
local verified_obj = jwt:verify_jwt_obj(
"lua-resty-jwt", jwt_obj, { }
)
ngx.say(jwt_obj["verified"])
ngx.say(jwt_obj["reason"])
';
}
--- request
GET /t
--- response_body
false
No algorithm supplied
--- no_error_log
[error]
=== TEST 26: Verify invalid JWT which looks like a JWE with alg
--- http_config eval: $::HttpConfig
--- config
location /t {
content_by_lua '
local jwt = require "resty.jwt"
local jwt_str = "eyJ0eXAiOiJKV1QiLCJlbmMiOiJBMjU2R0NNIiwiYWxnIjoiSFMyNTYifQ" ..
".eyJmb28iOiJiYXIifQ" ..
".signature"
local jwt_obj = jwt:load_jwt(jwt_str)
local verified_obj = jwt:verify_jwt_obj(
"lua-resty-jwt", jwt_obj, { }
)
ngx.say(jwt_obj["verified"])
ngx.say(jwt_obj["reason"])
';
}
--- request
GET /t
--- response_body
false
signature mismatch: signature
--- no_error_log
[error]

0 comments on commit d1558e2

Please sign in to comment.