liquidz / clj-jwt

Clojure library for JSON Web Token(JWT)
118 stars 31 forks source link

Critical vulnerabilities in JSON Web Token libraries #13

Closed donbonifacio closed 9 years ago

donbonifacio commented 9 years ago

Hello, Are you aware of this: https://auth0.com/blog/2015/03/31/critical-vulnerabilities-in-json-web-token-libraries/

liquidz commented 9 years ago

Thank you for your reporting. I'll review it.

liquidz commented 9 years ago

:none algorithm

Most (hopefully all?) implementations now have a basic check to prevent this attack: if a secret key was provided, then token verification will fail for tokens using the none algorithm.

Currently, clj-jwt does not allows to specify key to verify not signed token (:none algorithm).

:RS256 / :ES256 -> :HS256

clj-jwt handles public key as an object of bouncycastle, so verifying :HS256 signed token with RSA public key will fail.

Test codes

I tested clj-jwt codes with following test case and it is not vulnerable.

(ns clj-jwt.attack-test
  (:require
    [clj-jwt.core   :refer :all]
    [clj-jwt.sign   :refer :all]
    [clj-jwt.key    :refer [private-key public-key]]
    [clojure.string :as str]
    [clojure.test   :refer :all]))

(def pub-key-path "test/files/rsa/no_pass.pub.key")
(def rsa-prv-key  (private-key "test/files/rsa/no_pass.key"))
(def rsa-pub-key  (public-key  pub-key-path))

(deftest test-algorithm->none-attack
  (let [key "secret"
        original (-> {:foo "bar"} jwt (sign :HS256 key))
        attacked (update-in original [:header :alg] (constantly "none"))]
    (is (verify original key))
    (is (not (verify attacked key)))))

(deftest test-rsa->hmac-attack
  (let [base      (jwt {:foo "bar"})
        original  (sign base :RS256 rsa-prv-key)
        hmac-sign (-> base (sign :HS256 (str/trim (slurp pub-key-path))) :signature)
        attacked  (-> original
                      (update-in [:header :alg] (constantly "HS256"))
                      (update-in [:signature]   (constantly hmac-sign)))]
    (is (verify original rsa-pub-key))
    (is (thrown? Exception (verify attacked rsa-pub-key)))))

ToDo

  1. Add optional parameter that specifies algorithm to verify algorithm in token header.
donbonifacio commented 9 years ago

Great work @liquidz :)

liquidz commented 9 years ago

ToDo

  1. Add optional parameter that specifies algorithm to verify algorithm in token header.

I supported this optional parameter in ver 0.0.13.

https://clojars.org/clj-jwt/versions/0.0.13