Skip to content

Commit

Permalink
Validating an unsigned token with a key should be false.
Browse files Browse the repository at this point in the history
If the token supplied to the `verify` function is has a signature which
is an empty-string, the key is ignored, presuming that the token is
unsigned and that the calling code is not interested in ensuring the
token has been signed.

If the calling code is trying to verify that the token was signed with
their secret key, it is possible for a completely unsigned token to be
accepted as valid.

This patch adds a check to ensure that if the token is unsigned, but a
non-empty key was supplied to `validate`, then the token is considered
to not be valid.
  • Loading branch information
Shane Kilkelly committed Sep 13, 2014
1 parent a17fe62 commit d07210c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/clj_jwt/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@
([this key]
(let [alg (-> this :header :alg keyword)]
(cond
(and (= :none alg) (not (= "" key)))
false

(= :none alg) (= "" (:signature this))

(supported-algorithm? alg)
Expand All @@ -87,4 +90,3 @@
(->JWT (encoded-json->map header)
(encoded-json->map claims)
(or signature ""))))

3 changes: 3 additions & 0 deletions test/clj_jwt/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,19 @@
(fact "HS256 signed JWT should be verified."
(-> claim jwt (sign "foo") (verify "foo")) => true
(-> claim jwt (sign "foo") to-str str->jwt (verify "foo")) => true
(-> claim jwt to-str str->jwt (verify "foo")) => false
(-> claim jwt (sign "foo") (verify "bar")) => false)

(fact "HS384 signed JWT should be verified."
(-> claim jwt (sign :HS384 "foo") (verify "foo")) => true
(-> claim jwt (sign :HS384 "foo") to-str str->jwt (verify "foo")) => true
(-> claim jwt to-str str->jwt (verify "foo")) => false
(-> claim jwt (sign :HS384 "foo") (verify "bar")) => false)

(fact "HS512 signed JWT should be verified."
(-> claim jwt (sign :HS512 "foo") (verify "foo")) => true
(-> claim jwt (sign :HS512 "foo") to-str str->jwt (verify "foo")) => true
(-> claim jwt to-str str->jwt (verify "foo")) => false
(-> claim jwt (sign :HS512 "foo") (verify "bar")) => false)

(fact "RS256 signed JWT should be verified."
Expand Down

0 comments on commit d07210c

Please sign in to comment.