-
-
Notifications
You must be signed in to change notification settings - Fork 26
GH-1381 Verify dependency integrity (checksums) and add download timeouts #1381
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Jakubk15
wants to merge
2
commits into
master
Choose a base branch
from
harden/dependency-loader-integrity
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
eternalcore-plugin/src/main/java/com/eternalcode/core/loader/dependency/Checksum.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| package com.eternalcode.core.loader.dependency; | ||
|
|
||
| import java.security.MessageDigest; | ||
| import java.security.NoSuchAlgorithmException; | ||
|
|
||
| /** | ||
| * Verifies the integrity of downloaded artifacts against the checksum files published alongside them in a Maven | ||
| * repository (e.g. {@code artifact-1.0.jar.sha256}). | ||
| * | ||
| * <p>Ordered strongest-first so callers can prefer the most secure digest a repository publishes. SHA-1 is retained | ||
| * only as a last-resort fallback because it is the single digest Maven repositories are guaranteed to serve; it is | ||
| * cryptographically weak and should not be relied upon on its own. | ||
| */ | ||
| public enum Checksum { | ||
|
|
||
| SHA512("sha512", "SHA-512"), | ||
| SHA256("sha256", "SHA-256"), | ||
| SHA1("sha1", "SHA-1"); | ||
|
|
||
| private final String extension; | ||
| private final String algorithm; | ||
|
|
||
| Checksum(String extension, String algorithm) { | ||
| this.extension = extension; | ||
| this.algorithm = algorithm; | ||
| } | ||
|
|
||
| /** | ||
| * The file extension appended to the artifact name to locate this checksum (without a leading dot). | ||
| */ | ||
| public String extension() { | ||
| return this.extension; | ||
| } | ||
|
|
||
| /** | ||
| * Computes the lower-case hex digest of the given data using this algorithm. | ||
| */ | ||
| public String hash(byte[] data) { | ||
| MessageDigest digest; | ||
| try { | ||
| digest = MessageDigest.getInstance(this.algorithm); | ||
| } | ||
| catch (NoSuchAlgorithmException exception) { | ||
| throw new DependencyException("Missing digest algorithm: " + this.algorithm, exception); | ||
| } | ||
|
|
||
| return toHex(digest.digest(data)); | ||
| } | ||
|
|
||
| /** | ||
| * Returns {@code true} if the digest of {@code data} equals the published checksum. | ||
| * | ||
| * <p>Published checksum files sometimes contain trailing content such as {@code "<hash> <filename>"}; | ||
| * only the leading token is compared, and comparison is case-insensitive. | ||
| */ | ||
| public boolean matches(byte[] data, String publishedChecksum) { | ||
| if (publishedChecksum == null) { | ||
| return false; | ||
| } | ||
|
|
||
| String expected = normalize(publishedChecksum); | ||
| if (expected.isEmpty()) { | ||
| return false; | ||
| } | ||
|
|
||
| return expected.equalsIgnoreCase(this.hash(data)); | ||
| } | ||
|
|
||
| static String normalize(String rawChecksum) { | ||
| String trimmed = rawChecksum.trim(); | ||
|
|
||
| for (int index = 0; index < trimmed.length(); index++) { | ||
| if (Character.isWhitespace(trimmed.charAt(index))) { | ||
| return trimmed.substring(0, index); | ||
| } | ||
| } | ||
|
|
||
| return trimmed; | ||
| } | ||
|
|
||
| private static String toHex(byte[] bytes) { | ||
| StringBuilder builder = new StringBuilder(bytes.length * 2); | ||
|
|
||
| for (byte value : bytes) { | ||
| builder.append(Character.forDigit((value >> 4) & 0xF, 16)); | ||
| builder.append(Character.forDigit(value & 0xF, 16)); | ||
| } | ||
|
|
||
| return builder.toString(); | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
eternalcore-plugin/src/test/java/com/eternalcode/core/loader/dependency/ChecksumTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| package com.eternalcode.core.loader.dependency; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertFalse; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| import java.nio.charset.StandardCharsets; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| class ChecksumTest { | ||
|
|
||
| // Known digests of the ASCII string "abc". | ||
| private static final byte[] ABC = "abc".getBytes(StandardCharsets.UTF_8); | ||
| private static final String ABC_SHA1 = "a9993e364706816aba3e25717850c26c9cd0d89d"; | ||
| private static final String ABC_SHA256 = "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"; | ||
| private static final String ABC_SHA512 = | ||
| "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a" | ||
| + "2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f"; | ||
|
|
||
| @Test | ||
| void computesKnownDigests() { | ||
| assertEquals(ABC_SHA1, Checksum.SHA1.hash(ABC)); | ||
| assertEquals(ABC_SHA256, Checksum.SHA256.hash(ABC)); | ||
| assertEquals(ABC_SHA512, Checksum.SHA512.hash(ABC)); | ||
| } | ||
|
|
||
| @Test | ||
| void matchesIgnoringCase() { | ||
| assertTrue(Checksum.SHA256.matches(ABC, ABC_SHA256)); | ||
| assertTrue(Checksum.SHA256.matches(ABC, ABC_SHA256.toUpperCase())); | ||
| } | ||
|
|
||
| @Test | ||
| void rejectsMismatchedChecksum() { | ||
| assertFalse(Checksum.SHA256.matches(ABC, ABC_SHA1)); | ||
| assertFalse(Checksum.SHA256.matches(ABC, "not-a-hash")); | ||
| } | ||
|
|
||
| @Test | ||
| void rejectsNullOrBlankChecksum() { | ||
| assertFalse(Checksum.SHA256.matches(ABC, null)); | ||
| assertFalse(Checksum.SHA256.matches(ABC, " ")); | ||
| } | ||
|
|
||
| @Test | ||
| void ignoresTrailingFilenameInPublishedChecksum() { | ||
| // Maven repositories occasionally publish "<hash> <filename>". | ||
| assertTrue(Checksum.SHA256.matches(ABC, ABC_SHA256 + " artifact-1.0.jar")); | ||
| assertEquals(ABC_SHA256, Checksum.normalize(ABC_SHA256 + " artifact-1.0.jar")); | ||
| } | ||
|
|
||
| @Test | ||
| void exposesExpectedExtensions() { | ||
| assertEquals("sha512", Checksum.SHA512.extension()); | ||
| assertEquals("sha256", Checksum.SHA256.extension()); | ||
| assertEquals("sha1", Checksum.SHA1.extension()); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.