Skip to content
Closed
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
@@ -1,7 +1,5 @@
package com.onarandombox.MultiverseCore;

import java.util.Map;

import com.dumptruckman.minecraft.util.Logging;
import com.onarandombox.MultiverseCore.api.MultiverseCoreConfig;
import com.onarandombox.MultiverseCore.event.MVDebugModeEvent;
Expand All @@ -10,6 +8,8 @@
import me.main__.util.SerializationConfig.SerializationConfig;
import org.bukkit.Bukkit;

import java.util.Map;

/**
* Our configuration.
*/
Expand Down Expand Up @@ -77,6 +77,8 @@ public static MultiverseCoreConfiguration getInstance() {
private volatile boolean autopurge;
@Property
private volatile boolean idonotwanttodonate;
@Property
private volatile boolean allowunsafeworldname;

public MultiverseCoreConfiguration() {
super();
Expand Down Expand Up @@ -111,6 +113,7 @@ protected void setDefaults() {
portalsearchradius = 128;
autopurge = true;
idonotwanttodonate = false;
allowunsafeworldname = false;
// END CHECKSTYLE-SUPPRESSION: MagicNumberCheck
}

Expand Down Expand Up @@ -382,4 +385,14 @@ public boolean isShowingDonateMessage() {
public void setShowDonateMessage(boolean showDonateMessage) {
this.idonotwanttodonate = !showDonateMessage;
}

@Override
public boolean isAllowUnsafeWorldName() {
return allowunsafeworldname;
}

@Override
public void setAllowUnsafeWorldName(boolean allowUnsafeWorldName) {
this.allowunsafeworldname = allowUnsafeWorldName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -239,4 +239,8 @@ public interface MultiverseCoreConfig extends ConfigurationSerializable {
* @param idonotwanttodonate True if donation/patreon messages should be shown.
*/
void setShowDonateMessage(boolean idonotwanttodonate);

boolean isAllowUnsafeWorldName();

void setAllowUnsafeWorldName(boolean allowUnsafeWorldName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import com.onarandombox.MultiverseCore.MultiverseCore;
import com.onarandombox.MultiverseCore.api.MVWorldManager;
import com.onarandombox.MultiverseCore.utils.WorldNameChecker;
import com.pneumaticraft.commandhandler.CommandHandler;
import org.bukkit.ChatColor;
import org.bukkit.World.Environment;
Expand All @@ -17,7 +18,6 @@
import org.bukkit.command.CommandSender;
import org.bukkit.permissions.PermissionDefault;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -46,16 +46,10 @@ public CreateCommand(MultiverseCore plugin) {
this.addCommandExample("/mv create " + ChatColor.GOLD + "moonworld" + ChatColor.GREEN + " normal" + ChatColor.DARK_AQUA + " -g BukkitFullOfMoon");
this.worldManager = this.plugin.getMVWorldManager();
}

private String trimWorldName(String userInput) {
// Removes relative paths.
return userInput.replaceAll("^[./\\\\]+", "");
}


@Override
public void runCommand(CommandSender sender, List<String> args) {
String worldName = trimWorldName(args.get(0));
File worldFile = new File(this.plugin.getServer().getWorldContainer(), worldName);
String worldName = args.get(0);
String env = args.get(1);
String seed = CommandHandler.getFlag("-s", args);
String generator = CommandHandler.getFlag("-g", args);
Expand All @@ -71,20 +65,20 @@ public void runCommand(CommandSender sender, List<String> args) {
useSpawnAdjust = false;
}
}
// Make sure the world name doesn't contain the words 'plugins' and '.dat'
if(worldName.contains("plugins")||worldName.contains(".dat")){
sender.sendMessage(ChatColor.RED + "Multiverse cannot create a world that contains 'plugins' or '.dat'");

if(!this.plugin.getMVConfig().isAllowUnsafeWorldName() && !WorldNameChecker.isValidWorldName(worldName)) {
sender.sendMessage(ChatColor.RED + "Multiverse cannot create the world as the world name '"
+ worldName + "' contains spaces or invalid characters!");
return;
}

if (this.worldManager.isMVWorld(worldName)) {
sender.sendMessage(ChatColor.RED + "Multiverse cannot create " + ChatColor.GOLD + ChatColor.UNDERLINE
+ "another" + ChatColor.RESET + ChatColor.RED + " world named " + worldName);
return;
}

if (worldFile.exists()) {
if (WorldNameChecker.checkFolder(worldName) != WorldNameChecker.FolderStatus.DOES_NOT_EXIST) {
sender.sendMessage(ChatColor.RED + "A Folder/World already exists with this name!");
sender.sendMessage(ChatColor.RED + "If you are confident it is a world you can import with /mvimport");
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import org.bukkit.command.CommandSender;
import org.bukkit.permissions.PermissionDefault;

import java.io.File;
import java.util.Collection;
import java.util.List;

Expand Down Expand Up @@ -55,15 +54,10 @@ private String getPotentialWorldStrings() {

return worldList.toString();
}

private String trimWorldName(String userInput) {
// Removes relative paths.
return userInput.replaceAll("^[./\\\\]+", "");
}

@Override
public void runCommand(CommandSender sender, List<String> args) {
String worldName = trimWorldName(args.get(0));
String worldName = args.get(0);

if (worldName.toLowerCase().equals("--list") || worldName.toLowerCase().equals("-l")) {
String worldList = this.getPotentialWorldStrings();
Expand All @@ -82,11 +76,11 @@ public void runCommand(CommandSender sender, List<String> args) {
return;
}

// Make sure the world name doesn't contain the words 'plugins' and '.dat'
if(worldName.contains("plugins")||worldName.contains(".dat")){
sender.sendMessage(ChatColor.RED + "Multiverse cannot create a world that contains 'plugins' or '.dat'");
if(!this.plugin.getMVConfig().isAllowUnsafeWorldName() && !WorldNameChecker.isValidWorldName(worldName)) {
sender.sendMessage(ChatColor.RED + "Multiverse cannot import the world as the world name '"
+ worldName + "' contains spaces or invalid characters!");
return;
}
}

// Make sure we don't already know about this world.
if (this.worldManager.isMVWorld(worldName)) {
Expand All @@ -95,7 +89,18 @@ public void runCommand(CommandSender sender, List<String> args) {
return;
}

File worldFile = new File(this.plugin.getServer().getWorldContainer(), worldName);
switch (WorldNameChecker.checkFolder(worldName)) {
case DOES_NOT_EXIST:
sender.sendMessage(ChatColor.RED + "FAILED.");
sender.sendMessage(ChatColor.RED + "That world folder does not exist. These look like worlds to me:");
sender.sendMessage(this.getPotentialWorldStrings());
return;
case NOT_A_WORLD:
sender.sendMessage(ChatColor.RED + "FAILED.");
sender.sendMessage(String.format("%s'%s' does not appear to be a world. It is lacking a .dat file.",
ChatColor.RED, worldName));
return;
}

String generator = CommandHandler.getFlag("-g", args);
boolean useSpawnAdjust = true;
Expand All @@ -113,25 +118,18 @@ public void runCommand(CommandSender sender, List<String> args) {
return;
}

if (!worldFile.exists()) {
sender.sendMessage(ChatColor.RED + "FAILED.");
String worldList = this.getPotentialWorldStrings();
sender.sendMessage("That world folder does not exist. These look like worlds to me:");
sender.sendMessage(worldList);
} else if (!WorldNameChecker.isValidWorldFolder(worldFile)) {
sender.sendMessage(ChatColor.RED + "FAILED.");
sender.sendMessage(String.format("'%s' does not appear to be a world. It is lacking a .dat file.",
worldName));
} else if (env == null) {
if (env == null) {
sender.sendMessage(ChatColor.RED + "FAILED.");
sender.sendMessage("That world environment did not exist.");
sender.sendMessage("For a list of available world types, type: " + ChatColor.AQUA + "/mvenv");
return;
}

Command.broadcastCommandMessage(sender, String.format("Starting import of world '%s'...", worldName));
if (this.worldManager.addWorld(worldName, environment, null, null, null, generator, useSpawnAdjust)) {
Command.broadcastCommandMessage(sender, ChatColor.GREEN + "Complete!");
} else {
Command.broadcastCommandMessage(sender, String.format("Starting import of world '%s'...", worldName));
if (this.worldManager.addWorld(worldName, environment, null, null, null, generator, useSpawnAdjust))
Command.broadcastCommandMessage(sender, ChatColor.GREEN + "Complete!");
else
Command.broadcastCommandMessage(sender, ChatColor.RED + "Failed!");
Command.broadcastCommandMessage(sender, ChatColor.RED + "Failed!");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
import java.util.Stack;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
Expand Down Expand Up @@ -130,7 +129,11 @@ public boolean cloneWorld(String oldName, String newName) {
}

// Check for valid world name
if (!(WorldNameChecker.isValidWorldName(oldName) && WorldNameChecker.isValidWorldName(newName))) {
if (!this.plugin.getMVConfig().isAllowUnsafeWorldName() && !WorldNameChecker.isValidWorldName(newName)) {
Logging.warning("Unable to clone world as world name '" + newName + "' is invalid.");
Logging.warning("World name should not contain spaces or special characters!");
Logging.warning("Enable allowunsafeworldname to bypass the check if you are absolutely sure this world is valid.");
Logging.warning("However, MV is not response of any damage to your world if you enable this option.");
return false;
}

Expand Down Expand Up @@ -234,13 +237,12 @@ public boolean addWorld(String name, Environment env, String seedString, WorldTy
@Override
public boolean addWorld(String name, Environment env, String seedString, WorldType type, Boolean generateStructures,
String generator, boolean useSpawnAdjust) {
if (name.equalsIgnoreCase("plugins") || name.equalsIgnoreCase("logs")) {
return false;
}

if (!WorldNameChecker.isValidWorldName(name)) {
Logging.warning("Invalid world name '" + name + "'");
if (!this.plugin.getMVConfig().isAllowUnsafeWorldName() && !WorldNameChecker.isValidWorldName(name)) {
Logging.warning("Unable to add world as world name '" + name + "' is invalid.");
Logging.warning("World name should not contain spaces or special characters!");
Logging.warning("Enable allowunsafeworldname to bypass the check if you are absolutely sure this world is valid.");
Logging.warning("However, MV is not response of any damage to your world if you enable this option.");
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
public class WorldNameChecker {

private static final Pattern WORLD_NAME_PATTERN = Pattern.compile("[a-zA-Z0-9/._-]+");
private static final Pattern WORLD_NAME_PATTERN = Pattern.compile("[a-zA-Z0-9_-]+");
private static final Set<String> BLACKLIST_NAMES = Collections.unmodifiableSet(new HashSet<String>() {{
add("plugins");
add("logs");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,21 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentMatchers;
import org.mockito.Matchers;
import org.mockito.internal.verification.VerificationModeFactory;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import java.io.File;

import static junit.framework.Assert.*;
import static org.mockito.Mockito.*;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertTrue;
import static junit.framework.Assert.fail;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@RunWith(PowerMockRunner.class)
@PrepareForTest({ PluginManager.class, MultiverseCore.class, Permission.class, Bukkit.class, WorldManager.class,
Expand Down Expand Up @@ -92,7 +97,7 @@ public void testWorldImportWithNoFolder() {
// Import the first world. The world folder does not exist.
plugin.onCommand(mockCommandSender, mockCommand, "", normalArgs);
verify(mockCommandSender).sendMessage(ChatColor.RED + "FAILED.");
verify(mockCommandSender).sendMessage("That world folder does not exist. These look like worlds to me:");
verify(mockCommandSender).sendMessage(ChatColor.RED + "That world folder does not exist. These look like worlds to me:");

// We should still have no worlds.
assertEquals(0, creator.getCore().getMVWorldManager().getMVWorlds().size());
Expand Down