diff --git a/src/main/java/com/ldtteam/structurize/storage/ClientStructurePackLoader.java b/src/main/java/com/ldtteam/structurize/storage/ClientStructurePackLoader.java index d48365d01..8d281610e 100644 --- a/src/main/java/com/ldtteam/structurize/storage/ClientStructurePackLoader.java +++ b/src/main/java/com/ldtteam/structurize/storage/ClientStructurePackLoader.java @@ -25,6 +25,7 @@ import java.io.*; import java.nio.file.Files; +import java.nio.file.InvalidPathException; import java.nio.file.Path; import java.nio.file.StandardCopyOption; import java.util.*; @@ -262,6 +263,10 @@ else if (version != pack.getVersion()) public static void onStructurePackTransfer(final String name, final ByteBuf payload, final boolean eol) { final String packName = Utils.getSafePackName(name); + if (packName.isEmpty()) + { + return; + } Log.getLogger().warn("Received Structure pack from the Server: " + packName); IOPool.execute(() -> { @@ -356,12 +361,33 @@ public static Path zipSlipProtect(ZipEntry zipEntry, Path targetDir) throws IOEx public static void handleSaveScanMessage(final CompoundTag compound, final String fileName, final HolderLookup.Provider provider) { final String packName = Utils.getSafePackName(Minecraft.getInstance().getUser().getName()); + + final Path scansFolder = Minecraft.getInstance().gameDirectory.toPath() + .resolve(BLUEPRINT_FOLDER) + .resolve(packName.toLowerCase(Locale.US)) + .resolve(SCANS_FOLDER) + .toAbsolutePath() + .normalize(); + + final Path targetPath; + try + { + targetPath = scansFolder.resolve(fileName).normalize(); + } + catch (final InvalidPathException e) + { + return; + } + + if (!targetPath.startsWith(scansFolder) || targetPath.equals(scansFolder)) + { + Log.getLogger().warn("Rejected scan save request with unsafe file name: {}", fileName); + return; + } + StructurePacks.switchSelectedPack(StructurePacks.getStructurePack(Utils.getSafePackName(Minecraft.getInstance().getUser().getName()))); RenderingCache.getOrCreateBlueprintPreviewData("blueprint").setBlueprintFuture( - StructurePacks.storeBlueprint(packName, compound, Minecraft.getInstance().gameDirectory.toPath() - .resolve(BLUEPRINT_FOLDER) - .resolve(packName.toLowerCase(Locale.US)) - .resolve(SCANS_FOLDER).resolve(fileName), provider)); + StructurePacks.storeBlueprint(packName, compound, targetPath, provider)); RenderingCache.getOrCreateBlueprintPreviewData("blueprint").setPos(null); Minecraft.getInstance().player.displayClientMessage(Component.translatable("Scan successfully saved as %s", fileName), false); } diff --git a/src/main/java/com/ldtteam/structurize/storage/StructurePacks.java b/src/main/java/com/ldtteam/structurize/storage/StructurePacks.java index 97a417a4e..c20d9931e 100644 --- a/src/main/java/com/ldtteam/structurize/storage/StructurePacks.java +++ b/src/main/java/com/ldtteam/structurize/storage/StructurePacks.java @@ -3,11 +3,13 @@ import com.google.gson.internal.Streams; import com.google.gson.stream.JsonReader; import com.ldtteam.structurize.api.Log; +import com.ldtteam.structurize.api.Utils; import com.ldtteam.structurize.api.constants.Constants; import com.ldtteam.structurize.blueprints.v1.Blueprint; import com.ldtteam.structurize.blueprints.v1.BlueprintUtil; import com.ldtteam.structurize.util.IOPool; import com.ldtteam.structurize.util.ManualBarrier; +import net.minecraft.client.Minecraft; import net.minecraft.core.HolderLookup; import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.NbtAccounter; @@ -16,6 +18,7 @@ import java.io.*; import java.nio.file.Files; +import java.nio.file.InvalidPathException; import java.nio.file.Path; import java.util.*; import java.util.concurrent.CompletableFuture; @@ -456,17 +459,36 @@ public static byte[] getBlueprintData(final String structurePackId, final String return null; } + final String packName = Utils.getSafePackName(structurePackId); + if (packName.isEmpty()) + { + return null; + } final StructurePackMeta packMeta = getStructurePack(structurePackId); if (packMeta == null) { return null; } + final Path packRoot = packMeta.getPath().toAbsolutePath().normalize(); + final Path target; + try + { + target = packRoot.resolve(packMeta.getNormalizedSubPath(subPath)).normalize(); + } + catch (final IllegalArgumentException e) + { + return null; + } - //todo, here similarly as in the other places we could query a remote server for this if we don't have it locally. + if (!target.startsWith(packRoot) || !target.getFileName().toString().endsWith(".blueprint")) + { + Log.getLogger().warn("Rejected blueprint request outside pack {}: {}", packMeta.getName(), subPath); + return null; + } try { - return Files.readAllBytes(packMeta.getPath().resolve(packMeta.getNormalizedSubPath(subPath))); + return Files.readAllBytes(target); } catch (final IOException e) {