BasicCredentials equality testing timing side channel concern
Something @insaf021 noticed and raised on a few different PRs - BasicCredentials having its .equals() and .hashCode() methods do a standard string comparison on the password member is unsafe due to timing concerns.
This was one change (of a few different logical changes) raised in https://github.com/dropwizard/dropwizard/pull/11233. Because that PR is rather old (and possibly a number of the changes in that PR do not make sense to merge....I posted various questions on that PR), I have created this issue to track that this particular problem gets looked at further and not forgotten.
Is this really a security issue?
What @insaf021's PR does right now:
BasicCredentials.equals()- replacesString.equals(password)withMessageDigest.isEqual(passwordBytes, otherPasswordBytes)for constant-time byte comparison.BasicCredentials.hashCode()- drops the password from the hash so thathashCodeis derived only from the username. Stated justification: force same-username entries into the sameHashMapbucket so the constant-time equals path is always reached.
Best I can currently tell, today this does not actually fix a realistically exploitable vulnerability in the current internal usage of BasicCredentials in Dropwizard:
- It may however fix a vulnerability in custom customer flows using
BasicCredentials.equals()directly outside of aMapkey lookup flow - e.g. a customAuthenticatorthat keeps its own map of usernames ->BasicCredentialsand then calls.equals()to check the password.- It also does validly call out 'code smell' which is suspicious and probably too unsafe to keep as is. Since an exploitable security issue could crop up later.
Tracing the actual call path in CachingAuthenticator:
BasicCredentialsis used as the key of aCaffeine LoadingCache<C, Optional<P>>. On authentication, Caffeine performs aConcurrentHashMap.get()on the incomingBasicCredentials.- On a cache miss, the underlying user-supplied
Authenticatorruns - that's where the incoming password is compared against a server-side stored secret (DB row, LDAP, config, etc.).BasicCredentials.equals()is never invoked against a "known good password" on this path. - On a cache hit,
equals()compares the incomingBasicCredentialsto anotherBasicCredentialsthat was previously constructed from a successful authentication - i.e. both sides are attacker-supplied plaintext. There's no server secret being compared here.
- On a cache miss, the underlying user-supplied
- The character-oracle claim is slightly overstated:
String.hashCode()is a loop over the whole password; it does not early-exit on any character.- Yes, this DOES potentially leak timing info enough to determine the correct password's length.
- But overall I doubt this would be measurable in practice.
- The
ConcurrentHashMapbin walk short-circuits on hash mismatch before callingequals, soString.equals's early exit is not generally observable in the pre-patch code.- There IS still a chance that
hashCode()s compute to an identical value but it's going to be hard to encourage this to happen to make a remote timing measurement. Collisions are ~2^-32 per pair, and the attacker only controls their guess, not the stored password, so they can't reliably steer inputs into the same bucket.
- There IS still a chance that
The one real oracle inherent to any auth cache - "did this exact (username, password) recently authenticate?" - exists identically in both the pre- and post-patch code. It's a whole-password recognition oracle, not a per-character one. Fixing that would require an architectural change (not caching on secret material), not a comparison-function swap. So I consider that aspect out of scope for this issue.
Suggested Way Ahead - Yes, we should change this
Today, the fact that .equals() is non-constant time isn't exploitable in stock paths because of accidents of the surrounding call path (hash lookup happens before equals). But the class's contract makes equals()/hashCode() a public API — any future caller that invokes equals on BasicCredentials directly, without the hash lookup in front, would produce a real timing oracle.
Thus, I would still suggest that @insaf021 is right that we should improve this. I don't know that I would personally include this as a 'security vulnerability' reported by the project itself, though. I could see arguments either way, especially to alert users that any custom usages of it are affected below version X.
Alternative implementation
Rather than the submitted change, Claude suggested something interesting to me - what if we instead add an internally-computed 'password hash' member. It would be auto-computed during construction from the password. Then hashCode() and equals() would simply do 'normal things' using the username and passwordHash members. This might be a safer way to give typical hashCode/equals conventions without leaking secret information.
I don't know why, but to me the idea of keeping hashCode and equals() working "like normal" and just computing against a constant-length value feels 'cleaner' to me. I would be curious what others think about this.
If we're worried about the hash values being leaked and thus exposing info about the underlying passwords, the computed hash could be computed with a process-wide salt. The salt's marginal value is protecting the digest from being useful if it ever leaks without the plaintext - e.g. logs or serialized cache dumps. Since we're currently storing plaintext in the same object, this is currently of questionable value, however.
Separate but related: Proposed documentation updates
- Authenticators documentation section:
- The docs should not show
"secret".equals(credentials.getPassword())as an example - it's the exact anti-pattern the rest of this issue is about. - Add a warning block about timing side channels in credential comparison and recommend
MessageDigest.isEqual(or comparing hashes rather than plaintext).
- The docs should not show
- OAuth2 section: add a short pointer back to the Authenticators warning; the same concern applies to bearer-token comparisons.
- Fix
ExampleAuthenticator.javaindropwizard-example's line:
if (VALID_USERS.containsKey(credentials.getUsername()) && "secret".equals(credentials.getPassword())) {Source: dropwizard/dropwizard