Skip to content

Commit 10b666a

Browse files
committed
update api: restrict name of to-be-downloaded assets
and check digest value properly
1 parent 5719b69 commit 10b666a

3 files changed

Lines changed: 145 additions & 7 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ Changes to prior versions can be found on the [Github release page](https://gith
99

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

12-
No changes yet.
12+
### Security
13+
* `DownloadUpdateMechanism` rejects assets whose name is not a plain file name, preventing the download from being written outside of its temporary working directory
14+
15+
### Fixed
16+
* `DownloadUpdateMechanism` reports a malformed asset digest as `UpdateFailedException` instead of `IllegalArgumentException`
1317

1418

1519
## [1.10.0](https://github.com/cryptomator/integrations-api/releases/tag/1.10.0) - 2026-10-13

‎src/main/java/org/cryptomator/integrations/update/DownloadUpdateMechanism.java‎

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@
1919
import java.nio.file.Path;
2020
import java.util.HexFormat;
2121
import java.util.List;
22+
import java.util.regex.Pattern;
2223

2324
public abstract class DownloadUpdateMechanism implements UpdateMechanism<DownloadUpdateInfo> {
2425

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

2931
@Override
3032
public DownloadUpdateInfo checkForUpdate(String currentVersion, HttpClient httpClient) {
@@ -51,18 +53,42 @@ public DownloadUpdateInfo checkForUpdate(String currentVersion, HttpClient httpC
5153
* Returns the first step to prepare the update. This downloads the {@link DownloadUpdateInfo#asset() asset} to a temporary location and verifies its checksum.
5254
* @param updateInfo The {@link DownloadUpdateInfo} retrieved from {@link #checkForUpdate(String, HttpClient)}.
5355
* @return a new {@link UpdateStep} that can be used to monitor the download progress.
54-
* @throws UpdateFailedException When failing to prepare a temporary download location.
56+
* @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.
5557
*/
5658
@Override
5759
public UpdateStep firstStep(DownloadUpdateInfo updateInfo) throws UpdateFailedException {
60+
var asset = updateInfo.asset();
61+
// the name is used as file name in the work dir, so it must not be able to point anywhere else
62+
if (!isSafeAssetName(asset.name())) {
63+
LOG.debug("Rejected unsafe asset name: \"{}\"", asset.name());
64+
throw new UpdateFailedException("Asset name is not a safe file name.");
65+
}
66+
var digest = parseDigest(asset.digest());
5867
try {
5968
Path workDir = Files.createTempDirectory("cryptomator-update");
60-
return new FirstStep(workDir, updateInfo);
69+
return new FirstStep(workDir, updateInfo, digest);
6170
} catch (IOException e) {
6271
throw new UpdateFailedException("Failed to create temporary directory for update", e);
6372
}
6473
}
6574

75+
// allowlist of plain file names: no path separators, no drive letters, no leading dot, no shell metacharacters
76+
static boolean isSafeAssetName(@Nullable String name) {
77+
return name != null && SAFE_ASSET_NAME.matcher(name).matches();
78+
}
79+
80+
// returns the expected SHA-256 checksum or null, if the digest is not in "sha256:<hex>" format
81+
private static byte @Nullable [] parseDigest(String digest) throws UpdateFailedException {
82+
if (!digest.startsWith("sha256:")) {
83+
return null;
84+
}
85+
try {
86+
return HexFormat.of().withLowerCase().parseHex(digest.substring(7)); // remove "sha256:" prefix
87+
} catch (IllegalArgumentException e) {
88+
throw new UpdateFailedException("Malformed asset digest.", e);
89+
}
90+
}
91+
6692
/**
6793
* Second step that is executed after the download has completed in the {@link #firstStep(DownloadUpdateInfo) first step}.
6894
* @param workDir A temporary working directory to which the asset has been downloaded.
@@ -104,12 +130,9 @@ private class FirstStep extends DownloadUpdateStep {
104130
private final Path workDir;
105131
private final DownloadUpdateInfo updateInfo;
106132

107-
public FirstStep(Path workDir, DownloadUpdateInfo updateInfo) {
133+
public FirstStep(Path workDir, DownloadUpdateInfo updateInfo, byte @Nullable [] digest) {
108134
var uri = URI.create(updateInfo.asset().downloadUrl);
109135
var destination = workDir.resolve(updateInfo.asset().name);
110-
var digest = updateInfo.asset().digest().startsWith("sha256:")
111-
? HexFormat.of().withLowerCase().parseHex(updateInfo.asset().digest.substring(7)) // remove "sha256:" prefix
112-
: null;
113136
var size = updateInfo.asset().size;
114137
super(uri, destination, digest, size);
115138
this.workDir = workDir;
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package org.cryptomator.integrations.update;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
import org.junit.jupiter.params.ParameterizedTest;
6+
import org.junit.jupiter.params.provider.NullAndEmptySource;
7+
import org.junit.jupiter.params.provider.ValueSource;
8+
9+
import java.io.IOException;
10+
import java.nio.file.Files;
11+
import java.nio.file.Path;
12+
13+
public class DownloadUpdateMechanismTest {
14+
15+
private final DownloadUpdateMechanism mechanism = new DownloadUpdateMechanism() {
16+
@Override
17+
public UpdateStep secondStep(Path workDir, Path assetPath, DownloadUpdateInfo updateInfo) {
18+
throw new UnsupportedOperationException();
19+
}
20+
21+
@Override
22+
protected DownloadUpdateInfo checkForUpdate(String currentVersion, LatestVersionResponse response) {
23+
throw new UnsupportedOperationException();
24+
}
25+
};
26+
27+
@ParameterizedTest
28+
@ValueSource(strings = {
29+
"Cryptomator-1.18.0-x64.msi",
30+
"Cryptomator-1.18.0-x64.exe",
31+
"Cryptomator-1.18.0-arm64.dmg",
32+
"cryptomator-1.18.0-x86_64.AppImage",
33+
"Cryptomator-1.18.0-beta1+build.42.dmg",
34+
"a"
35+
})
36+
public void testSafeAssetNames(String name) {
37+
Assertions.assertTrue(DownloadUpdateMechanism.isSafeAssetName(name));
38+
}
39+
40+
@ParameterizedTest
41+
@NullAndEmptySource
42+
@ValueSource(strings = {
43+
".",
44+
"..",
45+
".hidden.exe",
46+
"-rf.exe",
47+
"../Cryptomator-x64.exe",
48+
"..\\..\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\Cryptomator-x64.exe",
49+
"sub/Cryptomator-x64.exe",
50+
"/etc/Cryptomator.AppImage",
51+
"C:\\Cryptomator-x64.exe",
52+
"C:Cryptomator-x64.exe",
53+
"Cryptomator-x64.exe:stream",
54+
"Cryptomator x64.exe",
55+
"Cryptomator\t.exe",
56+
"Cryptomator\n.exe",
57+
"Cryptomator\u0000.exe",
58+
"Cryptomator&calc.exe",
59+
"Cryptomator%TEMP%.exe",
60+
"Cryptomator$(id).AppImage",
61+
"Cryptomator\".exe",
62+
"Cryptomätor.exe"
63+
})
64+
public void testUnsafeAssetNames(String name) {
65+
Assertions.assertFalse(DownloadUpdateMechanism.isSafeAssetName(name));
66+
}
67+
68+
@ParameterizedTest
69+
@NullAndEmptySource
70+
@ValueSource(strings = {
71+
"..\\..\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\Cryptomator-x64.exe",
72+
"../../.local/share/applications/cryptomator.desktop"
73+
})
74+
public void testFirstStepRejectsUnsafeAssetName(String name) {
75+
var updateInfo = updateInfo(name, "sha256:" + "00".repeat(32));
76+
77+
Assertions.assertThrows(UpdateFailedException.class, () -> mechanism.firstStep(updateInfo));
78+
}
79+
80+
@ParameterizedTest
81+
@ValueSource(strings = {
82+
"sha256:0",
83+
"sha256:xyz",
84+
"sha256:-0123456789abcdef"
85+
})
86+
public void testFirstStepRejectsMalformedDigest(String digest) {
87+
var updateInfo = updateInfo("Cryptomator-1.18.0-x64.msi", digest);
88+
89+
Assertions.assertThrows(UpdateFailedException.class, () -> mechanism.firstStep(updateInfo));
90+
}
91+
92+
@Test
93+
public void testFirstStepAcceptsSafeAssetName() throws IOException {
94+
var updateInfo = updateInfo("Cryptomator-1.18.0-x64.msi", "sha256:" + "00".repeat(32));
95+
96+
var step = Assertions.assertInstanceOf(DownloadUpdateStep.class, mechanism.firstStep(updateInfo));
97+
var workDir = step.destination.getParent();
98+
try {
99+
Assertions.assertEquals("Cryptomator-1.18.0-x64.msi", step.destination.getFileName().toString());
100+
Assertions.assertTrue(Files.isDirectory(workDir));
101+
} finally {
102+
Files.deleteIfExists(workDir);
103+
}
104+
}
105+
106+
private DownloadUpdateInfo updateInfo(String assetName, String digest) {
107+
var asset = new DownloadUpdateMechanism.Asset(assetName, digest, 42L, "https://example.com/" + assetName);
108+
return new DownloadUpdateInfo(mechanism, "1.18.0", asset);
109+
}
110+
111+
}

0 commit comments

Comments
 (0)