From 89a54e3fe749770f919284dd206a06ba85111e29 Mon Sep 17 00:00:00 2001 From: Ojo Kayode Julius <93586369+kayodeo1@users.noreply.github.com> Date: Tue, 24 Nov 2026 17:11:33 +0100 Subject: [PATCH] [#2967] - Shiro1CryptFormat.parse() throws ArrayIndexOutOfBoundsException for malformed hashes Shiro1CryptFormat.parse() read tokens from the end of the split string without checking the token count, so truncated input such as "$shiro1$SHA-256" threw ArrayIndexOutOfBoundsException. Validate that all four tokens (algorithm, iterations, salt, digest) are present and throw IllegalArgumentException otherwise, consistent with the existing invalid-prefix and non-numeric-iterations handling. Fixes #2967 --- .../crypto/hash/format/Shiro1CryptFormat.java | 5 +++++ .../hash/format/Shiro1CryptFormatTest.groovy | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+) 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) + } + } + }