mirror of
https://github.com/AuthMe/AuthMeReloaded.git
synced 2024-11-23 18:55:11 +01:00
Add utility for safe pattern compiling
This commit is contained in:
parent
54ababdd28
commit
6812cfa4db
@ -15,6 +15,7 @@ import fr.xephi.authme.settings.properties.RegistrationSettings;
|
||||
import fr.xephi.authme.settings.properties.RestrictionSettings;
|
||||
import fr.xephi.authme.util.BukkitService;
|
||||
import fr.xephi.authme.util.StringUtils;
|
||||
import fr.xephi.authme.util.Utils;
|
||||
import fr.xephi.authme.util.ValidationService;
|
||||
import org.bukkit.Server;
|
||||
import org.bukkit.entity.Player;
|
||||
@ -56,13 +57,7 @@ class OnJoinVerifier implements Reloadable {
|
||||
@Override
|
||||
public void reload() {
|
||||
String nickRegEx = settings.getProperty(RestrictionSettings.ALLOWED_NICKNAME_CHARACTERS);
|
||||
try {
|
||||
nicknamePattern = Pattern.compile(nickRegEx);
|
||||
} catch (Exception e) {
|
||||
nicknamePattern = Pattern.compile(".*?");
|
||||
ConsoleLogger.showError("Nickname pattern is not a valid regular expression! "
|
||||
+ "Fallback to allowing all nicknames");
|
||||
}
|
||||
nicknamePattern = Utils.safePatternCompile(nickRegEx);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -10,6 +10,7 @@ import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* Utility class for various operations used in the codebase.
|
||||
@ -79,6 +80,15 @@ public final class Utils {
|
||||
}
|
||||
}
|
||||
|
||||
public static Pattern safePatternCompile(String pattern) {
|
||||
try {
|
||||
return Pattern.compile(pattern);
|
||||
} catch (Exception e) {
|
||||
ConsoleLogger.showError("Failed to compile pattern '" + pattern + "' - defaulting to allowing everything");
|
||||
return Pattern.compile(".*?");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the IP of the given player.
|
||||
*
|
||||
|
@ -38,7 +38,7 @@ public class ValidationService implements Reloadable {
|
||||
|
||||
@Override
|
||||
public void reload() {
|
||||
passwordRegex = Pattern.compile(settings.getProperty(RestrictionSettings.ALLOWED_PASSWORD_REGEX));
|
||||
passwordRegex = Utils.safePatternCompile(settings.getProperty(RestrictionSettings.ALLOWED_PASSWORD_REGEX));
|
||||
}
|
||||
|
||||
/**
|
||||
|
93
src/test/java/fr/xephi/authme/util/UtilsTest.java
Normal file
93
src/test/java/fr/xephi/authme/util/UtilsTest.java
Normal file
@ -0,0 +1,93 @@
|
||||
package fr.xephi.authme.util;
|
||||
|
||||
import fr.xephi.authme.TestHelper;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.UUID;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* Test for {@link Utils}.
|
||||
*/
|
||||
public class UtilsTest {
|
||||
|
||||
@BeforeClass
|
||||
public static void setAuthmeInstance() {
|
||||
TestHelper.setupLogger();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldCompilePattern() {
|
||||
// given
|
||||
String pattern = "gr(a|e)ys?";
|
||||
|
||||
// when
|
||||
Pattern result = Utils.safePatternCompile(pattern);
|
||||
|
||||
// then
|
||||
assertThat(result.toString(), equalTo(pattern));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldDefaultToAllAllowedPattern() {
|
||||
// given
|
||||
String invalidPattern = "gr(a|eys?"; // missing closing ')'
|
||||
|
||||
// when
|
||||
Pattern result = Utils.safePatternCompile(invalidPattern);
|
||||
|
||||
// then
|
||||
assertThat(result.toString(), equalTo(".*?"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldGetPlayerIp() {
|
||||
// given
|
||||
Player player = mock(Player.class);
|
||||
String ip = "124.86.248.62";
|
||||
TestHelper.mockPlayerIp(player, ip);
|
||||
|
||||
// when
|
||||
String result = Utils.getPlayerIp(player);
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo(ip));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldGetUuid() {
|
||||
// given
|
||||
UUID uuid = UUID.randomUUID();
|
||||
Player player = mock(Player.class);
|
||||
given(player.getUniqueId()).willReturn(uuid);
|
||||
|
||||
// when
|
||||
String result = Utils.getUUIDorName(player);
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo(uuid.toString()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldFallbackToName() {
|
||||
// given
|
||||
Player player = mock(Player.class);
|
||||
doThrow(RuntimeException.class).when(player).getUniqueId();
|
||||
String name = "Bobby12";
|
||||
given(player.getName()).willReturn(name);
|
||||
|
||||
// when
|
||||
String result = Utils.getUUIDorName(player);
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo(name));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user