The vulnerability, identified as GHSA-xgmm-8j9v-c9wx, is an algorithm confusion attack in the PyJWT library. The root cause is the insufficient validation of the key in the HMACAlgorithm.prepare_key function. The provided PoC demonstrates that when jws.decode is called with a list of algorithms including both a symmetric (HS256) and an asymmetric one (RS256), and a public key in JWK format is passed as the key, the library incorrectly uses the JWK as the secret for the HMAC algorithm.
The analysis of the patch commit 95791b1759b8aa4f2203575d344d5c78564cdc81, which fixes the vulnerability in version 2.13.0, confirms this. The commit message explicitly states that it 'rejects JWK JSON documents passed as raw HMAC secrets' in HMACAlgorithm.prepare_key. The code changes in jwt/algorithms.py add logic to detect if the key is a JSON string that looks like a JWK (by checking for a kty key). If it is, an InvalidKeyError is raised. This prevents the public key from being used as an HMAC secret.
Therefore, the vulnerable function is HMACAlgorithm.prepare_key. During exploitation, a call to a decoding function like PyJWS.decode would lead to the execution of this vulnerable method, which would then incorrectly prepare the malicious key for the signature verification process.