diff --git a/Dockerfile b/Dockerfile index be3c89b..19f749b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,5 @@ -FROM eclipse-temurin:17-jre +ARG JAVA_RUNTIME_IMAGE=eclipse-temurin:17-jre +FROM ${JAVA_RUNTIME_IMAGE} WORKDIR /app diff --git a/core/src/main/java/cn/qaiu/vx/core/verticle/HttpProxyVerticle.java b/core/src/main/java/cn/qaiu/vx/core/verticle/HttpProxyVerticle.java index 3ddff9a..930c433 100644 --- a/core/src/main/java/cn/qaiu/vx/core/verticle/HttpProxyVerticle.java +++ b/core/src/main/java/cn/qaiu/vx/core/verticle/HttpProxyVerticle.java @@ -13,7 +13,10 @@ import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.io.ByteArrayInputStream; +import java.io.IOException; import java.net.URI; +import java.nio.charset.StandardCharsets; import java.util.Base64; import static cn.qaiu.vx.core.util.ConfigConstant.GLOBAL_CONFIG; @@ -25,6 +28,20 @@ import static cn.qaiu.vx.core.util.ConfigConstant.LOCAL; public class HttpProxyVerticle extends AbstractVerticle { private static final Logger LOGGER = LoggerFactory.getLogger(HttpProxyVerticle.class); + static byte[] decodeProxyAuthorization(String encoded) { + var source = new ByteArrayInputStream(encoded.getBytes(StandardCharsets.ISO_8859_1)); + // The streaming decoder uses scalar Java code and does not call the bulk intrinsic. + try (var decoder = Base64.getDecoder().wrap(source)) { + byte[] decoded = decoder.readAllBytes(); + if (source.available() != 0) { + throw new IllegalArgumentException("Unexpected data after Base64 padding"); + } + return decoded; + } catch (IOException e) { + throw new IllegalArgumentException("Invalid Base64 proxy credentials", e); + } + } + private HttpClient httpClient; private NetClient netClient; private HttpServer httpServer; @@ -164,7 +181,7 @@ public class HttpProxyVerticle extends AbstractVerticle { } String[] split; try { - split = new String(Base64.getDecoder().decode(s.replace("Basic ", ""))).split(":"); + split = new String(decodeProxyAuthorization(s.replace("Basic ", ""))).split(":"); } catch (IllegalArgumentException e) { LOGGER.warn("Proxy-Authorization header is not valid Base64"); failClientResponse(clientRequest.response(), 403, null); diff --git a/core/src/test/java/cn/qaiu/vx/core/verticle/HttpProxyBase64Test.java b/core/src/test/java/cn/qaiu/vx/core/verticle/HttpProxyBase64Test.java new file mode 100644 index 0000000..db72330 --- /dev/null +++ b/core/src/test/java/cn/qaiu/vx/core/verticle/HttpProxyBase64Test.java @@ -0,0 +1,30 @@ +package cn.qaiu.vx.core.verticle; + +import org.junit.Test; + +import java.util.Base64; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.fail; + +public class HttpProxyBase64Test { + @Test + public void matchesJdkBasicDecoder() { + for (String value : new String[]{"", "Zg", "Zg==", "Zm8=", "dXNlcjpwYXNz", "+/8=", "ABCD", + "AB==", "Z", "Zg=", "Zm8==", "Zg==suffix", "Zg==\n", "-_8=", "AA\u2603", "===="}) { + byte[] expected; + try { + expected = Base64.getDecoder().decode(value); + } catch (IllegalArgumentException expectedFailure) { + try { + HttpProxyVerticle.decodeProxyAuthorization(value); + } catch (IllegalArgumentException actualFailure) { + continue; + } + fail("Accepted invalid proxy credentials: " + value); + return; + } + assertArrayEquals(expected, HttpProxyVerticle.decodeProxyAuthorization(value)); + } + } +} diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh index 7742770..eb79ab3 100644 --- a/docker-entrypoint.sh +++ b/docker-entrypoint.sh @@ -10,4 +10,5 @@ DEFAULT_JVM_OPTS="-Xmx${JVM_XMX:-512M} -Xss${JVM_XSS:-512k} -XX:MaxDirectMemoryS if [ -n "${JVM_MAX_METASPACE:-}" ]; then DEFAULT_JVM_OPTS="$DEFAULT_JVM_OPTS -XX:MaxMetaspaceSize=$JVM_MAX_METASPACE" fi -exec java ${DEFAULT_JVM_OPTS} ${JVM_OPTS} -Duser.timezone=${TZ:-Asia/Shanghai} -jar /app/netdisk-fast-download.jar +# Keep this after JVM_OPTS to disable the intrinsic even with an older base image. +exec java ${DEFAULT_JVM_OPTS} ${JVM_OPTS} -XX:-UseBASE64Intrinsics -Duser.timezone=${TZ:-Asia/Shanghai} -jar /app/netdisk-fast-download.jar diff --git a/parser/src/main/java/cn/qaiu/parser/impl/Ye2Tool.java b/parser/src/main/java/cn/qaiu/parser/impl/Ye2Tool.java index 3f99697..329e737 100644 --- a/parser/src/main/java/cn/qaiu/parser/impl/Ye2Tool.java +++ b/parser/src/main/java/cn/qaiu/parser/impl/Ye2Tool.java @@ -6,6 +6,7 @@ import cn.qaiu.parser.PanBase; import cn.qaiu.parser.PasswordLoginRisk; import cn.qaiu.parser.TokenCache; import cn.qaiu.util.CommonUtils; +import cn.qaiu.util.Base64Decoder; import cn.qaiu.util.FileSizeConverter; import cn.qaiu.util.YeShareHostUtil; import io.vertx.core.Future; @@ -23,7 +24,6 @@ import java.net.MalformedURLException; import java.time.OffsetDateTime; import java.time.format.DateTimeFormatter; import java.util.ArrayList; -import java.util.Base64; import java.util.List; import java.util.Map; import java.util.Random; @@ -514,7 +514,7 @@ public class Ye2Tool extends PanBase { return; } - byte[] decodeByte = Base64.getDecoder().decode(params); + byte[] decodeByte = Base64Decoder.decode(params); String downUrl2 = new String(decodeByte); clientNoRedirects.getAbs(downUrl2).putHeaders(header).send().onSuccess(res3 -> { diff --git a/parser/src/main/java/cn/qaiu/parser/impl/YeTool.java b/parser/src/main/java/cn/qaiu/parser/impl/YeTool.java index 4757d4e..5f85fa6 100644 --- a/parser/src/main/java/cn/qaiu/parser/impl/YeTool.java +++ b/parser/src/main/java/cn/qaiu/parser/impl/YeTool.java @@ -4,6 +4,7 @@ import cn.qaiu.entity.FileInfo; import cn.qaiu.entity.ShareLinkInfo; import cn.qaiu.parser.PanBase; import cn.qaiu.util.CommonUtils; +import cn.qaiu.util.Base64Decoder; import cn.qaiu.util.FileSizeConverter; import cn.qaiu.util.JsExecUtils; import io.vertx.core.Future; @@ -166,7 +167,7 @@ public class YeTool extends PanBase { try { Map urlParams = CommonUtils.getURLParams(downURL); String params = urlParams.get("params"); - byte[] decodeByte = Base64.getDecoder().decode(params); + byte[] decodeByte = Base64Decoder.decode(params); String downUrl2 = new String(decodeByte); // 获取直链 diff --git a/parser/src/main/java/cn/qaiu/util/AESUtils.java b/parser/src/main/java/cn/qaiu/util/AESUtils.java index a3a42b0..9452db4 100644 --- a/parser/src/main/java/cn/qaiu/util/AESUtils.java +++ b/parser/src/main/java/cn/qaiu/util/AESUtils.java @@ -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解密========================================== // diff --git a/parser/src/main/java/cn/qaiu/util/Base64Decoder.java b/parser/src/main/java/cn/qaiu/util/Base64Decoder.java new file mode 100644 index 0000000..a516a0c --- /dev/null +++ b/parser/src/main/java/cn/qaiu/util/Base64Decoder.java @@ -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; + } +} diff --git a/parser/src/main/java/cn/qaiu/util/CommonUtils.java b/parser/src/main/java/cn/qaiu/util/CommonUtils.java index afc4ec9..1e026e9 100644 --- a/parser/src/main/java/cn/qaiu/util/CommonUtils.java +++ b/parser/src/main/java/cn/qaiu/util/CommonUtils.java @@ -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; } diff --git a/parser/src/test/java/cn/qaiu/util/Base64DecoderTest.java b/parser/src/test/java/cn/qaiu/util/Base64DecoderTest.java new file mode 100644 index 0000000..6a8978e --- /dev/null +++ b/parser/src/test/java/cn/qaiu/util/Base64DecoderTest.java @@ -0,0 +1,65 @@ +package cn.qaiu.util; + +import org.junit.Test; + +import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; +import java.util.Base64; +import java.util.Random; +import java.util.function.Function; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +public class Base64DecoderTest { + @Test + public void matchesJdkForStandardAndUrlSafeEncodings() { + Random random = new Random(114938); + for (int length = 0; length <= 512; length++) { + byte[] value = new byte[length]; + random.nextBytes(value); + assertArrayEquals(value, Base64Decoder.decode(Base64.getEncoder().encodeToString(value))); + assertArrayEquals(value, Base64Decoder.decode(Base64.getEncoder().withoutPadding().encodeToString(value))); + assertArrayEquals(value, Base64Decoder.decodeUrl(Base64.getUrlEncoder().encodeToString(value))); + assertArrayEquals(value, Base64Decoder.decodeUrl(Base64.getUrlEncoder().withoutPadding().encodeToString(value))); + } + } + + @Test + public void matchesJdkForPaddingAlphabetAndInvalidCharacters() { + for (String value : new String[]{"", "A", "AA", "AB==", "ABC=", "ABCD", "AA=", "AAA==", + "AA==AA", "AA==\n", " AA", "A A", "AA\u00e9", "AA\u2603", "+/8=", "-_8=", "+_8=", "===="}) { + assertCompatible(value, Base64.getDecoder(), Base64Decoder::decode); + assertCompatible(value, Base64.getUrlDecoder(), Base64Decoder::decodeUrl); + } + } + + @Test + public void preservesLegacyDownloadLinkFormats() { + String value = "网盘下载😀"; + String standard = Base64.getEncoder().encodeToString(value.getBytes(StandardCharsets.UTF_8)); + String escaped = URLEncoder.encode(standard, StandardCharsets.UTF_8); + assertEquals(value, CommonUtils.urlBase64Decode(CommonUtils.urlBase64Encode(value))); + assertEquals(value, CommonUtils.urlBase64Decode(standard)); + assertEquals(value, CommonUtils.urlBase64Decode(escaped)); + assertEquals(value, CommonUtils.urlBase64Decode(URLEncoder.encode(escaped, StandardCharsets.UTF_8))); + assertEquals("f", CommonUtils.urlBase64Decode("Zg=")); + } + + private static void assertCompatible(String value, Base64.Decoder jdk, Function replacement) { + byte[] expected; + try { + expected = jdk.decode(value); + } catch (IllegalArgumentException expectedFailure) { + try { + replacement.apply(value); + } catch (IllegalArgumentException actualFailure) { + return; + } + fail("Accepted invalid Base64 input: " + value); + return; + } + assertArrayEquals(expected, replacement.apply(value)); + } +} diff --git a/pom.xml b/pom.xml index 3c11261..97ab7e6 100644 --- a/pom.xml +++ b/pom.xml @@ -36,6 +36,7 @@ 2.18.9 1.5.34 + true 4.13.2 @@ -97,7 +98,7 @@ maven-surefire-plugin 2.22.2 - true + ${skipTests} diff --git a/web-service/src/main/java/cn/qaiu/lz/common/util/AuthParamCodec.java b/web-service/src/main/java/cn/qaiu/lz/common/util/AuthParamCodec.java index 0a9c1f2..599fb28 100644 --- a/web-service/src/main/java/cn/qaiu/lz/common/util/AuthParamCodec.java +++ b/web-service/src/main/java/cn/qaiu/lz/common/util/AuthParamCodec.java @@ -2,6 +2,7 @@ package cn.qaiu.lz.common.util; import cn.qaiu.lz.web.model.AuthParam; import cn.qaiu.util.AESUtils; +import cn.qaiu.util.Base64Decoder; import cn.qaiu.vx.core.util.SharedDataUtil; import io.vertx.core.json.JsonObject; import lombok.extern.slf4j.Slf4j; @@ -9,7 +10,6 @@ import org.apache.commons.lang3.StringUtils; import java.net.URLDecoder; import java.nio.charset.StandardCharsets; -import java.util.Base64; /** * 认证参数编解码工具类 @@ -88,7 +88,7 @@ public class AuthParamCodec { log.debug("认证参数规范化结果: {}", normalized); // Step 2: Base64解码 - byte[] base64Decoded = Base64.getDecoder().decode(normalized); + byte[] base64Decoded = Base64Decoder.decode(normalized); log.debug("Base64解码成功,长度: {}", base64Decoded.length); // Step 3: AES解密 diff --git a/web-service/src/main/java/cn/qaiu/lz/common/util/JwtUtil.java b/web-service/src/main/java/cn/qaiu/lz/common/util/JwtUtil.java index 0da695f..732c718 100644 --- a/web-service/src/main/java/cn/qaiu/lz/common/util/JwtUtil.java +++ b/web-service/src/main/java/cn/qaiu/lz/common/util/JwtUtil.java @@ -1,5 +1,6 @@ package cn.qaiu.lz.common.util; +import cn.qaiu.util.Base64Decoder; import cn.qaiu.lz.web.model.SysUser; import io.vertx.core.json.JsonObject; @@ -102,7 +103,7 @@ public class JwtUtil { } // 验证过期时间 - String payload = new String(Base64.getUrlDecoder().decode(encodedPayload), StandardCharsets.UTF_8); + String payload = new String(Base64Decoder.decodeUrl(encodedPayload), StandardCharsets.UTF_8); JsonObject payloadJson = new JsonObject(payload); long expTime = payloadJson.getLong("exp", 0L); @@ -125,7 +126,7 @@ public class JwtUtil { } // Base64解码 - String payload = new String(Base64.getUrlDecoder().decode(parts[1]), StandardCharsets.UTF_8); + String payload = new String(Base64Decoder.decodeUrl(parts[1]), StandardCharsets.UTF_8); JsonObject jsonObject = new JsonObject(payload); return jsonObject.getString("id"); } @@ -143,7 +144,7 @@ public class JwtUtil { } // Base64解码 - String payload = new String(Base64.getUrlDecoder().decode(parts[1]), StandardCharsets.UTF_8); + String payload = new String(Base64Decoder.decodeUrl(parts[1]), StandardCharsets.UTF_8); JsonObject jsonObject = new JsonObject(payload); return jsonObject.getString("username"); } @@ -161,7 +162,7 @@ public class JwtUtil { } // Base64解码 - String payload = new String(Base64.getUrlDecoder().decode(parts[1]), StandardCharsets.UTF_8); + String payload = new String(Base64Decoder.decodeUrl(parts[1]), StandardCharsets.UTF_8); JsonObject jsonObject = new JsonObject(payload); return jsonObject.getString("role"); } diff --git a/web-service/src/main/java/cn/qaiu/lz/common/util/PasswordUtil.java b/web-service/src/main/java/cn/qaiu/lz/common/util/PasswordUtil.java index 62c466f..2496cdf 100644 --- a/web-service/src/main/java/cn/qaiu/lz/common/util/PasswordUtil.java +++ b/web-service/src/main/java/cn/qaiu/lz/common/util/PasswordUtil.java @@ -1,5 +1,6 @@ package cn.qaiu.lz.common.util; +import cn.qaiu.util.Base64Decoder; import java.nio.charset.StandardCharsets; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; @@ -72,7 +73,7 @@ public class PasswordUtil { String hashBase64 = parts[1]; // 解码盐 - byte[] salt = Base64.getDecoder().decode(saltBase64); + byte[] salt = Base64Decoder.decode(saltBase64); // 使用相同的盐计算哈希值 MessageDigest md = MessageDigest.getInstance(ALGORITHM); diff --git a/web-service/src/main/java/cn/qaiu/lz/web/service/impl/DbServiceImpl.java b/web-service/src/main/java/cn/qaiu/lz/web/service/impl/DbServiceImpl.java index fbf88a1..ee71490 100644 --- a/web-service/src/main/java/cn/qaiu/lz/web/service/impl/DbServiceImpl.java +++ b/web-service/src/main/java/cn/qaiu/lz/web/service/impl/DbServiceImpl.java @@ -1,5 +1,6 @@ package cn.qaiu.lz.web.service.impl; +import cn.qaiu.util.Base64Decoder; import cn.qaiu.db.pool.JDBCPoolInit; import cn.qaiu.lz.common.model.UserInfo; import cn.qaiu.lz.web.model.StatisticsInfo; @@ -526,7 +527,7 @@ public class DbServiceImpl implements DbService { private boolean isLikelyEncrypted(String value) { try { - byte[] decoded = Base64.getDecoder().decode(value); + byte[] decoded = Base64Decoder.decode(value); return decoded.length > 16; } catch (Exception e) { return false; @@ -538,8 +539,8 @@ public class DbServiceImpl implements DbService { throw new IllegalArgumentException("invalid donated account token"); } String[] parts = token.split("\\.", 2); - String payload = new String(Base64.getUrlDecoder().decode(parts[0]), StandardCharsets.UTF_8); - String signature = new String(Base64.getUrlDecoder().decode(parts[1]), StandardCharsets.UTF_8); + String payload = new String(Base64Decoder.decodeUrl(parts[0]), StandardCharsets.UTF_8); + String signature = new String(Base64Decoder.decodeUrl(parts[1]), StandardCharsets.UTF_8); String expected = hmacSha256(payload); if (!expected.equals(signature)) { throw new IllegalArgumentException("donated account token signature invalid"); diff --git a/web-service/src/main/java/cn/qaiu/lz/web/util/CryptoUtil.java b/web-service/src/main/java/cn/qaiu/lz/web/util/CryptoUtil.java index b2fc3cc..0d95782 100644 --- a/web-service/src/main/java/cn/qaiu/lz/web/util/CryptoUtil.java +++ b/web-service/src/main/java/cn/qaiu/lz/web/util/CryptoUtil.java @@ -1,5 +1,6 @@ package cn.qaiu.lz.web.util; +import cn.qaiu.util.Base64Decoder; import cn.qaiu.vx.core.util.ConfigUtil; import cn.qaiu.vx.core.util.VertxHolder; import io.vertx.core.Future; @@ -81,7 +82,7 @@ public class CryptoUtil { } return secretKeyFuture.compose(secretKey -> { try { - byte[] decodedBytes = Base64.getDecoder().decode(strToDecrypt); + byte[] decodedBytes = Base64Decoder.decode(strToDecrypt); // Extract IV from the beginning of the decoded bytes ByteBuffer byteBuffer = ByteBuffer.wrap(decodedBytes); diff --git a/web-service/src/test/java/cn/qaiu/lz/common/util/Base64CompatibilityTest.java b/web-service/src/test/java/cn/qaiu/lz/common/util/Base64CompatibilityTest.java new file mode 100644 index 0000000..0cc3471 --- /dev/null +++ b/web-service/src/test/java/cn/qaiu/lz/common/util/Base64CompatibilityTest.java @@ -0,0 +1,40 @@ +package cn.qaiu.lz.common.util; + +import org.junit.Test; + +import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; +import java.util.Base64; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +public class Base64CompatibilityTest { + @Test + public void acceptsStoredPasswordHash() { + String password = "密码-test"; + String hash = PasswordUtil.hashPassword(password); + assertTrue(PasswordUtil.checkPassword(password, hash)); + assertFalse(PasswordUtil.checkPassword("wrong", hash)); + } + + @Test + public void acceptsJwtWithUnpaddedUrlSafePayload() { + String json = "{\"username\":\"下载用户\"}"; + String payload = Base64.getUrlEncoder().withoutPadding().encodeToString(json.getBytes(StandardCharsets.UTF_8)); + assertEquals("下载用户", JwtUtil.getUsernameFromToken("header." + payload + ".signature")); + } + + @Test + public void acceptsLegacyEncodedAuth() throws Exception { + String key = "0123456789abcdef"; + String json = "{\"authType\":\"cookie\",\"token\":\"compatibility-test\"}"; + String base64 = cn.qaiu.util.AESUtils.encryptBase64ByAES(json, key); + String urlEncoded = URLEncoder.encode(base64, StandardCharsets.UTF_8); + assertEquals("cookie", AuthParamCodec.decode(urlEncoded, key).getAuthType()); + assertEquals("cookie", AuthParamCodec.decode(base64.replace('+', ' '), key).getAuthType()); + assertNull(AuthParamCodec.decode(base64 + "!", key)); + } +}