Skip to content

Commit 8b4e233

Browse files
committed
fix: reject loader-accepted PEM variants
1 parent 1f8180a commit 8b4e233

2 files changed

Lines changed: 55 additions & 9 deletions

File tree

‎jwt/utils.py‎

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -113,18 +113,22 @@ def raw_to_der_signature(raw_sig: bytes, curve: "EllipticCurve") -> bytes:
113113
b"X509 CRL",
114114
}
115115

116-
_PEM_RE = re.compile(
117-
b"----[- ]BEGIN ("
118-
+ b"|".join(_PEMS)
119-
+ b""")[- ]----\r?
120-
.+?\r?
121-
----[- ]END \\1[- ]----\r?\n?""",
122-
re.DOTALL,
116+
# Accept PEM formatting variants that the cryptography loader accepts, while
117+
# retaining the explicit asymmetric-key marker check.
118+
_PEM_MARKER_RE = re.compile(
119+
b"(?=(----[- ](BEGIN|END) (" + b"|".join(_PEMS) + b")[- ]----))"
123120
)
124121

125122

126123
def is_pem_format(key: bytes) -> bool:
127-
return bool(_PEM_RE.search(key))
124+
begin_markers: dict[bytes, int] = {}
125+
for marker in _PEM_MARKER_RE.finditer(key):
126+
marker_type, label = marker.group(2), marker.group(3)
127+
if marker_type == b"BEGIN":
128+
begin_markers[label] = marker.start(1) + len(marker.group(1))
129+
elif label in begin_markers and begin_markers[label] < marker.start(1):
130+
return True
131+
return False
128132

129133

130134
# Based on https://github.com/pyca/cryptography/blob/bcb70852d577b3f490f015378c75cba74986297b/src/cryptography/hazmat/primitives/serialization/ssh.py#L40-L46

‎tests/test_algorithms.py‎

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import base64
22
import json
3-
from typing import Union, cast
3+
from typing import Callable, Union, cast
44

55
import pytest
66

@@ -89,6 +89,48 @@ def test_hmac_should_throw_exception(self, key: str) -> None:
8989
with open(key_path(key)) as keyfile:
9090
algo.prepare_key(keyfile.read())
9191

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+
92134
def test_hmac_jwk_should_parse_and_verify(self) -> None:
93135
algo = HMACAlgorithm(HMACAlgorithm.SHA256)
94136

0 commit comments

Comments
 (0)