fix: pad HMAC key to 64 bytes to avoid InsecureKeyLengthWarning (#37) - #41
Merged
Merged
Conversation
Duo client secrets are 40 bytes, below the 64-byte minimum PyJWT recommends for HS512 (RFC 7518 Section 3.2), so PyJWT emits an InsecureKeyLengthWarning on every jwt.encode call. Zero-pad the secret to 64 bytes before using it as the HMAC key. HMAC zero-pads the key to the hash block size (128 bytes for SHA-512) regardless, so this produces a byte-identical signature and remains wire-compatible with Duo while silencing the warning. This mirrors the fix in the duo_universal_csharp library (PR #23). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
AaronAtDuo
enabled auto-merge (squash)
August 27, 2026 16:24
jeffreyparker
approved these changes
Aug 27, 2026
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.
Summary
Fixes #37. PyJWT emits an
InsecureKeyLengthWarningon everyjwt.encode/jwt.decodecall because Duo client secrets are 40 bytes, below the 64-byte minimum PyJWT recommends for HS512 (RFC 7518 Section 3.2). This surfaces to users as noisy warnings in their app logs (e.g. Django).This zero-pads the secret to 64 bytes before using it as the HMAC key. This mirrors the fix already shipped in
duo_universal_csharp(PR #23, "Pad the JWT secret out to 64 bytes if necessary").Why this is safe
HMAC internally zero-pads the key to the hash block size (128 bytes for SHA-512) regardless of input length. A 40-byte key and that same key zero-padded to 64 bytes therefore produce a byte-identical signature. Verified:
jwt.encode(payload, secret)==jwt.encode(payload, secret.ljust(64, b'\x00'))So this stays wire-compatible with Duo's servers (which sign with the 40-byte secret) and preserves the wrong-secret rejection path.
self._client_secretis left unchanged; only the derived signing key is padded.Testing
-W error::UserWarning— no moreInsecureKeyLengthWarning.flake8clean.This PR description was generated with AI assistance (Claude).
🤖 Generated with Claude Code