From 85b60f7cb6f0442c03ac645334468e99572a9fdd Mon Sep 17 00:00:00 2001 From: Minecrell Date: Tue, 24 Jul 2018 14:52:09 +0200 Subject: [PATCH] Extend validation of plugin descriptions --- .../pluginyml/bukkit/BukkitPlugin.kt | 17 +++++++++++------ .../pluginyml/bungee/BungeePlugin.kt | 9 ++------- .../pluginyml/nukkit/NukkitPlugin.kt | 19 ++++++------------- 3 files changed, 19 insertions(+), 26 deletions(-) diff --git a/src/main/kotlin/net/minecrell/pluginyml/bukkit/BukkitPlugin.kt b/src/main/kotlin/net/minecrell/pluginyml/bukkit/BukkitPlugin.kt index 87b2af7..b8b20f2 100644 --- a/src/main/kotlin/net/minecrell/pluginyml/bukkit/BukkitPlugin.kt +++ b/src/main/kotlin/net/minecrell/pluginyml/bukkit/BukkitPlugin.kt @@ -30,6 +30,10 @@ import org.gradle.api.Project class BukkitPlugin : PlatformPlugin("Bukkit", "plugin.yml") { + companion object { + @JvmStatic private val VALID_NAME = Regex("^[A-Za-z0-9 _.-]+$") + } + override fun createExtension(project: Project) = BukkitPluginDescription(project) override fun setDefaults(project: Project, description: BukkitPluginDescription) { @@ -41,13 +45,14 @@ class BukkitPlugin : PlatformPlugin("Bukkit", "plugin.y } override fun validate(description: BukkitPluginDescription) { - if (description.name == null) { - throw InvalidPluginDescriptionException("Plugin name is not set") - } + val name = description.name ?: throw InvalidPluginDescriptionException("Plugin name is not set") + if (!VALID_NAME.matches(name)) throw InvalidPluginDescriptionException("Invalid plugin name: should match $VALID_NAME") - if (description.main == null) { - throw InvalidPluginDescriptionException("Main class is not defined") - } + if (description.version.isNullOrEmpty()) throw InvalidPluginDescriptionException("Plugin version is not set") + + val main = description.main ?: throw InvalidPluginDescriptionException("Main class is not defined") + if (main.isEmpty()) throw InvalidPluginDescriptionException("Main class cannot be empty") + if (main.startsWith("org.bukkit.")) throw InvalidPluginDescriptionException("Main may not be within the org.bukkit namespace") } } diff --git a/src/main/kotlin/net/minecrell/pluginyml/bungee/BungeePlugin.kt b/src/main/kotlin/net/minecrell/pluginyml/bungee/BungeePlugin.kt index b54c35d..0c987fa 100644 --- a/src/main/kotlin/net/minecrell/pluginyml/bungee/BungeePlugin.kt +++ b/src/main/kotlin/net/minecrell/pluginyml/bungee/BungeePlugin.kt @@ -40,13 +40,8 @@ class BungeePlugin : PlatformPlugin("Bungee", "bungee.y } override fun validate(description: BungeePluginDescription) { - if (description.name == null) { - throw InvalidPluginDescriptionException("Plugin name is not set") - } - - if (description.main == null) { - throw InvalidPluginDescriptionException("Main class is not defined") - } + if (description.name.isNullOrEmpty()) throw InvalidPluginDescriptionException("Plugin name is not set") + if (description.main.isNullOrEmpty()) throw InvalidPluginDescriptionException("Main class is not defined") } } diff --git a/src/main/kotlin/net/minecrell/pluginyml/nukkit/NukkitPlugin.kt b/src/main/kotlin/net/minecrell/pluginyml/nukkit/NukkitPlugin.kt index 48545f9..51d4bfd 100644 --- a/src/main/kotlin/net/minecrell/pluginyml/nukkit/NukkitPlugin.kt +++ b/src/main/kotlin/net/minecrell/pluginyml/nukkit/NukkitPlugin.kt @@ -40,21 +40,14 @@ class NukkitPlugin : PlatformPlugin("Nukkit", "nukkit.y } override fun validate(description: NukkitPluginDescription) { - if (description.name == null) { - throw InvalidPluginDescriptionException("Plugin name is not set") - } + if (description.name.isNullOrEmpty()) throw InvalidPluginDescriptionException("Plugin name is not set") + if (description.version.isNullOrEmpty()) throw InvalidPluginDescriptionException("Plugin version class is not set") - if (description.main == null) { - throw InvalidPluginDescriptionException("Main class is not defined") - } + val main = description.main ?: throw InvalidPluginDescriptionException("Main class is not defined") + if (main.isEmpty()) throw InvalidPluginDescriptionException("Main class cannot be empty") + if (main.startsWith("cn.nukkit.")) throw InvalidPluginDescriptionException("Main class cannot be within cn.nukkit. package") - if (description.version == null) { - throw InvalidPluginDescriptionException("Plugin version class is not set") - } - - if (description.api == null || description.api!!.isEmpty()) { - throw InvalidPluginDescriptionException("Nukkit API version is not set") - } + if (description.api?.isEmpty() != false) throw InvalidPluginDescriptionException("Nukkit API version is not set") } } \ No newline at end of file