Explore essential questions on common JWT security mistakes and best practices in token-based authentication. This quiz highlights real-world vulnerabilities, prevention techniques, and key concepts in JWT security for developers and testers.
When validating incoming JWTs, what security risk arises if you accept tokens signed with the 'none' algorithm as valid in your authentication flow?
Explanation: Accepting JWTs with the 'none' algorithm effectively skips signature validation, allowing attackers to create fake tokens and gain access. This vulnerability is commonly known as the algorithm confusion attack. The none algorithm does not provide automatic encryption or forced expiration. Transforming tokens into opaque format is not an outcome of accepting 'none'; the main danger is the lack of verification, not compatibility issues.
Which is a significant risk when using weak or short secrets to sign HMAC-based JWT tokens in a production environment?
Explanation: Weak or short secrets are susceptible to brute-force attacks; attackers can guess the secret and create valid tokens. Such weak secrets do not make tokens unreadable or increase their size. Additionally, cryptographic algorithms do not automatically change from HMAC to RSA based on key length; the key risk is insufficient protection of tokens through weak secrets.
If JWTs are issued without proper expiration ('exp' claim), what is a potential security consequence in a web application?
Explanation: Without an 'exp' claim, tokens do not expire, allowing an attacker who obtains a token to use it as long as they want. The lack of expiration does not impact secure transmission over HTTPS or cause automatic invalidation at server restart. While some frameworks may warn about missing claims, most will not throw errors solely due to absence of 'exp'.
When choosing where to store JWTs on the client side, why is localStorage considered an insecure option in browser-based applications?
Explanation: Storing tokens in localStorage exposes them to JavaScript, which can be exploited through cross-site scripting (XSS). There is no automatic synchronization across browsers, and servers never directly access browser storage. localStorage does not encrypt or corrupt data, but rather, its exposure to JavaScript is the primary concern.
Why is it important to validate the 'aud' (audience) and 'iss' (issuer) claims when processing JWTs received from third-party sources?
Explanation: Validating the 'aud' and 'iss' claims confirms that the token is meant for your application and was generated by a trusted party, mitigating risks of token replay or misuse. Simply accepting all tokens regardless of audience or issuer is unsafe. Token lifetimes or payload encryption are not handled by these claims; they are used for verifying context and origin, not for token modification.