Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add validation for main class and api version #52

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions src/main/kotlin/net/minecrell/pluginyml/bukkit/BukkitPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,15 @@ import org.gradle.api.artifacts.result.ResolvedComponentResult
class BukkitPlugin : PlatformPlugin<BukkitPluginDescription>("Bukkit", "plugin.yml") {

companion object {
@JvmStatic private val VALID_NAME = Regex("^[A-Za-z0-9 _.-]+$")
@JvmStatic
private val VALID_NAME = Regex("^[A-Za-z0-9 _.-]+$")

@JvmStatic
private val VALID_API_VERSION = Regex("^1\\.[0-9]+$")
rainbowdashlabs marked this conversation as resolved.
Show resolved Hide resolved

@JvmStatic
private val INVALID_NAMESPACES =
listOf("net.minecraft.", "org.bukkit.", "io.papermc.", "com.destroystokoyo.paper.", "org.spigotmc")
}

override fun createExtension(project: Project) = BukkitPluginDescription(project)
Expand All @@ -53,12 +61,25 @@ class BukkitPlugin : PlatformPlugin<BukkitPluginDescription>("Bukkit", "plugin.y
override fun validate(description: BukkitPluginDescription) {
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.apiVersion != null) {
val apiVersion = description.apiVersion!!
val splitVersion = apiVersion.split("\\.").map { v -> v.toInt() }
if (splitVersion.size == 2) {
if (!VALID_API_VERSION.matches(apiVersion)) throw InvalidPluginDescriptionException("Invalid api version: should match $VALID_API_VERSION")
if (apiVersion < "1.13") throw InvalidPluginDescriptionException("Invalid api version: should be at least 1.13")
} else if (splitVersion.size == 3) {
if (splitVersion[1] < 20) throw InvalidPluginDescriptionException("Invalid api version: Minor versions are not supported before 1.20.5")
if (splitVersion[1] == 20 && splitVersion[2] < 5) throw InvalidPluginDescriptionException("Invalid api version: Minor versions are not supported before 1.20.5")
} else {
throw InvalidPluginDescriptionException("Invalid api version: $VALID_API_VERSION")
}
}

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")
validateNamespace(main, "Main")

for (command in description.commands) {
if (command.name.contains(':')) throw InvalidPluginDescriptionException("Command '${command.name}' cannot contain ':'")
Expand All @@ -72,4 +93,11 @@ class BukkitPlugin : PlatformPlugin<BukkitPluginDescription>("Bukkit", "plugin.y
}
}

private fun validateNamespace(namespace: String, name: String) {
for (invalidNamespace in INVALID_NAMESPACES) {
if (namespace.startsWith(invalidNamespace)) {
throw InvalidPluginDescriptionException("$name may not be within the $invalidNamespace namespace")
}
}
}
}
Loading