|
1 | 1 | import base64 |
2 | 2 | import json |
3 | | -from typing import Union, cast |
| 3 | +from typing import Callable, Union, cast |
4 | 4 |
|
5 | 5 | import pytest |
6 | 6 |
|
@@ -89,6 +89,48 @@ def test_hmac_should_throw_exception(self, key: str) -> None: |
89 | 89 | with open(key_path(key)) as keyfile: |
90 | 90 | algo.prepare_key(keyfile.read()) |
91 | 91 |
|
| 92 | + @pytest.mark.parametrize( |
| 93 | + "mutation", |
| 94 | + [ |
| 95 | + lambda key: key.replace(b"-----END", b"\t-----END"), |
| 96 | + lambda key: key.replace(b"\n", b"\r"), |
| 97 | + lambda key: key.replace(b"\n", b""), |
| 98 | + ], |
| 99 | + ) |
| 100 | + def test_hmac_should_reject_loader_accepted_pem_mutations( |
| 101 | + self, mutation: Callable[[bytes], bytes] |
| 102 | + ) -> None: |
| 103 | + algo = HMACAlgorithm(HMACAlgorithm.SHA256) |
| 104 | + with open(key_path("testkey2_rsa.pub.pem"), "rb") as keyfile: |
| 105 | + mutated_key = mutation(keyfile.read()) |
| 106 | + |
| 107 | + with pytest.raises(InvalidKeyError): |
| 108 | + algo.prepare_key(mutated_key) |
| 109 | + |
| 110 | + def test_hmac_should_accept_incomplete_pem_marker(self) -> None: |
| 111 | + algo = HMACAlgorithm(HMACAlgorithm.SHA256) |
| 112 | + key = b"-----BEGIN PUBLIC KEY-----" * 10000 |
| 113 | + |
| 114 | + assert algo.prepare_key(key) == key |
| 115 | + |
| 116 | + def test_hmac_should_reject_a_later_pem_block(self) -> None: |
| 117 | + algo = HMACAlgorithm(HMACAlgorithm.SHA256) |
| 118 | + with open(key_path("testkey2_rsa.pub.pem"), "rb") as keyfile: |
| 119 | + key = b"-----BEGIN PUBLIC KEY-----" + keyfile.read() |
| 120 | + |
| 121 | + with pytest.raises(InvalidKeyError): |
| 122 | + algo.prepare_key(key) |
| 123 | + |
| 124 | + def test_hmac_should_reject_a_pem_block_after_an_overlapping_end_marker( |
| 125 | + self, |
| 126 | + ) -> None: |
| 127 | + algo = HMACAlgorithm(HMACAlgorithm.SHA256) |
| 128 | + with open(key_path("testkey2_rsa.pub.pem"), "rb") as keyfile: |
| 129 | + key = b"-----END CERTIFICATE" + keyfile.read() |
| 130 | + |
| 131 | + with pytest.raises(InvalidKeyError): |
| 132 | + algo.prepare_key(key) |
| 133 | + |
92 | 134 | def test_hmac_jwk_should_parse_and_verify(self) -> None: |
93 | 135 | algo = HMACAlgorithm(HMACAlgorithm.SHA256) |
94 | 136 |
|
|
0 commit comments