Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ public Optional<Path> getLibrary(Library library) {
*/
public Path cacheLibrary(Library library, Path path, boolean forge) throws IOException {
String hash = library.getDownload().getSha1();
if (hash == null)
if (!DigestUtils.isSha1Digest(hash))
hash = DigestUtils.digestToString(SHA1, path);

Path cache = getFile(SHA1, hash);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ public void close() throws IOException {
handler.checkIntegrity(temp, file);
}

if (checksum != null) {
if (checksum != null && !checksum.isEmpty()) {
String actualChecksum = HexFormat.of().formatHex(digest.digest());
if (!checksum.equalsIgnoreCase(actualChecksum)) {
throw new ChecksumMismatchException(algorithm, checksum, actualChecksum);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ protected Path getFile(String algorithm, String hash) {
}

protected boolean fileExists(String algorithm, String hash) {
if (hash == null) return false;
if (!DigestUtils.isSha1Digest(hash)) return false;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

fileExists 方法中,直接使用 DigestUtils.isSha1Digest(hash) 会导致非 SHA-1 算法(如 MD5、SHA-256 等)的校验失效。由于 CacheRepository 的设计是支持多种算法的(通过 algorithm 参数和对应的子目录),这里应该根据传入的 algorithm 来动态验证哈希值的有效性。建议使用一个通用的 isValidHash(algorithm, hash) 方法进行校验。

Suggested change
if (!DigestUtils.isSha1Digest(hash)) return false;
if (!isValidHash(algorithm, hash)) return false;

Path file = getFile(algorithm, hash);
if (Files.exists(file)) {
try {
Expand All @@ -114,13 +114,23 @@ protected boolean fileExists(String algorithm, String hash) {
}
}

private void checkHash(String hash) throws IOException {
if (!DigestUtils.isSha1Digest(hash)) {
throw new IOException("Not SHA-1 checksum: " + hash);
}
}

public void tryCacheFile(Path path, String algorithm, String hash) throws IOException {
checkHash(hash);

Path cache = getFile(algorithm, hash);
if (Files.isRegularFile(cache)) return;
FileUtils.copyFile(path, cache);
}

public Path cacheFile(Path path, String algorithm, String hash) throws IOException {
checkHash(hash);

Path cache = getFile(algorithm, hash);
FileUtils.copyFile(path, cache);
return cache;
Comment on lines +117 to 136

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

checkHash 方法中硬编码 DigestUtils.isSha1Digest(hash) 会导致非 SHA-1 算法在缓存时抛出 IOException。此外,当下载 SHA-1 为空字符串的文件时,FileDownloadTask 仍会尝试调用 cacheFile,这会导致抛出异常并打印警告日志。

建议:

  1. 实现一个通用的 isValidHash(algorithm, hash) 方法,根据不同的算法验证哈希长度和十六进制格式。
  2. 重构 checkHash 方法,使其接受 algorithm 参数并调用 isValidHash
  3. tryCacheFilecacheFile 中,如果 hash 为空或为 null,则直接返回(no-op),避免抛出异常和打印无意义的警告日志。
    private static boolean isValidHash(String algorithm, String hash) {
        if (hash == null) return false;
        int expectedLength = switch (algorithm.toUpperCase(Locale.ROOT)) {
            case "SHA-1", "SHA1" -> 40;
            case "MD5" -> 32;
            case "SHA-256", "SHA256" -> 64;
            case "SHA-512", "SHA512" -> 128;
            default -> -1;
        };
        if (expectedLength != -1) {
            if (hash.length() != expectedLength) return false;
        } else {
            if (hash.length() < 2) return false;
        }
        for (int i = 0; i < hash.length(); i++) {
            char ch = hash.charAt(i);
            if ((ch < '0' || ch > '9') && (ch < 'a' || ch > 'f') && (ch < 'A' || ch > 'F')) {
                return false;
            }
        }
        return true;
    }

    private void checkHash(String algorithm, String hash) throws IOException {
        if (!isValidHash(algorithm, hash)) {
            throw new IOException("Not " + algorithm + " checksum: " + hash);
        }
    }

    public void tryCacheFile(Path path, String algorithm, String hash) throws IOException {
        if (hash == null || hash.isEmpty()) return;
        checkHash(algorithm, hash);

        Path cache = getFile(algorithm, hash);
        if (Files.isRegularFile(cache)) return;
        FileUtils.copyFile(path, cache);
    }

    public Path cacheFile(Path path, String algorithm, String hash) throws IOException {
        if (hash == null || hash.isEmpty()) return path;
        checkHash(algorithm, hash);

        Path cache = getFile(algorithm, hash);
        FileUtils.copyFile(path, cache);
        return cache;
    }

Expand Down