Skip to content

Commit 0f86ef1

Browse files
authored
modrinth: sanitize modpack file's path (#326)
1 parent aa062a7 commit 0f86ef1

File tree

4 files changed

+59
-10
lines changed

4 files changed

+59
-10
lines changed

src/main/java/me/itzg/helpers/modrinth/ModrinthPackInstaller.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,9 @@ private Flux<Path> processModpackFiles(ModpackIndex modpackIndex) {
112112
)
113113
.publishOn(Schedulers.boundedElastic())
114114
.flatMap(modpackFile -> {
115+
final String modpackFilePath = sanitizeModFilePath(modpackFile.getPath());
115116
final Path outFilePath =
116-
this.outputDirectory.resolve(modpackFile.getPath());
117+
this.outputDirectory.resolve(modpackFilePath);
117118
try {
118119
//noinspection BlockingMethodInNonBlockingContext
119120
Files.createDirectories(outFilePath.getParent());
@@ -126,11 +127,22 @@ private Flux<Path> processModpackFiles(ModpackIndex modpackIndex) {
126127
outFilePath,
127128
modpackFile.getDownloads().get(0),
128129
(uri, file, contentSizeBytes) ->
129-
log.info("Downloaded {}", modpackFile.getPath())
130+
log.info("Downloaded {}", modpackFilePath)
130131
);
131132
});
132133
}
133134

