python3-pyjwt: fix CVE-2026-102271

This patch applies the upstream fix as referenced in [2], using the
commit shown in [1].

[1] https://github.com/jpadilla/pyjwt/commit/2798504fa2663364573cf2d1043d8d7fef389499
[2] https://github.com/jpadilla/pyjwt/security/advisories/GHSA-p4g4-x82p-q773

Signed-off-by: Hetvi Thakar <hthakar@cisco.com>
Signed-off-by: Anuj Mittal <anuj.mittal@oss.qualcomm.com>
This commit is contained in:
Hetvi Thakar authored and Anuj Mittal committed 2026-10-28 12:31:36 +05:30
1 parent 2d80df4fa5
commit 95a0df87a1
2 files changed
+137

No files matched your search

@@ -0,0 +1,136 @@
From 2798504fa2663364573cf2d1043d8d7fef389499 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jos=C3=A9=20Padilla?= <jpadilla@users.noreply.github.com>
Date: Thu, 10 Sep 2026 08:33:25 -0400
Subject: [PATCH] fix: reject DER public keys as HMAC secrets
CVE: CVE-2026-102271
Upstream-Status: Backport [https://github.com/jpadilla/pyjwt/commit/2798504fa2663364573cf2d1043d8d7fef389499]
(cherry picked from commit 2798504fa2663364573cf2d1043d8d7fef389499)
Signed-off-by: Hetvi Thakar <hthakar@cisco.com>
---
jwt/algorithms.py | 27 ++++++++++++++++++++++++++-
tests/test_algorithms.py | 39 +++++++++++++++++++++++++++++++++++++++
2 files changed, 65 insertions(+), 1 deletion(-)
diff --git a/jwt/algorithms.py b/jwt/algorithms.py
index cf6da45..432fdcf 100644
--- a/jwt/algorithms.py
+++ b/jwt/algorithms.py
@@ -33,6 +33,7 @@ from .utils import (
)
try:
+ from cryptography import x509
from cryptography.exceptions import InvalidSignature, UnsupportedAlgorithm
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes
@@ -72,6 +73,7 @@ try:
NoEncryption,
PrivateFormat,
PublicFormat,
+ load_der_public_key,
load_pem_private_key,
load_pem_public_key,
load_ssh_public_key,
@@ -322,10 +324,33 @@ class HMACAlgorithm(Algorithm):
def __init__(self, hash_alg: HashlibHash) -> None:
self.hash_alg = hash_alg
+ @staticmethod
+ def _is_der_key(key_bytes: bytes) -> bool:
+ if not has_crypto:
+ return False
+
+ try:
+ load_der_public_key(key_bytes)
+ except (TypeError, ValueError, UnsupportedAlgorithm):
+ pass
+ else:
+ return True
+
+ try:
+ x509.load_der_x509_certificate(key_bytes)
+ except (TypeError, ValueError, UnsupportedAlgorithm):
+ return False
+ else:
+ return True
+
def prepare_key(self, key: str | bytes) -> bytes:
key_bytes = force_bytes(key)
- if is_pem_format(key_bytes) or is_ssh_key(key_bytes):
+ if (
+ is_pem_format(key_bytes)
+ or is_ssh_key(key_bytes)
+ or self._is_der_key(key_bytes)
+ ):
raise InvalidKeyError(
"The specified key is an asymmetric key or x509 certificate and"
" should not be used as an HMAC secret."
diff --git a/tests/test_algorithms.py b/tests/test_algorithms.py
index 4e149a7..acaa1fe 100644
--- a/tests/test_algorithms.py
+++ b/tests/test_algorithms.py
@@ -12,6 +12,7 @@ from .keys import load_ec_pub_key_p_521, load_hmac_key, load_rsa_pub_key
from .utils import crypto_required, key_path
if has_crypto:
+ from cryptography import x509
from cryptography.hazmat.primitives.asymmetric.ec import (
EllipticCurvePrivateKey,
EllipticCurvePublicKey,
@@ -28,6 +29,10 @@ if has_crypto:
RSAPrivateKey,
RSAPublicKey,
)
+ from cryptography.hazmat.primitives.serialization import (
+ Encoding,
+ PublicFormat,
+ )
from jwt.algorithms import ECAlgorithm, OKPAlgorithm, RSAAlgorithm, RSAPSSAlgorithm
@@ -187,6 +192,40 @@ class TestAlgorithms:
key = algo.prepare_key('{"this": "is just a json-shaped secret"}')
assert key == b'{"this": "is just a json-shaped secret"}'
+ @crypto_required
+ @pytest.mark.parametrize(
+ "key_format_name",
+ ("SubjectPublicKeyInfo", "PKCS1"),
+ )
+ def test_hmac_prepare_key_rejects_der_public_key(
+ self, key_format_name: str
+ ) -> None:
+ algo = HMACAlgorithm(HMACAlgorithm.SHA256)
+ public_key = cast(RSAPublicKey, load_rsa_pub_key())
+ der_key = public_key.public_bytes(
+ Encoding.DER, getattr(PublicFormat, key_format_name)
+ )
+
+ with pytest.raises(InvalidKeyError, match="asymmetric key"):
+ algo.prepare_key(der_key)
+
+ @crypto_required
+ def test_hmac_prepare_key_rejects_der_certificate(self) -> None:
+ algo = HMACAlgorithm(HMACAlgorithm.SHA256)
+ with open(key_path("testkey_rsa.cer"), "rb") as certificate_file:
+ certificate = x509.load_pem_x509_certificate(certificate_file.read())
+ der_certificate = certificate.public_bytes(Encoding.DER)
+
+ with pytest.raises(InvalidKeyError, match="x509 certificate"):
+ algo.prepare_key(der_certificate)
+
+ @crypto_required
+ def test_hmac_prepare_key_accepts_non_key_binary_secret(self) -> None:
+ algo = HMACAlgorithm(HMACAlgorithm.SHA256)
+ secret = b"\x30\x82not-a-der-key"
+
+ assert algo.prepare_key(secret) == secret
+
@crypto_required
def test_rsa_should_parse_pem_public_key(self) -> None:
algo = RSAAlgorithm(RSAAlgorithm.SHA256)
--
2.35.6
@@ -15,6 +15,7 @@ SRC_URI += "file://CVE-2026-48522.patch \
file://CVE-2026-102269.patch \
file://CVE-2026-102267.patch \
file://CVE-2026-102268_CVE-2026-102270.patch \
file://CVE-2026-102271.patch \
"
SRC_URI[sha256sum] = "c74a7a2adf861c04d002db713dd85f84beb242228e671280bf709d765b03672b"