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
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -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(() ->
{
Expand Down Expand Up @@ -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);
}
Expand Down
26 changes: 24 additions & 2 deletions src/main/java/com/ldtteam/structurize/storage/StructurePacks.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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)
{
Expand Down
Loading