|
| 1 | +import jwt |
| 2 | +import pytest |
| 3 | +from jwt.exceptions import InvalidKeyError |
| 4 | + |
| 5 | +priv_key_bytes = b'''-----BEGIN PRIVATE KEY----- |
| 6 | +MC4CAQAwBQYDK2VwBCIEIIbBhdo2ah7X32i50GOzrCr4acZTe6BezUdRIixjTAdL |
| 7 | +-----END PRIVATE KEY-----''' |
| 8 | + |
| 9 | +pub_key_bytes = b'ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIPL1I9oiq+B8crkmuV4YViiUnhdLjCp3hvy1bNGuGfNL' |
| 10 | + |
| 11 | +ssh_priv_key_bytes = b"""-----BEGIN EC PRIVATE KEY----- |
| 12 | +MHcCAQEEIOWc7RbaNswMtNtc+n6WZDlUblMr2FBPo79fcGXsJlGQoAoGCCqGSM49 |
| 13 | +AwEHoUQDQgAElcy2RSSSgn2RA/xCGko79N+7FwoLZr3Z0ij/ENjow2XpUDwwKEKk |
| 14 | +Ak3TDXC9U8nipMlGcY7sDpXp2XyhHEM+Rw== |
| 15 | +-----END EC PRIVATE KEY-----""" |
| 16 | + |
| 17 | +ssh_key_bytes = b"""ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBJXMtkUkkoJ9kQP8QhpKO/TfuxcKC2a92dIo/xDY6MNl6VA8MChCpAJN0w1wvVPJ4qTJRnGO7A6V6dl8oRxDPkc=""" |
| 18 | + |
| 19 | + |
| 20 | +class TestAdvisory: |
| 21 | + def test_ghsa_ffqj_6fqr_9h24(self): |
| 22 | + # Generate ed25519 private key |
| 23 | + # private_key = ed25519.Ed25519PrivateKey.generate() |
| 24 | + |
| 25 | + # Get private key bytes as they would be stored in a file |
| 26 | + # priv_key_bytes = private_key.private_bytes( |
| 27 | + # encoding=serialization.Encoding.PEM, |
| 28 | + # format=serialization.PrivateFormat.PKCS8, |
| 29 | + # encryption_algorithm=serialization.NoEncryption(), |
| 30 | + # ) |
| 31 | + |
| 32 | + # Get public key bytes as they would be stored in a file |
| 33 | + # pub_key_bytes = private_key.public_key().public_bytes( |
| 34 | + # encoding=serialization.Encoding.OpenSSH, |
| 35 | + # format=serialization.PublicFormat.OpenSSH, |
| 36 | + # ) |
| 37 | + |
| 38 | + # Making a good jwt token that should work by signing it |
| 39 | + # with the private key |
| 40 | + # encoded_good = jwt.encode({"test": 1234}, priv_key_bytes, algorithm="EdDSA") |
| 41 | + encoded_good = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJFZERTQSJ9.eyJ0ZXN0IjoxMjM0fQ.M5y1EEavZkHSlj9i8yi9nXKKyPBSAUhDRTOYZi3zZY11tZItDaR3qwAye8pc74_lZY3Ogt9KPNFbVOSGnUBHDg' |
| 42 | + |
| 43 | + # Using HMAC with the public key to trick the receiver to think that the |
| 44 | + # public key is a HMAC secret |
| 45 | + encoded_bad = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0ZXN0IjoxMjM0fQ.6ulDpqSlbHmQ8bZXhZRLFko9SwcHrghCwh8d-exJEE4' |
| 46 | + |
| 47 | + # Both of the jwt tokens are validated as valid |
| 48 | + jwt.decode( |
| 49 | + encoded_good, |
| 50 | + pub_key_bytes, |
| 51 | + algorithms=jwt.algorithms.get_default_algorithms(), |
| 52 | + ) |
| 53 | + |
| 54 | + with pytest.raises(InvalidKeyError): |
| 55 | + jwt.decode( |
| 56 | + encoded_bad, |
| 57 | + pub_key_bytes, |
| 58 | + algorithms=jwt.algorithms.get_default_algorithms(), |
| 59 | + ) |
| 60 | + |
| 61 | + # Of course the receiver should specify ed25519 algorithm to be used if |
| 62 | + # they specify ed25519 public key. However, if other algorithms are used, |
| 63 | + # the POC does not work |
| 64 | + # HMAC specifies illegal strings for the HMAC secret in jwt/algorithms.py |
| 65 | + # |
| 66 | + # invalid_str ings = [ |
| 67 | + # b"-----BEGIN PUBLIC KEY-----", |
| 68 | + # b"-----BEGIN CERTIFICATE-----", |
| 69 | + # b"-----BEGIN RSA PUBLIC KEY-----", |
| 70 | + # b"ssh-rsa", |
| 71 | + # ] |
| 72 | + # |
| 73 | + # However, OKPAlgorithm (ed25519) accepts the following in jwt/algorithms.py: |
| 74 | + # |
| 75 | + # if "-----BEGIN PUBLIC" in str_key: |
| 76 | + # return load_pem_public_key(key) |
| 77 | + # if "-----BEGIN PRIVATE" in str_key: |
| 78 | + # return load_pem_private_key(key, password=None) |
| 79 | + # if str_key[0:4] == "ssh-": |
| 80 | + # return load_ssh_public_key(key) |
| 81 | + # |
| 82 | + # These should most likely made to match each other to prevent this behavior |
| 83 | + |
| 84 | + # POC for the ecdsa-sha2-nistp256 format. |
| 85 | + # openssl ecparam -genkey -name prime256v1 -noout -out ec256-key-priv.pem |
| 86 | + # openssl ec -in ec256-key-priv.pem -pubout > ec256-key-pub.pem |
| 87 | + # ssh-keygen -y -f ec256-key-priv.pem > ec256-key-ssh.pub |
| 88 | + |
| 89 | + # Making a good jwt token that should work by signing it with the private key |
| 90 | + # encoded_good = jwt.encode({"test": 1234}, ssh_priv_key_bytes, algorithm="ES256") |
| 91 | + encoded_good = "eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0ZXN0IjoxMjM0fQ.NX42mS8cNqYoL3FOW9ZcKw8Nfq2mb6GqJVADeMA1-kyHAclilYo_edhdM_5eav9tBRQTlL0XMeu_WFE_mz3OXg" |
| 92 | + |
| 93 | + # Using HMAC with the ssh public key to trick the receiver to think that the public key is a HMAC secret |
| 94 | + # encoded_bad = jwt.encode({"test": 1234}, ssh_key_bytes, algorithm="HS256") |
| 95 | + encoded_bad = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0ZXN0IjoxMjM0fQ.5eYfbrbeGYmWfypQ6rMWXNZ8bdHcqKng5GPr9MJZITU" |
| 96 | + |
| 97 | + # Both of the jwt tokens are validated as valid |
| 98 | + jwt.decode( |
| 99 | + encoded_good, |
| 100 | + ssh_key_bytes, |
| 101 | + algorithms=jwt.algorithms.get_default_algorithms() |
| 102 | + ) |
| 103 | + |
| 104 | + with pytest.raises(InvalidKeyError): |
| 105 | + jwt.decode( |
| 106 | + encoded_bad, |
| 107 | + ssh_key_bytes, |
| 108 | + algorithms=jwt.algorithms.get_default_algorithms() |
| 109 | + ) |
0 commit comments