diff --git a/common/src/main/java/com/adamcalculator/dynamicpack/IDValidator.java b/common/src/main/java/com/adamcalculator/dynamicpack/IDValidator.java deleted file mode 100644 index a6cab54..0000000 --- a/common/src/main/java/com/adamcalculator/dynamicpack/IDValidator.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.adamcalculator.dynamicpack; - -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -public class IDValidator { - private static final Pattern PATTERN_ID = Pattern.compile("^[a-z0-9]{1,100}$"); - - public static boolean isValid(String input) { - Matcher matcher = PATTERN_ID.matcher(input); - return matcher.matches(); - } -} diff --git a/common/src/main/java/com/adamcalculator/dynamicpack/InputValidator.java b/common/src/main/java/com/adamcalculator/dynamicpack/InputValidator.java new file mode 100644 index 0000000..696f4ca --- /dev/null +++ b/common/src/main/java/com/adamcalculator/dynamicpack/InputValidator.java @@ -0,0 +1,24 @@ +package com.adamcalculator.dynamicpack; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class InputValidator { + private static final Pattern CONTENT_ID_PATTERN = Pattern.compile("^[a-z0-9_:]{2,128}$"); + + public static boolean isContentIdValid(String input) { + if (input == null) { + return false; + } + Matcher matcher = CONTENT_ID_PATTERN.matcher(input); + return matcher.matches(); + } + + public static boolean isPackNameValid(String input) { + if (input == null) { + return false; + } + + return input.trim().length() < 64 && !input.trim().isEmpty() && !input.contains("\n") && !input.contains("\r") && !input.contains("\b"); + } +} diff --git a/common/src/main/java/com/adamcalculator/dynamicpack/pack/DynamicRepoRemote.java b/common/src/main/java/com/adamcalculator/dynamicpack/pack/DynamicRepoRemote.java index f0eb91d..2800b06 100644 --- a/common/src/main/java/com/adamcalculator/dynamicpack/pack/DynamicRepoRemote.java +++ b/common/src/main/java/com/adamcalculator/dynamicpack/pack/DynamicRepoRemote.java @@ -1,6 +1,7 @@ package com.adamcalculator.dynamicpack.pack; import com.adamcalculator.dynamicpack.DynamicPackModBase; +import com.adamcalculator.dynamicpack.InputValidator; import com.adamcalculator.dynamicpack.Mod; import com.adamcalculator.dynamicpack.PackUtil; import com.adamcalculator.dynamicpack.sync.PackSyncProgress; @@ -12,7 +13,6 @@ import java.io.IOException; import java.nio.file.Path; -import java.rmi.RemoteException; import java.security.NoSuchAlgorithmException; import java.util.HashMap; import java.util.function.LongConsumer; @@ -105,11 +105,8 @@ public void onUpdate(FileDownloadConsumer it) { } String remoteName = repoJson.getString("name"); - if (remoteName.isBlank()) { - throw new RuntimeException("Name of remote pack can't be blank"); - } - if (remoteName.trim().length() > 50) { - throw new RuntimeException("Length of name pack can't > 50"); + if (!InputValidator.isPackNameValid(remoteName)) { + throw new RuntimeException("Remote name of pack not valid."); } diff --git a/common/src/main/java/com/adamcalculator/dynamicpack/pack/DynamicRepoSyncProcessV1.java b/common/src/main/java/com/adamcalculator/dynamicpack/pack/DynamicRepoSyncProcessV1.java index d9d5f30..66e5cd7 100644 --- a/common/src/main/java/com/adamcalculator/dynamicpack/pack/DynamicRepoSyncProcessV1.java +++ b/common/src/main/java/com/adamcalculator/dynamicpack/pack/DynamicRepoSyncProcessV1.java @@ -1,7 +1,7 @@ package com.adamcalculator.dynamicpack.pack; import com.adamcalculator.dynamicpack.DynamicPackModBase; -import com.adamcalculator.dynamicpack.IDValidator; +import com.adamcalculator.dynamicpack.InputValidator; import com.adamcalculator.dynamicpack.Mod; import com.adamcalculator.dynamicpack.PackUtil; import com.adamcalculator.dynamicpack.sync.PackSyncProgress; @@ -61,7 +61,7 @@ public void close() throws IOException { private void processContent(JSONObject jsonContent) throws IOException { String id = jsonContent.getString("id"); - if (!IDValidator.isValid(id)) { + if (!InputValidator.isContentIdValid(id)) { throw new RuntimeException("Id of content is not valid."); } diff --git a/common/src/test/java/tests/InputValidatorTest.java b/common/src/test/java/tests/InputValidatorTest.java new file mode 100644 index 0000000..8ddcf0d --- /dev/null +++ b/common/src/test/java/tests/InputValidatorTest.java @@ -0,0 +1,30 @@ +package tests; + +import com.adamcalculator.dynamicpack.InputValidator; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class InputValidatorTest { + + @Test + public void testContentId() { + Assertions.assertFalse(InputValidator.isContentIdValid("")); + Assertions.assertFalse(InputValidator.isContentIdValid(" ")); + Assertions.assertFalse(InputValidator.isContentIdValid(" ")); + Assertions.assertFalse(InputValidator.isContentIdValid(" 32")); + Assertions.assertFalse(InputValidator.isContentIdValid("test\ntest")); + + Assertions.assertTrue(InputValidator.isContentIdValid("__")); + Assertions.assertTrue(InputValidator.isContentIdValid("pack:megapack")); + Assertions.assertTrue(InputValidator.isContentIdValid("1234567890")); + Assertions.assertTrue(InputValidator.isContentIdValid("01")); + Assertions.assertTrue(InputValidator.isContentIdValid("test_pack")); + Assertions.assertTrue(InputValidator.isContentIdValid("super:mega_puper:")); + } + + @Test + public void testRemoteName() { + Assertions.assertFalse(InputValidator.isPackNameValid("\n")); + Assertions.assertTrue(InputValidator.isPackNameValid("__")); + } +}