Repository navigation
The update API: Restrict name of to-be-downloaded asset #109
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
129 changes: 129 additions & 0 deletions
129
src/test/java/org/cryptomator/integrations/update/DownloadUpdateMechanismTest.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,129 @@ | ||
| package org.cryptomator.integrations.update; | ||
|
|
||
| import org.junit.jupiter.api.Assertions; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.params.ParameterizedTest; | ||
| import org.junit.jupiter.params.provider.NullAndEmptySource; | ||
| import org.junit.jupiter.params.provider.ValueSource; | ||
|
|
||
| import java.io.IOException; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.util.HexFormat; | ||
|
|
||
| public class DownloadUpdateMechanismTest { | ||
|
|
||
| private final DownloadUpdateMechanism mechanism = new DownloadUpdateMechanism() { | ||
| @Override | ||
| public UpdateStep secondStep(Path workDir, Path assetPath, DownloadUpdateInfo updateInfo) { | ||
| throw new UnsupportedOperationException(); | ||
| } | ||
|
|
||
| @Override | ||
| protected DownloadUpdateInfo checkForUpdate(String currentVersion, LatestVersionResponse response) { | ||
| throw new UnsupportedOperationException(); | ||
| } | ||
| }; | ||
|
|
||
| @ParameterizedTest | ||
| @ValueSource(strings = { | ||
| "Cryptomator-1.18.0-x64.msi", | ||
| "cryptomator-1.18.0-x86_64.AppImage", | ||
| "Cryptomator-1.18.0-beta1+build.42.dmg", | ||
| "a" | ||
| }) | ||
| public void testSafeAssetNames(String name) { | ||
| Assertions.assertTrue(DownloadUpdateMechanism.isSafeAssetName(name)); | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
| @NullAndEmptySource | ||
| @ValueSource(strings = { | ||
| "..", | ||
| "-rf.exe", | ||
| "../Cryptomator-x64.exe", | ||
| "..\\..\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\Cryptomator-x64.exe", | ||
| "C:Cryptomator-x64.exe", | ||
| "Cryptomator x64.exe", | ||
| "Cryptomator\u0000.exe", | ||
| "Cryptomator&calc.exe", | ||
| "Cryptomätor.exe" | ||
| }) | ||
| public void testUnsafeAssetNames(String name) { | ||
| Assertions.assertFalse(DownloadUpdateMechanism.isSafeAssetName(name)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testFirstStepRejectsUnsafeAssetName() { | ||
| var updateInfo = updateInfo("..\\..\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\Cryptomator-x64.exe", "sha256:" + "00".repeat(32)); | ||
|
|
||
| Assertions.assertThrows(UpdateFailedException.class, () -> mechanism.firstStep(updateInfo)); | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
| @ValueSource(strings = {"0123456789abcdef", "0123456789ABCDEF"}) | ||
| public void testParseSha256Digest(String hexBlock) throws UpdateFailedException { | ||
| var digest = DownloadUpdateMechanism.parseSha256Digest("sha256:" + hexBlock.repeat(4)); | ||
|
|
||
| Assertions.assertArrayEquals(HexFormat.of().parseHex("0123456789abcdef".repeat(4)), digest); | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
| @ValueSource(strings = { | ||
| "", | ||
| "md5:0123456789abcdef0123456789abcdef", | ||
| "sha512:0123456789abcdef" | ||
| }) | ||
| public void testParseSha256DigestIgnoresOtherAlgorithms(String digest) throws UpdateFailedException { | ||
| Assertions.assertNull(DownloadUpdateMechanism.parseSha256Digest(digest)); | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
| @ValueSource(strings = { | ||
| "sha256:000000000000000000000000000000000000000000000000000000000000000", // 63 chars | ||
| "sha256:00000000000000000000000000000000000000000000000000000000000000000" // 65 chars | ||
| }) | ||
| public void testParseSha256DigestRejectsWrongLength(String digest) { | ||
| var e = Assertions.assertThrows(UpdateFailedException.class, () -> DownloadUpdateMechanism.parseSha256Digest(digest)); | ||
|
|
||
| Assertions.assertNull(e.getCause()); // rejected by the length check, not by the hex parser | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
| @ValueSource(strings = { | ||
| "sha256:000000000000000000000000000000000000000000000000000000000000000g", | ||
| "sha256:0000000000000000000000000000000000000000000000000000000000000000" // fullwidth digit zero, accepted by Character.digit() | ||
| }) | ||
| public void testParseSha256DigestRejectsNonHex(String digest) { | ||
| var e = Assertions.assertThrows(UpdateFailedException.class, () -> DownloadUpdateMechanism.parseSha256Digest(digest)); | ||
|
|
||
| Assertions.assertInstanceOf(IllegalArgumentException.class, e.getCause()); // passed the length check, rejected by the hex parser | ||
| } | ||
|
|
||
| @Test | ||
| public void testFirstStepRejectsMalformedDigest() { | ||
| var updateInfo = updateInfo("Cryptomator-1.18.0-x64.msi", "sha256:0"); | ||
|
|
||
| Assertions.assertThrows(UpdateFailedException.class, () -> mechanism.firstStep(updateInfo)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testFirstStepAcceptsSafeAssetName() throws IOException { | ||
| var updateInfo = updateInfo("Cryptomator-1.18.0-x64.msi", "sha256:" + "00".repeat(32)); | ||
|
|
||
| var step = Assertions.assertInstanceOf(DownloadUpdateStep.class, mechanism.firstStep(updateInfo)); | ||
| var workDir = step.destination.getParent(); | ||
| try { | ||
| Assertions.assertEquals("Cryptomator-1.18.0-x64.msi", step.destination.getFileName().toString()); | ||
| Assertions.assertTrue(Files.isDirectory(workDir)); | ||
| } finally { | ||
| Files.deleteIfExists(workDir); | ||
| } | ||
| } | ||
|
|
||
| private DownloadUpdateInfo updateInfo(String assetName, String digest) { | ||
| var asset = new DownloadUpdateMechanism.Asset(assetName, digest, 42L, "https://example.com/" + assetName); | ||
| return new DownloadUpdateInfo(mechanism, "1.18.0", asset); | ||
| } | ||
|
|
||
| } |
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.