Skip to content

Commit 0d5b172

Browse files
committed
resolveHasher defaults to NOOP (elastic#31723)
* Default resolveFromHash to Hasher.NOOP This changes the default behavior when resolving the hashing algorithm from unrecognised hash strings, which was introduced in elastic#31234 A hash string that doesn't start with an algorithm identifier can either be a malformed/corrupted hash or a plaintext password when Hasher.NOOP is used(against warnings). Do not make assumptions about which of the two is true for such strings and default to Hasher.NOOP. Hash verification will subsequently fail for malformed hashes. Finally, do not log the potentially malformed hash as this can very well be a plaintext password. Resolves elastic#31697 Reverts 58cf95a
1 parent 24d9d5e commit 0d5b172

File tree

3 files changed

+7
-14
lines changed

3 files changed

+7
-14
lines changed

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/support/Hasher.java

+6-9
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,8 @@ public static Hasher resolve(String name) {
438438

439439
/**
440440
* Returns a {@link Hasher} instance that can be used to verify the {@code hash} by inspecting the
441-
* hash prefix and determining the algorithm used for its generation.
441+
* hash prefix and determining the algorithm used for its generation. If no specific algorithm
442+
* prefix, can be determined {@code Hasher.NOOP} is returned.
442443
*
443444
* @param hash the char array from which the hashing algorithm is to be deduced
444445
* @return the hasher that can be used for validation
@@ -457,7 +458,8 @@ public static Hasher resolveFromHash(char[] hash) {
457458
} else if (CharArrays.charsBeginsWith(SSHA256_PREFIX, hash)) {
458459
return Hasher.SSHA256;
459460
} else {
460-
throw new IllegalArgumentException("unknown hash format for hash [" + new String(hash) + "]");
461+
// This is either a non hashed password from cache or a corrupted hash string.
462+
return Hasher.NOOP;
461463
}
462464
}
463465

@@ -471,13 +473,8 @@ public static Hasher resolveFromHash(char[] hash) {
471473
* @return true if the hash corresponds to the data, false otherwise
472474
*/
473475
public static boolean verifyHash(SecureString data, char[] hash) {
474-
try {
475-
final Hasher hasher = resolveFromHash(hash);
476-
return hasher.verify(data, hash);
477-
} catch (IllegalArgumentException e) {
478-
// The password hash format is invalid, we're unable to verify password
479-
return false;
480-
}
476+
final Hasher hasher = resolveFromHash(hash);
477+
return hasher.verify(data, hash);
481478
}
482479

483480
private static char[] getPbkdf2Hash(SecureString data, int cost) {

x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/file/FileRealmTests.java

-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ public void testAuthenticateCaching() throws Exception {
8989
.put("cache.hash_algo", Hasher.values()[randomIntBetween(0, Hasher.values().length - 1)].name().toLowerCase(Locale.ROOT)).build();
9090
RealmConfig config = new RealmConfig("file-test", settings, globalSettings, TestEnvironment.newEnvironment(globalSettings),
9191
threadContext);
92-
9392
when(userPasswdStore.verifyPassword(eq("user1"), eq(new SecureString("test123")), any(Supplier.class)))
9493
.thenAnswer(VERIFY_PASSWORD_ANSWER);
9594
when(userRolesStore.roles("user1")).thenReturn(new String[]{"role1", "role2"});

x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/support/HasherTests.java

+1-4
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,7 @@ public void testResolveFromHash() {
128128
assertThat(Hasher.resolveFromHash(
129129
"{PBKDF2}1000000$UuyhtjDEzWmE2wyY80akZKPWWpy2r2X50so41YML82U=$WFasYLelqbjQwt3EqFlUcwHiC38EZC45Iu/Iz0xL1GQ=".toCharArray()),
130130
sameInstance(Hasher.PBKDF2_1000000));
131-
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> {
132-
Hasher.resolveFromHash("{GBGN}cGR8S2vr3FuFuOpQitR".toCharArray());
133-
});
134-
assertThat(e.getMessage(), containsString("unknown hash format for hash"));
131+
assertThat(Hasher.resolveFromHash("notavalidhashformat".toCharArray()), sameInstance(Hasher.NOOP));
135132
}
136133

137134
private static void testHasherSelfGenerated(Hasher hasher) {

0 commit comments

Comments
 (0)