Use constant-time comparison for the JWE auth tag and the OIDC at_hash claim#411
Open
Nexory wants to merge 1 commit into
Open
Use constant-time comparison for the JWE auth tag and the OIDC at_hash claim#411Nexory wants to merge 1 commit into
Nexory wants to merge 1 commit into
Conversation
…h claim Two authentication-gating comparisons used a plain `!=`, which is timing-variable and leaks how many leading bytes match (CWE-208): - jose/jwe.py `_decrypt_and_auth`: for the HMAC enc modes (A128CBC-HS256 / A192CBC-HS384 / A256CBC-HS512) `auth_tag_check` is the freshly computed HMAC and `auth_tag` is the attacker-supplied tag from the JWE, so this is the authentication gate. - jose/jwt.py `_validate_at_hash`: `claims["at_hash"]` is attacker-influenced (the JWT payload) and `expected_hash` binds the ID token to the access token (OIDC). Both now use hmac.compare_digest, the timing-safe primitive already used in jose/backends/native.py (HMACKey.verify). For at_hash a non-string claim is treated as invalid (preserving the previous semantics) so compare_digest is never called with a non-string. Refs mpdavis#398.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Use constant-time comparison for the JWE auth tag and the OIDC at_hash claim
Two authentication-gating comparisons in the library use plain
!=, which istiming-variable and leaks how many leading bytes match (CWE-208):
jose/jwe.py_decrypt_and_auth:if auth_tag != auth_tag_check:- for theHMAC enc modes (
A128CBC-HS256/A192CBC-HS384/A256CBC-HS512),auth_tag_checkis the freshly computed HMAC andauth_tagis theattacker-supplied tag from the JWE; this
!=is the authentication gate, so atiming oracle helps an attacker forge a valid tag.
jose/jwt.py_validate_at_hash:if claims["at_hash"] != expected_hash:-claims["at_hash"]is attacker-influenced (the JWT payload) andexpected_hashis the server-computed value that binds the ID token to theaccess token (OIDC).
Both are switched to
hmac.compare_digest, which is the timing-safe primitivealready used elsewhere in this package (
jose/backends/native.pyHMACKey.verify). Forat_hash, a non-string claim is treated as invalid (theprevious
!=semantics) socompare_digestis never called with a non-string.Behavior is unchanged for valid and invalid inputs; only the comparison is now
constant-time.
tests/test_jwe.pyandtests/test_jwt.pypass (180 passed, 6skipped);
flake8is clean.Refs the open issue #398, which lists both spots.