diff mbox series

[meta-python,scarthgap,4/8] python3-pyjwt: fix CVE-2026-102271

Message ID 20261105045614.2677776-4-hthakar@cisco.com
State New
Headers show
Series [meta-python,scarthgap,1/8] python3-pyjwt: fix CVE-2026-102269 | expand

Commit Message

From: Hetvi Thakar <hthakar@cisco.com>

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

Reject DER-encoded public keys and certificates when preparing an HMAC
key to prevent asymmetric-key material from being accepted as an HMAC
secret.

[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>
---
 .../python3-pyjwt/CVE-2026-102271.patch       | 141 ++++++++++++++++++
 .../python/python3-pyjwt_2.8.0.bb             |   1 +
 2 files changed, 142 insertions(+)
 create mode 100644 meta-python/recipes-devtools/python/python3-pyjwt/CVE-2026-102271.patch
diff mbox series

Patch

diff --git a/meta-python/recipes-devtools/python/python3-pyjwt/CVE-2026-102271.patch b/meta-python/recipes-devtools/python/python3-pyjwt/CVE-2026-102271.patch
new file mode 100644
index 0000000000..82938afb27
--- /dev/null
+++ b/meta-python/recipes-devtools/python/python3-pyjwt/CVE-2026-102271.patch
@@ -0,0 +1,141 @@ 
+From 20f172fe70aad3314ee988d343c5f1fb05b4ff70 Mon Sep 17 00:00:00 2001
+From: =?UTF-8?q?Jos=C3=A9=20Padilla?= <jpadilla@users.noreply.github.com>
+Date: Wed, 14 Oct 2026 05:10:38 -0700
+Subject: [PATCH] fix: reject DER public keys as HMAC secrets
+
+CVE: CVE-2026-102271
+Upstream-Status: Backport [https://github.com/jpadilla/pyjwt/commit/2798504fa2663364573cf2d1043d8d7fef389499]
+
+Backport Changes:
+- Import UnsupportedAlgorithm, which the PyJWT 2.8.0 algorithms module
+  does not already import, for the DER loader exception handlers.
+
+Signed-off-by: Hetvi Thakar <hthakar@cisco.com>
+---
+ jwt/algorithms.py        | 29 +++++++++++++++++++++++++++--
+ tests/test_algorithms.py | 39 +++++++++++++++++++++++++++++++++++++++
+ 2 files changed, 66 insertions(+), 2 deletions(-)
+
+diff --git a/jwt/algorithms.py b/jwt/algorithms.py
+index b6303ed..fcfca89 100644
+--- a/jwt/algorithms.py
++++ b/jwt/algorithms.py
+@@ -28,7 +28,8 @@ else:
+ 
+ 
+ try:
+-    from cryptography.exceptions import InvalidSignature
++    from cryptography import x509
++    from cryptography.exceptions import InvalidSignature, UnsupportedAlgorithm
+     from cryptography.hazmat.backends import default_backend
+     from cryptography.hazmat.primitives import hashes
+     from cryptography.hazmat.primitives.asymmetric import padding
+@@ -67,6 +68,7 @@ try:
+         NoEncryption,
+         PrivateFormat,
+         PublicFormat,
++        load_der_public_key,
+         load_pem_private_key,
+         load_pem_public_key,
+         load_ssh_public_key,
+@@ -261,10 +263,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 0fbffc9..c843903 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
+ 
+@@ -172,6 +177,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):
+         algo = RSAAlgorithm(RSAAlgorithm.SHA256)
+-- 
+2.35.6
diff --git a/meta-python/recipes-devtools/python/python3-pyjwt_2.8.0.bb b/meta-python/recipes-devtools/python/python3-pyjwt_2.8.0.bb
index f62b8aa7bb..612b1c9cde 100644
--- a/meta-python/recipes-devtools/python/python3-pyjwt_2.8.0.bb
+++ b/meta-python/recipes-devtools/python/python3-pyjwt_2.8.0.bb
@@ -15,6 +15,7 @@  SRC_URI += " \
     file://CVE-2026-102269_p2.patch \
     file://CVE-2026-102267.patch \
     file://CVE-2026-102268_CVE-2026-102270.patch \
+    file://CVE-2026-102271.patch \
 "
 SRC_URI[sha256sum] = "57e28d156e3d5c10088e0c68abb90bfac3df82b40a71bd0daa20c65ccd5c23de"