From 096279efd9bd42aee9b0836aee769d908b4f210f Mon Sep 17 00:00:00 2001 From: Bixilon Date: Mon, 21 Dec 2020 18:41:52 +0100 Subject: [PATCH] cssc: Check identifier against regex --- .codeclimate.yml | 6 --- .../data/commands/parser/CommandParsers.kt | 2 +- .../commands/parser/IdentifierListParser.kt | 9 ++++- .../data/commands/parser/IdentifierParser.kt | 38 +++++++++++++++++++ .../InvalidIdentifierCommandParseException.kt | 25 ++++++++++++ .../minosoft/data/mappings/ModIdentifier.java | 11 ++++++ .../protocol/protocol/ProtocolDefinition.java | 1 + 7 files changed, 84 insertions(+), 8 deletions(-) delete mode 100644 .codeclimate.yml create mode 100644 src/main/java/de/bixilon/minosoft/data/commands/parser/IdentifierParser.kt create mode 100644 src/main/java/de/bixilon/minosoft/data/commands/parser/exceptions/InvalidIdentifierCommandParseException.kt diff --git a/.codeclimate.yml b/.codeclimate.yml deleted file mode 100644 index 1299fc1bb..000000000 --- a/.codeclimate.yml +++ /dev/null @@ -1,6 +0,0 @@ -version: "2" # required to adjust maintainability checks -plugins: - sonar-java: - enabled: true - config: - sonar.java.source: "14" \ No newline at end of file diff --git a/src/main/java/de/bixilon/minosoft/data/commands/parser/CommandParsers.kt b/src/main/java/de/bixilon/minosoft/data/commands/parser/CommandParsers.kt index d948b38f8..57d761217 100644 --- a/src/main/java/de/bixilon/minosoft/data/commands/parser/CommandParsers.kt +++ b/src/main/java/de/bixilon/minosoft/data/commands/parser/CommandParsers.kt @@ -49,7 +49,7 @@ object CommandParsers { // swizzle // team // item_slot - // resource_location + ModIdentifier("resource_location") to IdentifierParser.IDENTIFIER_PARSER, ModIdentifier("mob_effect") to IdentifierListParser.MOB_EFFECT_PARSER, // function // entity_anchor diff --git a/src/main/java/de/bixilon/minosoft/data/commands/parser/IdentifierListParser.kt b/src/main/java/de/bixilon/minosoft/data/commands/parser/IdentifierListParser.kt index 4e1d4a40e..c996001a2 100644 --- a/src/main/java/de/bixilon/minosoft/data/commands/parser/IdentifierListParser.kt +++ b/src/main/java/de/bixilon/minosoft/data/commands/parser/IdentifierListParser.kt @@ -14,6 +14,7 @@ package de.bixilon.minosoft.data.commands.parser import de.bixilon.minosoft.data.commands.parser.exceptions.CommandParseException import de.bixilon.minosoft.data.commands.parser.exceptions.EnchantmentNotFoundCommandParseException +import de.bixilon.minosoft.data.commands.parser.exceptions.InvalidIdentifierCommandParseException import de.bixilon.minosoft.data.commands.parser.exceptions.MobEffectNotFoundCommandParseException import de.bixilon.minosoft.data.commands.parser.properties.ParserProperties import de.bixilon.minosoft.data.mappings.ModIdentifier @@ -26,7 +27,13 @@ class IdentifierListParser : CommandParser() { override fun isParsable(connection: Connection, properties: ParserProperties?, stringReader: ImprovedStringReader) { val argument = stringReader.readUntilNextCommandArgument() - val identifier = ModIdentifier(argument) + + val identifier: ModIdentifier + try { + identifier = ModIdentifier.getIdentifier(argument) + } catch (exception: IllegalArgumentException) { + throw InvalidIdentifierCommandParseException(stringReader, argument) + } if (this == ENCHANTMENT_PARSER) { if (!connection.mapping.doesEnchantmentExist(identifier)) { diff --git a/src/main/java/de/bixilon/minosoft/data/commands/parser/IdentifierParser.kt b/src/main/java/de/bixilon/minosoft/data/commands/parser/IdentifierParser.kt new file mode 100644 index 000000000..f287e1685 --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/data/commands/parser/IdentifierParser.kt @@ -0,0 +1,38 @@ +/* + * Minosoft + * Copyright (C) 2020 Moritz Zwerger + * + * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program.If not, see . + * + * This software is not affiliated with Mojang AB, the original developer of Minecraft. + */ +package de.bixilon.minosoft.data.commands.parser + +import de.bixilon.minosoft.data.commands.parser.exceptions.CommandParseException +import de.bixilon.minosoft.data.commands.parser.exceptions.InvalidIdentifierCommandParseException +import de.bixilon.minosoft.data.commands.parser.properties.ParserProperties +import de.bixilon.minosoft.data.mappings.ModIdentifier +import de.bixilon.minosoft.protocol.network.Connection +import de.bixilon.minosoft.util.buffers.ImprovedStringReader + +class IdentifierParser : CommandParser() { + + @Throws(CommandParseException::class) + override fun isParsable(connection: Connection, properties: ParserProperties?, stringReader: ImprovedStringReader) { + val argument = stringReader.readUntilNextCommandArgument() + + try { + ModIdentifier.getIdentifier(argument) + } catch (exception: IllegalArgumentException) { + throw InvalidIdentifierCommandParseException(stringReader, argument) + } + } + + companion object { + val IDENTIFIER_PARSER = IdentifierParser() + } +} diff --git a/src/main/java/de/bixilon/minosoft/data/commands/parser/exceptions/InvalidIdentifierCommandParseException.kt b/src/main/java/de/bixilon/minosoft/data/commands/parser/exceptions/InvalidIdentifierCommandParseException.kt new file mode 100644 index 000000000..5d7b5232d --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/data/commands/parser/exceptions/InvalidIdentifierCommandParseException.kt @@ -0,0 +1,25 @@ +/* + * Minosoft + * Copyright (C) 2020 Moritz Zwerger + * + * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program.If not, see . + * + * This software is not affiliated with Mojang AB, the original developer of Minecraft. + */ +package de.bixilon.minosoft.data.commands.parser.exceptions + +import de.bixilon.minosoft.util.buffers.ImprovedStringReader + +class InvalidIdentifierCommandParseException : CommandParseException { + constructor(command: ImprovedStringReader, currentArgument: String) : super(ERROR_MESSAGE, command, currentArgument) + + constructor(command: ImprovedStringReader, currentArgument: String, cause: Throwable) : super(ERROR_MESSAGE, command, currentArgument, cause) + + companion object { + private const val ERROR_MESSAGE = "Identifier is invalid!" + } +} diff --git a/src/main/java/de/bixilon/minosoft/data/mappings/ModIdentifier.java b/src/main/java/de/bixilon/minosoft/data/mappings/ModIdentifier.java index 78f24ec51..201b78a53 100644 --- a/src/main/java/de/bixilon/minosoft/data/mappings/ModIdentifier.java +++ b/src/main/java/de/bixilon/minosoft/data/mappings/ModIdentifier.java @@ -16,6 +16,7 @@ package de.bixilon.minosoft.data.mappings; import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition; import java.util.Objects; +import java.util.regex.Matcher; public class ModIdentifier { protected final String mod; @@ -42,6 +43,16 @@ public class ModIdentifier { this.identifier = identifier.getIdentifier(); } + public static ModIdentifier getIdentifier(String identifier) throws IllegalArgumentException { + Matcher matcher = ProtocolDefinition.IDENTIFIER_PATTERN.matcher(identifier); + if (!matcher.find() || !matcher.group().equals(identifier)) { + throw new IllegalArgumentException(String.format("%s in not a valid identifier!", identifier)); + } + + return new ModIdentifier(identifier); + + } + public String getMod() { return this.mod; } diff --git a/src/main/java/de/bixilon/minosoft/protocol/protocol/ProtocolDefinition.java b/src/main/java/de/bixilon/minosoft/protocol/protocol/ProtocolDefinition.java index f82e398e5..4e32e3bca 100644 --- a/src/main/java/de/bixilon/minosoft/protocol/protocol/ProtocolDefinition.java +++ b/src/main/java/de/bixilon/minosoft/protocol/protocol/ProtocolDefinition.java @@ -48,6 +48,7 @@ public final class ProtocolDefinition { public static final String COMMAND_SEPARATOR = " "; public static final Pattern MINECRAFT_NAME_VALIDATOR = Pattern.compile("\\w{3,16}"); + public static final Pattern IDENTIFIER_PATTERN = Pattern.compile("([a-z_]+:)?[a-z_]+"); public static final int SECTION_WIDTH_X = 16; public static final int SECTION_WIDTH_Z = 16;