Reject empty HMAC secrets when constructing an algorithm
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- Mức phù hợp với người mới
- 88/100
Hướng nghiên cứu
Bắt đầu với hàm khởi tạo trong lib/src/main/java/com/auth0/jwt/algorithms/HMACAlgorithm.java và tài liệu về phương thức HMAC trong lib/src/main/java/com/auth0/jwt/algorithms/Algorithm.java. Chạy các bài kiểm thử Gradle liên quan, sau đó bổ sung kiểm thử cho các secret String và byte[] rỗng trên HMAC256/384/512. Công việc được xem là hoàn tất khi quá trình khởi tạo từ chối các secret có độ dài bằng không bằng ngoại lệ được chỉ định và tài liệu phản ánh điều này.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Checklist
- I have looked into the Readme and Examples, and have not found a suitable solution or answer.
- I have looked into the API documentation and have not found a suitable solution or answer.
- I have searched the issues and have not found a suitable solution or answer.
- I have searched the Auth0 Community forums and have not found a suitable solution or answer.
- I agree to the terms within the Auth0 Code of Conduct.
Describe the problem you'd like to have solved
Location (auth0/java-jwt @ default branch 1d589a4):
lib/src/main/java/com/auth0/jwt/algorithms/HMACAlgorithm.java:24-32(constructor)lib/src/main/java/com/auth0/jwt/algorithms/HMACAlgorithm.java:43-49(getSecretBytes)lib/src/main/java/com/auth0/jwt/algorithms/HMACAlgorithm.java:50-63(verify)lib/src/main/java/com/auth0/jwt/algorithms/CryptoHelper.java:234-236,CryptoHelper.java:254-256(new SecretKeySpec(...))lib/src/main/java/com/auth0/jwt/algorithms/Algorithm.java:258-268(javadoc)
if (secretBytes == null) {
throw new IllegalArgumentException("The Secret cannot be null");
}
this.secret = Arrays.copyOf(secretBytes, secretBytes.length);
...
static byte[] getSecretBytes(String secret) throws IllegalArgumentException {
if (secret == null) {
throw new IllegalArgumentException("The Secret cannot be null");
}
return secret.getBytes(StandardCharsets.UTF_8);
}
final Mac mac = Mac.getInstance(algorithm);
mac.init(new SecretKeySpec(secretBytes, algorithm));
Problem
Algorithm.HMAC256/384/512 reject a null secret when the algorithm is created, but accept an empty one ("" or new byte[0]): the constructor and getSecretBytes only check for null.
An empty secret can never be used. The key is later passed to new SecretKeySpec(secretBytes, algorithm), and the JDK documents that SecretKeySpec throws IllegalArgumentException "if algorithm is null or key is null or empty". The failure is therefore only postponed:
- when signing, a JCA
IllegalArgumentExceptioncomes fromHMACAlgorithm.sign(which catches onlyNoSuchAlgorithmException/InvalidKeyException); - when verifying, it is wrapped into a
SignatureVerificationException, which looks like an invalid token rather than a configuration error.
A common way to get here is reading the secret from an unset or empty environment variable/config value at startup. The application starts normally and only fails on the first sign/verify request, far from where the secret was configured.
The current API contract matches this behavior. The javadoc only says @throws IllegalArgumentException if the provided Secret is null. For that reason, this is a diagnostic improvement request, not a bug or security report.
Reproduction
Constructing the algorithm with an empty secret succeeds:
Algorithm.HMAC256(""); // no exception
Algorithm.HMAC256(new byte[0]); // no exception (same for HMAC384/HMAC512)
Environment: master at 1d589a4c1c3c, version 4.6.2, OpenJDK 11.
Describe the ideal solution
Already implemented in jwt/ruby-jwt
ruby-jwt is a separate JWT implementation, so this is a precedent rather than an obligation. PR: https://github.com/jwt/ruby-jwt/pull/530 — "Bugfix - Raise Descriptive Error for hmac_secret empty and OpenSSL 3.0 Issue" (merged 2022-10-24T15:22:47Z, merge commit 538c3c65b01). It replaced the cryptic error raised by the crypto backend for an empty hmac_secret (OpenSSL::HMACError (EVP_PKEY_new_mac_key: malloc failure)) with a descriptive JWT::DecodeError naming the empty secret. ruby-jwt does this at signing time inside JWT::Algos::Hmac.sign; the proposal below moves the same kind of check to construction time, where java-jwt already validates null.
How ruby-jwt did it:
--- a/lib/jwt/algos/hmac.rb
+++ b/lib/jwt/algos/hmac.rb
@@ -13,7 +13,15 @@ def sign(algorithm, msg, key)
if authenticator && padded_key
authenticator.auth(padded_key, msg.encode('binary'))
else
- OpenSSL::HMAC.digest(OpenSSL::Digest.new(algorithm.sub('HS', 'sha')), key, msg)
+ begin
+ OpenSSL::HMAC.digest(OpenSSL::Digest.new(algorithm.sub('HS', 'sha')), key, msg)
+ rescue OpenSSL::HMACError => e
+ if key == '' && e.message == 'EVP_PKEY_new_mac_key: malloc failure'
+ raise JWT::DecodeError.new('OpenSSL 3.0 does not support nil or empty hmac_secret')
+ end
+
+ raise e
+ end
end
end
Suggested fix
Make the existing null check in the HMACAlgorithm constructor also reject zero-length secrets (both the String and byte[] overloads of HMAC256/384/512 go through it) with IllegalArgumentException("The Secret cannot be empty"), and update the @throws javadoc:
diff --git a/lib/src/main/java/com/auth0/jwt/algorithms/Algorithm.java b/lib/src/main/java/com/auth0/jwt/algorithms/Algorithm.java
--- a/lib/src/main/java/com/auth0/jwt/algorithms/Algorithm.java
+++ b/lib/src/main/java/com/auth0/jwt/algorithms/Algorithm.java
@@ -261,7 +261,7 @@ public abstract class Algorithm {
* @param secret the secret bytes to use in the verify or signing instance.
* Ensure the length of the secret is at least 256 bit long
* @return a valid HMAC256 Algorithm.
- * @throws IllegalArgumentException if the provided Secret is null.
+ * @throws IllegalArgumentException if the provided Secret is null or empty.
*/
public static Algorithm HMAC256(String secret) throws IllegalArgumentException {
return new HMACAlgorithm("HS256", "HmacSHA256", secret);
@@ -273,7 +273,7 @@ public abstract class Algorithm {
* @param secret the secret bytes to use in the verify or signing instance.
* Ensure the length of the secret is at least 256 bit long
* @return a valid HMAC256 Algorithm.
- * @throws IllegalArgumentException if the provided Secret is null.
+ * @throws IllegalArgumentException if the provided Secret is null or empty.
*/
public static Algorithm HMAC256(byte[] secret) throws IllegalArgumentException {
return new HMACAlgorithm("HS256", "HmacSHA256", secret);
@@ -285,7 +285,7 @@ public abstract class Algorithm {
* @param secret the secret bytes to use in the verify or signing instance.
* Ensure the length of the secret is at least 384 bit long
* @return a valid HMAC384 Algorithm.
- * @throws IllegalArgumentException if the provided Secret is null.
+ * @throws IllegalArgumentException if the provided Secret is null or empty.
*/
public static Algorithm HMAC384(String secret) throws IllegalArgumentException {
return new HMACAlgorithm("HS384", "HmacSHA384", secret);
@@ -297,7 +297,7 @@ public abstract class Algorithm {
* @param secret the secret bytes to use in the verify or signing instance.
* Ensure the length of the secret is at least 384 bit long
* @return a valid HMAC384 Algorithm.
- * @throws IllegalArgumentException if the provided Secret is null.
+ * @throws IllegalArgumentException if the provided Secret is null or empty.
*/
public static Algorithm HMAC384(byte[] secret) throws IllegalArgumentException {
return new HMACAlgorithm("HS384", "HmacSHA384", secret);
@@ -309,7 +309,7 @@ public abstract class Algorithm {
* @param secret the secret bytes to use in the verify or signing instance.
* Ensure the length of the secret is at least 512 bit long
* @return a valid HMAC512 Algorithm.
- * @throws IllegalArgumentException if the provided Secret is null.
+ * @throws IllegalArgumentException if the provided Secret is null or empty.
*/
public static Algorithm HMAC512(String secret) throws IllegalArgumentException {
return new HMACAlgorithm("HS512", "HmacSHA512", secret);
@@ -321,7 +321,7 @@ public abstract class Algorithm {
* @param secret the secret bytes to use in the verify or signing instance.
* Ensure the length of the secret is at least 512 bit long
* @return a valid HMAC512 Algorithm.
- * @throws IllegalArgumentException if the provided Secret is null.
+ * @throws IllegalArgumentException if the provided Secret is null or empty.
*/
public static Algorithm HMAC512(byte[] secret) throws IllegalArgumentException {
return new HMACAlgorithm("HS512", "HmacSHA512", secret);
diff --git a/lib/src/main/java/com/auth0/jwt/algorithms/HMACAlgorithm.java b/lib/src/main/java/com/auth0/jwt/algorithms/HMACAlgorithm.java
--- a/lib/src/main/java/com/auth0/jwt/algorithms/HMACAlgorithm.java
+++ b/lib/src/main/java/com/auth0/jwt/algorithms/HMACAlgorithm.java
@@ -27,6 +27,9 @@ class HMACAlgorithm extends Algorithm {
if (secretBytes == null) {
throw new IllegalArgumentException("The Secret cannot be null");
}
+ if (secretBytes.length == 0) {
+ throw new IllegalArgumentException("The Secret cannot be empty");
+ }
this.secret = Arrays.copyOf(secretBytes, secretBytes.length);
this.crypto = crypto;
}
Scope:
- Only zero-length secrets are affected. I am not asking for a minimum key length. I've read #444, where enforcing JWA key sizes was declined because users should be free to choose secret length, and this proposal keeps that policy: any non-empty secret is still accepted.
- This should not break working setups, because an empty secret already fails on its first use. The change only moves the error to construction time and gives it a clear message.
Verified on 1d589a4 (OpenJDK 11.0.32.1, Gradle 6.9.2 wrapper): a test asserting that Algorithm.HMAC256/384/512("") and (new byte[0]) throw IllegalArgumentException with that message fails without this patch and passes with it; no other test results change.
I can submit a PR for this.
Alternatives and current workarounds
Applications can check secret.isEmpty() themselves before calling Algorithm.HMAC256(...). The point of this request is to have the library report the problem where the secret is supplied.
Additional context
Related: checked #444 and README PRs #447/#477 (minimum key-length recommendations, not empty secrets); #674 (empty audience claim) and #782 (NumericDate range) are unrelated.
- Ngôn ngữ chính
- Java
- Star
- 6.2k
- Fork
- 950
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của auth0/java-jwt
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 66/100
-
Date claims before the Unix epoch are rounded toward zeroCó thể đã có người làm @hossam1244 đã nhận 47 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
-
bug
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 42/100
-
Out-of-range NumericDate claims can throw unchecked DateTimeException during verificationCó thể đã có người làm @ANSHUL-REAL đã nhận 118 ngày trước. Đang mởbug
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 72/100
-
Upgrade Jackson 2 => Jackson 3Có thể đã có người làm Có pull request liên kết đang mở hoặc đã được merge. Đang mởfeature request
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 45/100
Tất cả issue của auth0/java-jwt
Issue tương tự
-
enhancement
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
it-at-m/Wahllokalsystem#3846 ·
Maintainer thường phản hồi trong vòng 2 ngày
-
Area/Connector module/solace Priority/Low Type/Bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 77/100
ballerina-platform/ballerina-library#9849 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 65/100
Vakore/ZappierGames#327 ·
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Radix number block generates NaN after switching a negative or decimal number to binary, octal or hexCó thể đã có người làm @matthewalberto đã nhận hôm nay. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 64/100
mit-cml/appinventor-sources#4246 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày