fix: mitigate Java4Shell in Base64 decoders and containers

This commit is contained in:
qaiu committed 2026-11-20 18:29:17 +08:00
1 parent b6922b152f
commit 8c07c6e657
17 files changed
+231 -22

No files matched your search

@@ -219,12 +219,12 @@ public class AESUtils {
*/
public static String decryptByBase64AES(String encrypted, String keyString) throws IllegalBlockSizeException,
BadPaddingException, InvalidKeyException, NoSuchPaddingException, NoSuchAlgorithmException {
return decryptByAES(Base64.getDecoder().decode(encrypted), keyString);
return decryptByAES(Base64Decoder.decode(encrypted), keyString);
}
public static String decryptByBase64AES(String encrypted, Key key) throws IllegalBlockSizeException,
BadPaddingException, InvalidKeyException, NoSuchPaddingException, NoSuchAlgorithmException {
return decryptByAES(Base64.getDecoder().decode(encrypted), key);
return decryptByAES(Base64Decoder.decode(encrypted), key);
}
// ================================飞机盘Id解密========================================== //
@@ -0,0 +1,49 @@
package cn.qaiu.util;
/** Strict Base64 decoding without the HotSpot Base64 intrinsic (CVE-2026-114938). */
public final class Base64Decoder {
private static final String BASIC = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
private static final String URL_SAFE = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
private Base64Decoder() {
}
public static byte[] decode(String input) {
return decode(input, BASIC);
}
public static byte[] decodeUrl(String input) {
return decode(input, URL_SAFE);
}
private static byte[] decode(String input, String alphabet) {
int length = input.length();
int firstPadding = input.indexOf('=');
int dataLength = firstPadding < 0 ? length : firstPadding;
int remainder = dataLength % 4;
if (remainder == 1 || (firstPadding >= 0
&& (remainder == 0 || length % 4 != 0
|| length - dataLength != 4 - remainder
|| input.charAt(length - 1) != '='))) {
throw new IllegalArgumentException("Invalid Base64 padding or length");
}
byte[] decoded = new byte[dataLength / 4 * 3 + remainder * 6 / 8];
int buffer = 0;
int bits = 0;
int outputIndex = 0;
for (int i = 0; i < dataLength; i++) {
int value = alphabet.indexOf(input.charAt(i));
if (value < 0) {
throw new IllegalArgumentException("Invalid Base64 character");
}
buffer = (buffer << 6) | value;
bits += 6;
if (bits >= 8) {
bits -= 8;
decoded[outputIndex++] = (byte) (buffer >>> bits);
}
}
return decoded;
}
}
@@ -100,12 +100,12 @@ public class CommonUtils {
Exception last = null;
for (String candidate : new String[]{s, padBase64(s)}) {
try {
return new String(java.util.Base64.getUrlDecoder().decode(candidate), StandardCharsets.UTF_8);
return new String(Base64Decoder.decodeUrl(candidate), StandardCharsets.UTF_8);
} catch (Exception e) {
last = e;
}
try {
return new String(java.util.Base64.getDecoder().decode(candidate), StandardCharsets.UTF_8);
return new String(Base64Decoder.decode(candidate), StandardCharsets.UTF_8);
} catch (Exception e) {
last = e;
}