Skip to content

Commit e4d2072

Browse files
committed
chore(webauthn): in OptionsValidator, use java.util.Base64 when applicable
1 parent 5afa6e2 commit e4d2072

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

src/main/java/io/supertokens/webauthn/validator/OptionsValidator.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import java.net.MalformedURLException;
2424
import java.net.URL;
25+
import java.util.Base64;
2526
import java.util.List;
2627

2728
public class OptionsValidator {
@@ -49,14 +50,21 @@ private static void validateOrigin(String origin, String rpId) throws InvalidWeb
4950
throw new InvalidWebauthNOptionsException("Android origin must contain a valid base64 hash");
5051
}
5152

52-
// Accept URL-safe base64 (A-Za-z0-9-_ only)
53-
if (!hash.matches("^[A-Za-z0-9\\-_]+$")) {
54-
throw new InvalidWebauthNOptionsException("Android origin hash must be valid URL-safe base64");
53+
// Validate base64 characters first before checking length
54+
try {
55+
Base64.getUrlDecoder().decode(hash);
56+
} catch (IllegalArgumentException error) {
57+
throw new InvalidWebauthNOptionsException("Android origin hash must be valid URL-safe base64 (no padding)");
5558
}
5659

57-
// Validate length: SHA256 is 32 bytes, base64-urlsafe encoding is 43 chars
60+
// SHA-256 fingerprint in base64url (no padding) is always 43 characters and decodes to 32 bytes
5861
if (hash.length() != 43) {
59-
throw new InvalidWebauthNOptionsException("Android origin hash must be 43 characters (base64 of signing certificate's SHA 256 fingerprint)");
62+
throw new InvalidWebauthNOptionsException("Android origin hash must be 43 characters (base64url SHA-256)");
63+
}
64+
65+
// Verify it decodes to exactly 32 bytes (SHA-256)
66+
if (Base64.getUrlDecoder().decode(hash).length != 32) {
67+
throw new InvalidWebauthNOptionsException("Android origin hash must decode to 32 bytes (SHA-256)");
6068
}
6169

6270
return;

0 commit comments

Comments
 (0)