diff --git a/crypto/hash/src/main/java/org/apache/shiro/crypto/hash/format/Shiro1CryptFormat.java b/crypto/hash/src/main/java/org/apache/shiro/crypto/hash/format/Shiro1CryptFormat.java index b91ce3cf9d..5a9e46777b 100644 --- a/crypto/hash/src/main/java/org/apache/shiro/crypto/hash/format/Shiro1CryptFormat.java +++ b/crypto/hash/src/main/java/org/apache/shiro/crypto/hash/format/Shiro1CryptFormat.java @@ -144,6 +144,11 @@ public Hash parse(final String formatted) { String suffix = formatted.substring(MCF_PREFIX.length()); String[] parts = suffix.split("\\$"); + //a valid shiro1 hash always has 4 tokens: algorithm, iterations, salt (may be empty) and digest: + if (parts.length < 4) { + throw new IllegalArgumentException("The argument is not a valid '" + ID + "' formatted hash."); + } + final String algorithmName = parts[0]; if (!new SimpleHashProvider().getImplementedAlgorithms().contains(algorithmName)) { throw new UnsupportedOperationException("Algorithm " + algorithmName + " is not supported in shiro1 format."); diff --git a/crypto/hash/src/test/groovy/org/apache/shiro/crypto/hash/format/Shiro1CryptFormatTest.groovy b/crypto/hash/src/test/groovy/org/apache/shiro/crypto/hash/format/Shiro1CryptFormatTest.groovy index 7ac441e3a6..8e2a7ca8a2 100644 --- a/crypto/hash/src/test/groovy/org/apache/shiro/crypto/hash/format/Shiro1CryptFormatTest.groovy +++ b/crypto/hash/src/test/groovy/org/apache/shiro/crypto/hash/format/Shiro1CryptFormatTest.groovy @@ -22,6 +22,7 @@ import org.apache.shiro.crypto.SecureRandomNumberGenerator import org.apache.shiro.crypto.hash.SimpleHash import org.junit.jupiter.api.Test +import static org.assertj.core.api.Assertions.assertThatThrownBy import static org.junit.jupiter.api.Assertions.* /** @@ -167,4 +168,22 @@ class Shiro1CryptFormatTest { } } + @Test + void testParseWithMissingTokens() { + def format = new Shiro1CryptFormat() + def malformed = [ + '$shiro1$SHA-256', + '$shiro1$SHA-256$', + '$shiro1$SHA-256$abc', + '$shiro1$SHA-256$1$abc', + '$shiro1$SHA-256$1$$' + ] + + malformed.each { formatted -> + assertThatThrownBy { format.parse(formatted) } + .describedAs("parsing '%s'", formatted) + .isInstanceOf(IllegalArgumentException.class) + } + } + }