summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMark Adams <mark@markadams.me>2015-03-17 20:21:43 -0500
committerMark Adams <mark@markadams.me>2015-03-17 20:21:43 -0500
commitf6d0ff2778a4f3d2f57935e5f7ed0006fe219d4c (patch)
treefdea98e3f4936948519994441431bd9fda6f869f
parent14c5f46391aa1fbaf07be4905998a70dcededa62 (diff)
downloadpyjwt-f6d0ff2778a4f3d2f57935e5f7ed0006fe219d4c.tar.gz
Added tests to cover invalid string validations on HMAC
-rw-r--r--jwt/algorithms.py8
-rw-r--r--tests/keys/testkey_rsa.cer21
-rw-r--r--tests/test_algorithms.py41
3 files changed, 68 insertions, 2 deletions
diff --git a/jwt/algorithms.py b/jwt/algorithms.py
index f539b5e..b22a1d9 100644
--- a/jwt/algorithms.py
+++ b/jwt/algorithms.py
@@ -103,7 +103,13 @@ class HMACAlgorithm(Algorithm):
if isinstance(key, text_type):
key = key.encode('utf-8')
- if (b'-----BEGIN PUBLIC KEY-----' in key or b'-----BEGIN CERTIFICATE-----' in key):
+ invalid_strings = [
+ b'-----BEGIN PUBLIC KEY-----',
+ b'-----BEGIN CERTIFICATE-----',
+ b'ssh-rsa'
+ ]
+
+ if any([string_value in key for string_value in invalid_strings]):
raise InvalidKeyError(
'The specified key is an assymetric key or x509 certificate and'
' should not be used as an HMAC secret.')
diff --git a/tests/keys/testkey_rsa.cer b/tests/keys/testkey_rsa.cer
new file mode 100644
index 0000000..1682ae8
--- /dev/null
+++ b/tests/keys/testkey_rsa.cer
@@ -0,0 +1,21 @@
+-----BEGIN CERTIFICATE-----
+MIIDhTCCAm2gAwIBAgIJANE4sir3EkX8MA0GCSqGSIb3DQEBCwUAMFkxCzAJBgNV
+BAYTAlVTMQ4wDAYDVQQIDAVUZXhhczEPMA0GA1UEBwwGQXVzdGluMQ4wDAYDVQQK
+DAVQeUpXVDEZMBcGA1UECwwQVGVzdCBDZXJ0aWZpY2F0ZTAeFw0xNTAzMTgwMTE2
+MTRaFw0xODAzMT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orithms.py
index 0fafc4d..7a10a90 100644
--- a/tests/test_algorithms.py
+++ b/tests/test_algorithms.py
@@ -1,6 +1,7 @@
import base64
-from jwt.algorithms import Algorithm, HMACAlgorithm
+from jwt.algorithms import Algorithm, HMACAlgorithm, NoneAlgorithm
+from jwt.exceptions import InvalidKeyError
from .compat import unittest
from .utils import ensure_bytes, ensure_unicode, key_path
@@ -35,6 +36,12 @@ class TestAlgorithms(unittest.TestCase):
with self.assertRaises(NotImplementedError):
algo.verify('message', 'key', 'signature')
+ def test_none_algorithm_should_throw_exception_if_key_is_not_none(self):
+ algo = NoneAlgorithm()
+
+ with self.assertRaises(InvalidKeyError):
+ algo.prepare_key('123')
+
def test_hmac_should_reject_nonstring_key(self):
algo = HMACAlgorithm(HMACAlgorithm.SHA256)
@@ -50,6 +57,38 @@ class TestAlgorithms(unittest.TestCase):
algo.prepare_key(ensure_unicode('awesome'))
@unittest.skipIf(not has_crypto, 'Not supported without cryptography library')
+ def test_hmac_should_throw_exception_if_key_is_pem_public_key(self):
+ algo = HMACAlgorithm(HMACAlgorithm.SHA256)
+
+ with self.assertRaises(InvalidKeyError):
+ with open(key_path('testkey2_rsa.pub.pem'), 'r') as keyfile:
+ algo.prepare_key(keyfile.read())
+
+ @unittest.skipIf(not has_crypto, 'Not supported without cryptography library')
+ def test_hmac_should_throw_exception_if_key_is_x509_certificate(self):
+ algo = HMACAlgorithm(HMACAlgorithm.SHA256)
+
+ with self.assertRaises(InvalidKeyError):
+ with open(key_path('testkey_rsa.cer'), 'r') as keyfile:
+ algo.prepare_key(keyfile.read())
+
+ @unittest.skipIf(not has_crypto, 'Not supported without cryptography library')
+ def test_hmac_should_throw_exception_if_key_is_ssh_public_key(self):
+ algo = HMACAlgorithm(HMACAlgorithm.SHA256)
+
+ with self.assertRaises(InvalidKeyError):
+ with open(key_path('testkey_rsa.pub'), 'r') as keyfile:
+ algo.prepare_key(keyfile.read())
+
+ @unittest.skipIf(not has_crypto, 'Not supported without cryptography library')
+ def test_hmac_should_throw_exception_if_key_is_x509_cert(self):
+ algo = HMACAlgorithm(HMACAlgorithm.SHA256)
+
+ with self.assertRaises(InvalidKeyError):
+ with open(key_path('testkey2_rsa.pub.pem'), 'r') as keyfile:
+ algo.prepare_key(keyfile.read())
+
+ @unittest.skipIf(not has_crypto, 'Not supported without cryptography library')
def test_rsa_should_parse_pem_public_key(self):
algo = RSAAlgorithm(RSAAlgorithm.SHA256)