Skip to content

fix: Return HTTP 401 instead of 500 when password exceeds bcrypt maximum length#27508

Open
kiyeonjeon21 wants to merge 2 commits intoprestodb:masterfrom
kiyeonjeon21:fix/25679-password-too-long-http-500
Open

fix: Return HTTP 401 instead of 500 when password exceeds bcrypt maximum length#27508
kiyeonjeon21 wants to merge 2 commits intoprestodb:masterfrom
kiyeonjeon21:fix/25679-password-too-long-http-500

Conversation

@kiyeonjeon21
Copy link
Copy Markdown

@kiyeonjeon21 kiyeonjeon21 commented Apr 4, 2026

Summary

  • Fixes Incorrect HTTP response code 500 is returned when the password is too long #25679
  • When file-based authentication is enabled and a password exceeds bcrypt's 72-byte limit, BCrypt.verifyer().verify() throws an IllegalArgumentException that propagates as an unhandled RuntimeException, resulting in HTTP 500
  • Catch IllegalArgumentException in EncryptionUtil.doesBCryptPasswordMatch() and return false, so the normal authentication failure flow (HTTP 401) is used instead

Test plan

  • Added TestEncryptionUtil.testBCryptPasswordExceedingMaxLength() to verify bcrypt returns false for oversized passwords
  • Added TestPasswordStore.testAuthenticateWithTooLongPassword() to verify end-to-end authentication failure without exception
  • All existing tests pass (10/10)

Summary by Sourcery

Handle oversized bcrypt passwords in file-based authentication without throwing server errors.

Bug Fixes:

  • Catch IllegalArgumentException in bcrypt password verification and treat it as an authentication failure instead of an unhandled error.

Tests:

  • Add unit test to verify bcrypt password verification returns false when the password exceeds the supported length.
  • Add integration-style test to confirm authenticating with an oversized password fails cleanly without throwing exceptions.

@kiyeonjeon21 kiyeonjeon21 requested a review from a team as a code owner April 4, 2026 14:37
@linux-foundation-easycla
Copy link
Copy Markdown

linux-foundation-easycla bot commented Apr 4, 2026

CLA Signed

The committers listed above are authorized under a signed CLA.

@sourcery-ai
Copy link
Copy Markdown
Contributor

sourcery-ai bot commented Apr 4, 2026

Reviewer's Guide

Adjusts bcrypt-based password verification to treat excessively long passwords as authentication failures (401) instead of server errors (500), and adds focused unit tests for the utility and end-to-end authentication behavior.

Sequence diagram for handling too-long bcrypt passwords during authentication

sequenceDiagram
    actor User
    participant Client
    participant AuthController
    participant PasswordStore
    participant EncryptionUtil
    participant BCrypt

    User->>Client: Submit login form with too_long_password
    Client->>AuthController: POST /login (username, too_long_password)
    AuthController->>PasswordStore: authenticate(username, too_long_password)
    PasswordStore->>EncryptionUtil: doesBCryptPasswordMatch(too_long_password, storedHash)
    EncryptionUtil->>BCrypt: verify(too_long_password.toCharArray(), storedHash)
    BCrypt-->>EncryptionUtil: throw IllegalArgumentException
    EncryptionUtil-->>PasswordStore: return false
    PasswordStore-->>AuthController: authentication failed
    AuthController-->>Client: HTTP 401 Unauthorized
    Client-->>User: Show authentication failed message
Loading

Class diagram for updated bcrypt password verification flow

classDiagram
    class EncryptionUtil {
        +boolean doesBCryptPasswordMatch(String inputPassword, String hashedPassword)
    }

    class BCryptVerifier {
        +BCryptResult verify(char[] passwordChars, String hashedPassword)
    }

    class BCryptResult {
        +boolean verified
    }

    class PasswordStore {
        +boolean authenticate(String username, String password)
    }

    EncryptionUtil ..> BCryptVerifier : uses
    BCryptVerifier ..> BCryptResult : returns
    PasswordStore ..> EncryptionUtil : uses
Loading

File-Level Changes

Change Details Files
Handle bcrypt IllegalArgumentException in password verification and treat oversized passwords as failed authentication instead of propagating an error.
  • Wrap the bcrypt verify call in a try/catch for IllegalArgumentException in the password verification helper
  • Return false when bcrypt throws IllegalArgumentException so the normal authentication failure flow is used
presto-password-authenticators/src/main/java/com/facebook/presto/password/file/EncryptionUtil.java
Add unit test to ensure bcrypt password verification returns false when the password exceeds bcrypt's maximum length.
  • Import doesBCryptPasswordMatch and assertFalse for clearer test semantics
  • Create a deterministic oversized password string for the test
  • Assert that doesBCryptPasswordMatch returns false for an oversized password against a known bcrypt hash
presto-password-authenticators/src/test/java/com/facebook/presto/password/file/TestEncryptionUtil.java
Add end-to-end authentication test to verify that too-long passwords fail authentication cleanly without exceptions.
  • Instantiate a PasswordStore configured with a bcrypt-stored password
  • Use a deterministic overlong password string in the test
  • Assert that store.authenticate returns false for the user with the too-long password
presto-password-authenticators/src/test/java/com/facebook/presto/password/file/TestPasswordStore.java

Assessment against linked issues

Issue Objective Addressed Explanation
#25679 Ensure that when file-based authentication is enabled and the supplied password is too long, the server returns HTTP 401 (normal auth failure) instead of HTTP 500.
#25679 Add automated tests to cover the behavior of too-long passwords in bcrypt-based file authentication to prevent regressions.

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've left some high level feedback:

  • Catching all IllegalArgumentException instances in doesBCryptPasswordMatch() may hide unrelated problems (e.g., malformed hash); consider either validating input length/format before calling BCrypt.verifyer() or narrowing the catch to only the expected failure case and at least logging unexpected issues.
  • The bcrypt maximum length is effectively a protocol boundary here; consider extracting the limit into a named constant and using it (and perhaps trimming or rejecting inputs above it) so the behavior is clearer and easier to adjust if needed.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Catching all IllegalArgumentException instances in doesBCryptPasswordMatch() may hide unrelated problems (e.g., malformed hash); consider either validating input length/format before calling BCrypt.verifyer() or narrowing the catch to only the expected failure case and at least logging unexpected issues.
- The bcrypt maximum length is effectively a protocol boundary here; consider extracting the limit into a named constant and using it (and perhaps trimming or rejecting inputs above it) so the behavior is clearer and easier to adjust if needed.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@kiyeonjeon21
Copy link
Copy Markdown
Author

Thanks for the feedback! Updated the implementation to validate password byte length upfront (BCRYPT_MAX_PASSWORD_LENGTH = 72) instead of broadly catching IllegalArgumentException. This avoids hiding unrelated issues like malformed hashes.

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.

Incorrect HTTP response code 500 is returned when the password is too long

1 participant