135+
private String sanitizeModFilePath(String path) {
136+
// Using only backslash delimiters and not forward slashes?
137+
// (mixed usage will assume backslashes were purposeful)
138+
if (path.contains("\\") && !path.contains("/")) {
139+
return path.replace("\\", "/");
140+
}
141+
else {
142+
return path;
143+
}
144+
}
145+
134146
@SuppressWarnings("SameParameterValue")
135147
private Stream<Path> extractOverrides(String... overridesDirs) {
136148
try (ZipFile zipFileReader = new ZipFile(zipFile.toFile())) {

src/test/java/me/itzg/helpers/modrinth/InstallModrinthModpackCommandTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ void downloadsAndInstallsModrinthModpack(
4747
String expectedFileData = "some test data";
4848
String relativeFilePath = "test_file";
4949
ModpackFile testFile = createHostedModpackFile(
50-
relativeFilePath, expectedFileData, wm.getHttpBaseUrl());
50+
relativeFilePath, relativeFilePath, expectedFileData, wm.getHttpBaseUrl());
5151

5252
ModpackIndex index = createBasicModpackIndex();
5353
index.getFiles().add(testFile);
@@ -74,7 +74,7 @@ void downloadsAndInstallsModrinthModpack_versionNumberAndAnyLoader(
7474
String expectedFileData = "some test data";
7575
String relativeFilePath = "test_file";
7676
ModpackFile testFile = createHostedModpackFile(
77-
relativeFilePath, expectedFileData, wm.getHttpBaseUrl());
77+
relativeFilePath, relativeFilePath, expectedFileData, wm.getHttpBaseUrl());
7878

7979
ModpackIndex index = createBasicModpackIndex();
8080
index.getFiles().add(testFile);
@@ -161,7 +161,7 @@ void removesFilesNoLongerNeedeByUpdatedModpack(
161161
String expectedFileData = "some test data";
162162
String relativeFilePath = "test_file";
163163
ModpackFile testFile = createHostedModpackFile(
164-
relativeFilePath, expectedFileData, wm.getHttpBaseUrl());
164+
relativeFilePath, relativeFilePath, expectedFileData, wm.getHttpBaseUrl());
165165

166166
ModpackIndex index = createBasicModpackIndex();
167167
index.getFiles().add(testFile);
@@ -201,7 +201,7 @@ void downloadsAndInstallsGenericModpacksOverHttp(
201201
String relativeFilePath = "test_file";
202202
String modpackDownloadPath = "/files/modpacks/test_modpack-1.0.0.mrpack";
203203
ModpackFile testFile = createHostedModpackFile(
204-
relativeFilePath, expectedFileData, wm.getHttpBaseUrl());
204+
relativeFilePath, relativeFilePath, expectedFileData, wm.getHttpBaseUrl());
205205

206206
ModpackIndex index = createBasicModpackIndex();
207207
index.getFiles().add(testFile);

src/test/java/me/itzg/helpers/modrinth/ModrinthPackInstallerTest.java

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,44 @@ void installDownloadsDependentFilesToInstallation(
6969

7070
ModpackIndex index = createBasicModpackIndex();
7171
index.getFiles().add(createHostedModpackFile(
72-
relativeFilePath, expectedFileData, wm.getHttpBaseUrl()));
72+
relativeFilePath, relativeFilePath, expectedFileData, wm.getHttpBaseUrl()));
73+
74+
Files.write(modpackPath, createModrinthPack(index));
75+
76+
ModrinthPackInstaller installerUT = new ModrinthPackInstaller(
77+
apiClient, fetchOpts, modpackPath, tempDir, resultsFile, false);
78+
79+
final Installation installation = installerUT.processModpack(sharedFetch).block();
80+
81+
assertThat(installation).isNotNull();
82+
List<Path> installedFiles = installation.getFiles();
83+
84+
assertThat(expectedFilePath).isRegularFile();
85+
assertThat(expectedFilePath).content()
86+
.isEqualTo(expectedFileData);
87+
assertThat(installedFiles.size()).isEqualTo(1);
88+
assertThat(installedFiles.get(0)).isEqualTo(expectedFilePath);
89+
}
90+
}
91+
92+
@Test
93+
void sanitizesModFilePath(
94+
WireMockRuntimeInfo wm, @TempDir Path tempDir
95+
) throws IOException, URISyntaxException
96+
{
97+
Options fetchOpts = new SharedFetchArgs().options();
98+
try (SharedFetch sharedFetch = Fetch.sharedFetch("install-modrinth-modpack", fetchOpts)) {
99+
ModrinthApiClient apiClient = new ModrinthApiClient(
100+
wm.getHttpBaseUrl(), "install-modrinth-modpack", fetchOpts);
101+
102+
String expectedFileData = "some test data";
103+
Path expectedFilePath = tempDir.resolve("mods/mod.jar");
104+
Path resultsFile = tempDir.resolve("results");
105+
Path modpackPath = tempDir.resolve("test.mrpack");
106+
107+
ModpackIndex index = createBasicModpackIndex();
108+
index.getFiles().add(createHostedModpackFile(
109+
"mods\\mod.jar", "mods/mod.jar", expectedFileData, wm.getHttpBaseUrl()));
73110

74111
Files.write(modpackPath, createModrinthPack(index));
75112

src/test/java/me/itzg/helpers/modrinth/ModrinthTestHelpers.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,17 +101,17 @@ static byte[] createModrinthPack(ModpackIndex index) throws IOException {
101101
}
102102

103103
static ModpackIndex.ModpackFile createHostedModpackFile(
104-
String relativeFileLocation, String data, String wmBaseUrl
104+
String relativeFileLocation, String urlSubpath, String data, String wmBaseUrl
105105
) throws URISyntaxException
106106
{
107-
stubFor(get("/files/" + relativeFileLocation)
107+
stubFor(get("/files/" + urlSubpath)
108108
.willReturn(ok().withBody(data)));
109109

110110
ModpackIndex.ModpackFile modpackFile = new ModpackIndex.ModpackFile()
111111
.setDownloads(new ArrayList<>())
112112
.setPath(relativeFileLocation);
113113
modpackFile.getDownloads().add(
114-
new URI(wmBaseUrl + "/files/" + relativeFileLocation));
114+
new URI(wmBaseUrl + "/files/" + urlSubpath));
115115

116116
return modpackFile;
117117
}

0 commit comments

Comments
 (0)