Skip to content
Merged
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
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ Changes to prior versions can be found on the [Github release page](https://gith

## [Unreleased](https://github.com/cryptomator/integrations-api/compare/1.10.0...HEAD)

No changes yet.
### Security
* `DownloadUpdateMechanism` rejects assets whose name is not a plain file name, preventing the download from being written outside of its temporary working directory

### Fixed
* `DownloadUpdateMechanism` reports a malformed asset digest as `UpdateFailedException` instead of `IllegalArgumentException`


## [1.10.0](https://github.com/cryptomator/integrations-api/releases/tag/1.10.0) - 2026-10-13
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@
import java.nio.file.Path;
import java.util.HexFormat;
import java.util.List;
import java.util.regex.Pattern;

public abstract class DownloadUpdateMechanism implements UpdateMechanism<DownloadUpdateInfo> {

private static final Logger LOG = LoggerFactory.getLogger(DownloadUpdateMechanism.class);
private static final String LATEST_VERSION_API_URL = "https://api.cryptomator.org/connect/apps/desktop/latest-version?format=1";
private static final ObjectMapper MAPPER = new ObjectMapper();
private static final Pattern SAFE_ASSET_NAME = Pattern.compile("[A-Za-z0-9][A-Za-z0-9._+-]*");

@Override
public DownloadUpdateInfo checkForUpdate(String currentVersion, HttpClient httpClient) {
Expand All @@ -51,18 +53,44 @@ public DownloadUpdateInfo checkForUpdate(String currentVersion, HttpClient httpC
* Returns the first step to prepare the update. This downloads the {@link DownloadUpdateInfo#asset() asset} to a temporary location and verifies its checksum.
* @param updateInfo The {@link DownloadUpdateInfo} retrieved from {@link #checkForUpdate(String, HttpClient)}.
* @return a new {@link UpdateStep} that can be used to monitor the download progress.
* @throws UpdateFailedException When failing to prepare a temporary download location.
* @throws UpdateFailedException When the asset's name is not a plain file name, its digest is malformed, or when failing to prepare a temporary download location.
*/
@Override
public UpdateStep firstStep(DownloadUpdateInfo updateInfo) throws UpdateFailedException {
var asset = updateInfo.asset();
// the name is used as file name in the work dir, so it must not be able to point anywhere else
if (!isSafeAssetName(asset.name())) {
LOG.debug("Rejected unsafe asset name: \"{}\"", asset.name());
throw new UpdateFailedException("Asset name is not a safe file name.");
}
var digest = parseSha256Digest(asset.digest());
try {
Path workDir = Files.createTempDirectory("cryptomator-update");
return new FirstStep(workDir, updateInfo);
return new FirstStep(workDir, updateInfo, digest);
} catch (IOException e) {
throw new UpdateFailedException("Failed to create temporary directory for update", e);
}
}

// allowlist of plain file names: no path separators, no drive letters, no leading dot, no shell metacharacters
static boolean isSafeAssetName(@Nullable String name) {
return name != null && SAFE_ASSET_NAME.matcher(name).matches();
}

// returns the expected SHA-256 checksum or null, if the digest is not prefixed with "sha256:"; throws if the hex part is not exactly 64 hex chars
static byte @Nullable [] parseSha256Digest(String digest) throws UpdateFailedException {
if (!digest.startsWith("sha256:")) {
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
return null;
} else if (digest.length() != 71) {
throw new UpdateFailedException("Malformed asset digest: expected 64 hexadecimal characters for SHA-256.");
}
try {
return HexFormat.of().withLowerCase().parseHex(digest.substring(7)); // remove "sha256:" prefix
Comment thread
infeo marked this conversation as resolved.
Resolved
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
} catch (IllegalArgumentException e) {
throw new UpdateFailedException("Malformed asset digest.", e);
}
}

/**
* Second step that is executed after the download has completed in the {@link #firstStep(DownloadUpdateInfo) first step}.
* @param workDir A temporary working directory to which the asset has been downloaded.
Expand Down Expand Up @@ -104,12 +132,9 @@ private class FirstStep extends DownloadUpdateStep {
private final Path workDir;
private final DownloadUpdateInfo updateInfo;

public FirstStep(Path workDir, DownloadUpdateInfo updateInfo) {
public FirstStep(Path workDir, DownloadUpdateInfo updateInfo, byte @Nullable [] digest) {
var uri = URI.create(updateInfo.asset().downloadUrl);
var destination = workDir.resolve(updateInfo.asset().name);
var digest = updateInfo.asset().digest().startsWith("sha256:")
? HexFormat.of().withLowerCase().parseHex(updateInfo.asset().digest.substring(7)) // remove "sha256:" prefix
: null;
var size = updateInfo.asset().size;
super(uri, destination, digest, size);
this.workDir = workDir;
Expand Down
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);
}

}
Loading