fix: Return HTTP 401 instead of 500 when password exceeds bcrypt maximum length#27508
Open
kiyeonjeon21 wants to merge 2 commits intoprestodb:masterfrom
Open
fix: Return HTTP 401 instead of 500 when password exceeds bcrypt maximum length#27508kiyeonjeon21 wants to merge 2 commits intoprestodb:masterfrom
kiyeonjeon21 wants to merge 2 commits intoprestodb:masterfrom
Conversation
Contributor
Reviewer's GuideAdjusts 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 authenticationsequenceDiagram
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
Class diagram for updated bcrypt password verification flowclassDiagram
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
File-Level Changes
Assessment against linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Contributor
There was a problem hiding this comment.
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.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
…atching IllegalArgumentException
Author
|
Thanks for the feedback! Updated the implementation to validate password byte length upfront ( |
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
BCrypt.verifyer().verify()throws anIllegalArgumentExceptionthat propagates as an unhandledRuntimeException, resulting in HTTP 500IllegalArgumentExceptioninEncryptionUtil.doesBCryptPasswordMatch()and returnfalse, so the normal authentication failure flow (HTTP 401) is used insteadTest plan
TestEncryptionUtil.testBCryptPasswordExceedingMaxLength()to verify bcrypt returnsfalsefor oversized passwordsTestPasswordStore.testAuthenticateWithTooLongPassword()to verify end-to-end authentication failure without exceptionSummary by Sourcery
Handle oversized bcrypt passwords in file-based authentication without throwing server errors.
Bug Fixes:
Tests: