Debian Codegraph @

tests/test_advisory.py

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