Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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,42 @@ 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 = parseDigest(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 in "sha256:<hex>" format
private static byte @Nullable [] parseDigest(String digest) throws UpdateFailedException {
if (!digest.startsWith("sha256:")) {
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
return null;
}
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 +130,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,111 @@
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;

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-x64.exe",
"Cryptomator-1.18.0-arm64.dmg",
"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 = {
".",
"..",
".hidden.exe",
"-rf.exe",
"../Cryptomator-x64.exe",
"..\\..\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\Cryptomator-x64.exe",
"sub/Cryptomator-x64.exe",
"/etc/Cryptomator.AppImage",
"C:\\Cryptomator-x64.exe",
"C:Cryptomator-x64.exe",
"Cryptomator-x64.exe:stream",
"Cryptomator x64.exe",
"Cryptomator\t.exe",
"Cryptomator\n.exe",
"Cryptomator\u0000.exe",
"Cryptomator&calc.exe",
"Cryptomator%TEMP%.exe",
"Cryptomator$(id).AppImage",
"Cryptomator\".exe",
"Cryptomätor.exe"
})
public void testUnsafeAssetNames(String name) {
Assertions.assertFalse(DownloadUpdateMechanism.isSafeAssetName(name));
}

@ParameterizedTest
@NullAndEmptySource
@ValueSource(strings = {
"..\\..\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\Cryptomator-x64.exe",
"../../.local/share/applications/cryptomator.desktop"
})
public void testFirstStepRejectsUnsafeAssetName(String name) {
var updateInfo = updateInfo(name, "sha256:" + "00".repeat(32));

Assertions.assertThrows(UpdateFailedException.class, () -> mechanism.firstStep(updateInfo));
}

@ParameterizedTest
@ValueSource(strings = {
"sha256:0",
"sha256:xyz",
"sha256:-0123456789abcdef"
})
public void testFirstStepRejectsMalformedDigest(String digest) {
var updateInfo = updateInfo("Cryptomator-1.18.0-x64.msi", digest);

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