Skip to content

Commit ebd7ab7

Browse files
committed
🐛 constant-time HMAC security fix
1 parent 5615c0a commit ebd7ab7

1 file changed

Lines changed: 11 additions & 2 deletions

File tree

src/main/java/com/mindee/parsing/BaseLocalResponse.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.nio.file.Files;
1010
import java.nio.file.Path;
1111
import java.security.InvalidKeyException;
12+
import java.security.MessageDigest;
1213
import java.security.NoSuchAlgorithmException;
1314
import java.util.stream.Collectors;
1415
import java.util.stream.Stream;
@@ -99,10 +100,18 @@ public String getHmacSignature(String secretKey) {
99100
* Verify that the payload's signature matches the one received from the server.
100101
*
101102
* @param secretKey Your secret key from the Mindee platform.
102-
* @param signature The signature from the "X-Mindee-Hmac-Signature" HTTP header.
103+
* @param signature The signature from the "X-Signature" HTTP header.
103104
* @return true if the signatures match.
104105
*/
105106
public boolean isValidHmacSignature(String secretKey, String signature) {
106-
return signature.equals(getHmacSignature(secretKey));
107+
if (signature == null || secretKey == null) {
108+
return false;
109+
}
110+
byte[] expectedBytes = getHmacSignature(secretKey).getBytes(StandardCharsets.UTF_8);
111+
byte[] actualBytes = signature
112+
.toLowerCase(java.util.Locale.ROOT)
113+
.getBytes(StandardCharsets.UTF_8);
114+
115+
return MessageDigest.isEqual(expectedBytes, actualBytes);
107116
}
108117
}

0 commit comments

Comments
 (0)