Skip to content

Conversation

amanjolhe
Copy link

@amanjolhe amanjolhe commented Aug 20, 2025


Security Fix: CVE-2025-45768 – Enforce Minimum Key Length Validation with Configurable API

This PR resolves CVE-2025-45768 by enforcing minimum key length requirements for HMAC and RSA algorithms, with a professional function-based API for configuration, in compliance with RFC 7518 and NIST SP800-117.

🔒 Security Enhancements

  • HMAC Algorithms:
    • HS256: Minimum 32 bytes (256 bits)
    • HS384: Minimum 48 bytes (384 bits)
    • HS512: Minimum 64 bytes (512 bits)
  • RSA Algorithms:
    • Minimum 2048-bit key size enforced
  • Validation Coverage:
    • Enhanced validation in prepare_key() and from_jwk() methods
    • Applies to all key input formats: bytes, PEM, and JWK

🚀 New API Functions

  • set_min_key_length_enforcement(enforce: bool) – Configure validation behavior
    • True (default): Raises InvalidKeyError for weak keys
    • False (deprecated): Issues security warnings but allows operation
  • get_min_key_length_enforcement() -> bool – Get current enforcement mode
  • Deprecation Warning System – Warns when enforcement is disabled

📚 Documentation & Testing

  • Comprehensive Documentation:
    • New "Security Considerations" section in usage docs
    • Key generation examples and best practices
  • Robust Test Suite:
    • 5 new API configuration tests
    • All existing tests updated (333 total passing)
    • Test secrets upgraded to meet security requirements

⚠️ Breaking Change

  • Default Behavior: Keys below minimum length are rejected with InvalidKeyError
  • Migration Path: Temporarily disable enforcement with deprecation warnings
  • Future: Enforcement will be mandatory in PyJWT 3.0

Example Usage

import jwt

# Default: enforcement enabled (recommended)
jwt.encode(payload, strong_secret, algorithm='HS256')

# Temporary migration mode (deprecated)
jwt.algorithms.set_min_key_length_enforcement(False)
jwt.encode(payload, weak_secret, algorithm='HS256')  # Issues warning

# Check current mode
is_enforced = jwt.algorithms.get_min_key_length_enforcement()

@amanjolhe
Copy link
Author

Hello @jpadilla, @auvipy ,please review. The last PR was removed because I made my repository private.

@auvipy auvipy requested review from auvipy and Copilot August 20, 2025 11:29
Copilot

This comment was marked as outdated.

@amanjolhe amanjolhe force-pushed the security/cve-2025-45768-key-length-validation branch from f640139 to be2fe1b Compare August 20, 2025 12:28
@amanjolhe
Copy link
Author

amanjolhe commented Aug 20, 2025

Hi @auvipy ,
This is the same PR you approved earlier today. I just wanted to check when the security fix will be released, as our organization’s project is currently using this library. It has been flagged by both CxOne and Black Duck, and many companies are now considering migrating to alternative libraries due to the issue.
Also, could you please share when you're planning to release the next version? Clean scan releases on our projects are currently blocked until we get a green signal from both CxOne and Black Duck.

@auvipy
Copy link
Collaborator

auvipy commented Aug 20, 2025

hopefully very soon.

@amanjolhe amanjolhe force-pushed the security/cve-2025-45768-key-length-validation branch from be2fe1b to 366ee13 Compare August 20, 2025 20:24
@auvipy auvipy requested review from jpadilla and Copilot August 21, 2025 04:58
Copilot

This comment was marked as outdated.

@amanjolhe amanjolhe requested a review from auvipy August 21, 2025 06:55
@abij
Copy link

abij commented Aug 22, 2025

Note the build fails because the "docs/index.rst" have examples which are not updated. The key length is too short.

@amanjolhe
Copy link
Author

Hello @auvipy @abij , Can You trigger workflows I made changes in doc files.

@jpadilla
Copy link
Owner

I wont be able to review this for a couple of days. Whatever we do should have the option of just a warning vs enforcement.

@amanjolhe
Copy link
Author

I wont be able to review this for a couple of days. Whatever we do should have the option of just a warning vs enforcement.

I'll implement two methods to give users the option to enable or disable enforcement as needed.

@amanjolhe
Copy link
Author

@jpadilla I made the enforcement configurable, added two new APIs, included comments in the code, and updated the documentation.

@cleder
Copy link
Contributor

cleder commented Sep 1, 2025

@amanjolhe when you merge main back into your branch, the tests should turn green

@JanEgner
Copy link

JanEgner commented Sep 3, 2025

Thanks @amanjolhe for taking this up!
Just one thing - I'm in two minds about the enforcement. While "secure by default" is obviously a Good Thing, this change will be a breaking one in some (perhaps HMAC) cases and that would be unexpected in a point release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants