Better handle invalid warp names

This commit is contained in:
KHobbits 2012-10-02 00:19:42 +01:00
parent 732dc3bf34
commit 147a2fc227
5 changed files with 26 additions and 9 deletions

View file

@ -344,6 +344,11 @@ public class EssentialsConf extends YamlConfiguration
}
}
public void saveWithError() throws IOException
{
save(configFile);
}
@Override
public synchronized void save(final File file) throws IOException
{

View file

@ -161,7 +161,7 @@ public abstract class UserData extends PlayerExtension implements IConf
public void setHome(String name, Location loc)
{
//Invalid names will corrupt the yaml
name = Util.sanitizeFileName(name);
name = Util.safeString(name);
homes.put(name, loc);
config.setProperty("homes." + name, loc);
config.save();
@ -172,7 +172,7 @@ public abstract class UserData extends PlayerExtension implements IConf
String search = getHomeName(name);
if (!homes.containsKey(search))
{
search = Util.sanitizeFileName(search);
search = Util.safeString(search);
}
if (homes.containsKey(search))
{

View file

@ -23,16 +23,20 @@ public class Util
private final static Logger logger = Logger.getLogger("Minecraft");
private final static Pattern INVALIDFILECHARS = Pattern.compile("[^a-z0-9]");
private final static Pattern INVALIDCHARS = Pattern.compile("[^\t\n\r\u0020-\u007E\u0085\u00A0-\uD7FF\uE000-\uFFFC]");
private final static Pattern BADFILENAMES = Pattern.compile("^(CON|PRN|AUX|NUL|COM[1-9]|LPT[1-9])(\\.(.+))?$", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE | Pattern.COMMENTS);
//Used to clean file names before saving to disk
public static String sanitizeFileName(final String name)
{
String newName = INVALIDFILECHARS.matcher(name.toLowerCase(Locale.ENGLISH)).replaceAll("_");
if(BADFILENAMES.matcher(newName).matches())
newName = "_" + newName;
return newName;
return safeString(name);
}
//Used to clean strings/names before saving as filenames/permissions
public static String safeString(final String string)
{
return INVALIDFILECHARS.matcher(string.toLowerCase(Locale.ENGLISH)).replaceAll("_");
}
//Less restrictive string sanitizing, when not used as perm or filename
public static String sanitizeString(final String string)
{
return INVALIDCHARS.matcher(string).replaceAll("");

View file

@ -3,6 +3,7 @@ package com.earth2me.essentials;
import static com.earth2me.essentials.I18n._;
import com.earth2me.essentials.commands.WarpNotFoundException;
import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
@ -70,7 +71,14 @@ public class Warps implements IConf
}
conf.setProperty(null, loc);
conf.setProperty("name", name);
conf.save();
try
{
conf.saveWithError();
}
catch (IOException ex)
{
throw new IOException(_("invalidWarpName"));
}
}
public void delWarp(String name) throws Exception

View file

@ -40,7 +40,7 @@ public class Commandsetwarp extends EssentialsCommand
{
}
if (warpLoc == null || user.isAuthorized("essentials.warp.overwrite." + Util.sanitizeFileName(args[0])))
if (warpLoc == null || user.isAuthorized("essentials.warp.overwrite." + Util.safeString(args[0])))
{
warps.setWarp(args[0], loc);
}