From 6812cfa4db10298f9bf5dd45cd56472097387af6 Mon Sep 17 00:00:00 2001 From: ljacqu Date: Fri, 24 Jun 2016 23:50:11 +0200 Subject: [PATCH] Add utility for safe pattern compiling --- .../xephi/authme/listener/OnJoinVerifier.java | 9 +- src/main/java/fr/xephi/authme/util/Utils.java | 10 ++ .../xephi/authme/util/ValidationService.java | 2 +- .../java/fr/xephi/authme/util/UtilsTest.java | 93 +++++++++++++++++++ 4 files changed, 106 insertions(+), 8 deletions(-) create mode 100644 src/test/java/fr/xephi/authme/util/UtilsTest.java diff --git a/src/main/java/fr/xephi/authme/listener/OnJoinVerifier.java b/src/main/java/fr/xephi/authme/listener/OnJoinVerifier.java index 297e74c6c..38dd7d9c4 100644 --- a/src/main/java/fr/xephi/authme/listener/OnJoinVerifier.java +++ b/src/main/java/fr/xephi/authme/listener/OnJoinVerifier.java @@ -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); } /** diff --git a/src/main/java/fr/xephi/authme/util/Utils.java b/src/main/java/fr/xephi/authme/util/Utils.java index ae9406569..d26ae366e 100644 --- a/src/main/java/fr/xephi/authme/util/Utils.java +++ b/src/main/java/fr/xephi/authme/util/Utils.java @@ -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. * diff --git a/src/main/java/fr/xephi/authme/util/ValidationService.java b/src/main/java/fr/xephi/authme/util/ValidationService.java index 8e72f852a..615095d00 100644 --- a/src/main/java/fr/xephi/authme/util/ValidationService.java +++ b/src/main/java/fr/xephi/authme/util/ValidationService.java @@ -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)); } /** diff --git a/src/test/java/fr/xephi/authme/util/UtilsTest.java b/src/test/java/fr/xephi/authme/util/UtilsTest.java new file mode 100644 index 000000000..58af9b7c9 --- /dev/null +++ b/src/test/java/fr/xephi/authme/util/UtilsTest.java @@ -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)); + } +}