SimpleCommandMap: Enforce command permissions by default (#4681)

this resolves many security issues, as well as removing a ton of boilerplate code.
It may be desirable to react to permission denied; this can be done by overriding Command->testPermission(), or by using setPermissionMessage() to set a custom permission denied message.
This commit is contained in:
Dylan T 2022-08-15 16:42:51 +01:00 committed by GitHub
parent 304bb84af2
commit 8e97e9dcda
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
42 changed files with 3 additions and 164 deletions

View File

@ -43,10 +43,6 @@ final class PluginCommand extends Command implements PluginOwned{
return false;
}
if(!$this->testPermission($sender)){
return false;
}
$success = $this->executor->onCommand($sender, $this, $commandLabel, $args);
if(!$success && $this->usageMessage !== ""){

View File

@ -202,7 +202,9 @@ class SimpleCommandMap implements CommandMap{
$target->timings->startTiming();
try{
$target->execute($sender, $sentCommandLabel, $args);
if($target->testPermission($sender)){
$target->execute($sender, $sentCommandLabel, $args);
}
}catch(InvalidCommandSyntaxException $e){
$sender->sendMessage($sender->getLanguage()->translate(KnownTranslationFactory::commands_generic_usage($target->getUsage())));
}finally{

View File

@ -45,10 +45,6 @@ class BanCommand extends VanillaCommand{
}
public function execute(CommandSender $sender, string $commandLabel, array $args){
if(!$this->testPermission($sender)){
return true;
}
if(count($args) === 0){
throw new InvalidCommandSyntaxException();
}

View File

@ -46,10 +46,6 @@ class BanIpCommand extends VanillaCommand{
}
public function execute(CommandSender $sender, string $commandLabel, array $args){
if(!$this->testPermission($sender)){
return true;
}
if(count($args) === 0){
throw new InvalidCommandSyntaxException();
}

View File

@ -47,10 +47,6 @@ class BanListCommand extends VanillaCommand{
}
public function execute(CommandSender $sender, string $commandLabel, array $args){
if(!$this->testPermission($sender)){
return true;
}
if(isset($args[0])){
$args[0] = strtolower($args[0]);
if($args[0] === "ips"){

View File

@ -51,10 +51,6 @@ class ClearCommand extends VanillaCommand{
}
public function execute(CommandSender $sender, string $commandLabel, array $args){
if(!$this->testPermission($sender)){
return true;
}
if(count($args) > 3){
throw new InvalidCommandSyntaxException();
}

View File

@ -42,10 +42,6 @@ class DefaultGamemodeCommand extends VanillaCommand{
}
public function execute(CommandSender $sender, string $commandLabel, array $args){
if(!$this->testPermission($sender)){
return true;
}
if(count($args) === 0){
throw new InvalidCommandSyntaxException();
}

View File

@ -45,10 +45,6 @@ class DeopCommand extends VanillaCommand{
}
public function execute(CommandSender $sender, string $commandLabel, array $args){
if(!$this->testPermission($sender)){
return true;
}
if(count($args) === 0){
throw new InvalidCommandSyntaxException();
}

View File

@ -43,10 +43,6 @@ class DifficultyCommand extends VanillaCommand{
}
public function execute(CommandSender $sender, string $commandLabel, array $args){
if(!$this->testPermission($sender)){
return true;
}
if(count($args) !== 1){
throw new InvalidCommandSyntaxException();
}

View File

@ -40,10 +40,6 @@ class DumpMemoryCommand extends VanillaCommand{
}
public function execute(CommandSender $sender, string $commandLabel, array $args){
if(!$this->testPermission($sender)){
return true;
}
$sender->getServer()->getMemoryManager()->dumpServerMemory($args[0] ?? (Path::join($sender->getServer()->getDataPath(), "memory_dumps", date("D_M_j-H.i.s-T_Y"))), 48, 80);
return true;
}

View File

@ -46,10 +46,6 @@ class EffectCommand extends VanillaCommand{
}
public function execute(CommandSender $sender, string $commandLabel, array $args){
if(!$this->testPermission($sender)){
return true;
}
if(count($args) < 2){
throw new InvalidCommandSyntaxException();
}

View File

@ -44,10 +44,6 @@ class EnchantCommand extends VanillaCommand{
}
public function execute(CommandSender $sender, string $commandLabel, array $args){
if(!$this->testPermission($sender)){
return true;
}
if(count($args) < 2){
throw new InvalidCommandSyntaxException();
}

View File

@ -45,10 +45,6 @@ class GamemodeCommand extends VanillaCommand{
}
public function execute(CommandSender $sender, string $commandLabel, array $args){
if(!$this->testPermission($sender)){
return true;
}
if(count($args) === 0){
throw new InvalidCommandSyntaxException();
}

View File

@ -43,10 +43,6 @@ class GarbageCollectorCommand extends VanillaCommand{
}
public function execute(CommandSender $sender, string $commandLabel, array $args){
if(!$this->testPermission($sender)){
return true;
}
$chunksCollected = 0;
$entitiesCollected = 0;

View File

@ -51,10 +51,6 @@ class GiveCommand extends VanillaCommand{
}
public function execute(CommandSender $sender, string $commandLabel, array $args){
if(!$this->testPermission($sender)){
return true;
}
if(count($args) < 2){
throw new InvalidCommandSyntaxException();
}

View File

@ -55,10 +55,6 @@ class HelpCommand extends VanillaCommand{
}
public function execute(CommandSender $sender, string $commandLabel, array $args){
if(!$this->testPermission($sender)){
return true;
}
if(count($args) === 0){
$commandName = "";
$pageNumber = 1;

View File

@ -47,10 +47,6 @@ class KickCommand extends VanillaCommand{
}
public function execute(CommandSender $sender, string $commandLabel, array $args){
if(!$this->testPermission($sender)){
return true;
}
if(count($args) === 0){
throw new InvalidCommandSyntaxException();
}

View File

@ -47,10 +47,6 @@ class KillCommand extends VanillaCommand{
}
public function execute(CommandSender $sender, string $commandLabel, array $args){
if(!$this->testPermission($sender)){
return true;
}
if(count($args) >= 2){
throw new InvalidCommandSyntaxException();
}

View File

@ -45,10 +45,6 @@ class ListCommand extends VanillaCommand{
}
public function execute(CommandSender $sender, string $commandLabel, array $args){
if(!$this->testPermission($sender)){
return true;
}
$playerNames = array_map(function(Player $player) : string{
return $player->getName();
}, array_filter($sender->getServer()->getOnlinePlayers(), function(Player $player) use ($sender) : bool{

View File

@ -44,10 +44,6 @@ class MeCommand extends VanillaCommand{
}
public function execute(CommandSender $sender, string $commandLabel, array $args){
if(!$this->testPermission($sender)){
return true;
}
if(count($args) === 0){
throw new InvalidCommandSyntaxException();
}

View File

@ -45,10 +45,6 @@ class OpCommand extends VanillaCommand{
}
public function execute(CommandSender $sender, string $commandLabel, array $args){
if(!$this->testPermission($sender)){
return true;
}
if(count($args) === 0){
throw new InvalidCommandSyntaxException();
}

View File

@ -43,10 +43,6 @@ class PardonCommand extends VanillaCommand{
}
public function execute(CommandSender $sender, string $commandLabel, array $args){
if(!$this->testPermission($sender)){
return true;
}
if(count($args) !== 1){
throw new InvalidCommandSyntaxException();
}

View File

@ -44,10 +44,6 @@ class PardonIpCommand extends VanillaCommand{
}
public function execute(CommandSender $sender, string $commandLabel, array $args){
if(!$this->testPermission($sender)){
return true;
}
if(count($args) !== 1){
throw new InvalidCommandSyntaxException();
}

View File

@ -84,10 +84,6 @@ class ParticleCommand extends VanillaCommand{
}
public function execute(CommandSender $sender, string $commandLabel, array $args){
if(!$this->testPermission($sender)){
return true;
}
if(count($args) < 7){
throw new InvalidCommandSyntaxException();
}

View File

@ -47,10 +47,6 @@ class PluginsCommand extends VanillaCommand{
}
public function execute(CommandSender $sender, string $commandLabel, array $args){
if(!$this->testPermission($sender)){
return true;
}
$list = array_map(function(Plugin $plugin) : string{
return ($plugin->isEnabled() ? TextFormat::GREEN : TextFormat::RED) . $plugin->getDescription()->getFullName();
}, $sender->getServer()->getPluginManager()->getPlugins());

View File

@ -41,10 +41,6 @@ class SaveCommand extends VanillaCommand{
}
public function execute(CommandSender $sender, string $commandLabel, array $args){
if(!$this->testPermission($sender)){
return true;
}
Command::broadcastCommandMessage($sender, KnownTranslationFactory::pocketmine_save_start());
$start = microtime(true);

View File

@ -39,10 +39,6 @@ class SaveOffCommand extends VanillaCommand{
}
public function execute(CommandSender $sender, string $commandLabel, array $args){
if(!$this->testPermission($sender)){
return true;
}
$sender->getServer()->getWorldManager()->setAutoSave(false);
Command::broadcastCommandMessage($sender, KnownTranslationFactory::commands_save_disabled());

View File

@ -39,10 +39,6 @@ class SaveOnCommand extends VanillaCommand{
}
public function execute(CommandSender $sender, string $commandLabel, array $args){
if(!$this->testPermission($sender)){
return true;
}
$sender->getServer()->getWorldManager()->setAutoSave(true);
Command::broadcastCommandMessage($sender, KnownTranslationFactory::commands_save_enabled());

View File

@ -45,10 +45,6 @@ class SayCommand extends VanillaCommand{
}
public function execute(CommandSender $sender, string $commandLabel, array $args){
if(!$this->testPermission($sender)){
return true;
}
if(count($args) === 0){
throw new InvalidCommandSyntaxException();
}

View File

@ -39,10 +39,6 @@ class SeedCommand extends VanillaCommand{
}
public function execute(CommandSender $sender, string $commandLabel, array $args){
if(!$this->testPermission($sender)){
return true;
}
if($sender instanceof Player){
$seed = $sender->getPosition()->getWorld()->getSeed();
}else{

View File

@ -46,10 +46,6 @@ class SetWorldSpawnCommand extends VanillaCommand{
}
public function execute(CommandSender $sender, string $commandLabel, array $args){
if(!$this->testPermission($sender)){
return true;
}
if(count($args) === 0){
if($sender instanceof Player){
$location = $sender->getPosition();

View File

@ -47,10 +47,6 @@ class SpawnpointCommand extends VanillaCommand{
}
public function execute(CommandSender $sender, string $commandLabel, array $args){
if(!$this->testPermission($sender)){
return true;
}
$target = null;
if(count($args) === 0){

View File

@ -45,10 +45,6 @@ class StatusCommand extends VanillaCommand{
}
public function execute(CommandSender $sender, string $commandLabel, array $args){
if(!$this->testPermission($sender)){
return true;
}
$mUsage = Process::getAdvancedMemoryUsage();
$server = $sender->getServer();

View File

@ -39,10 +39,6 @@ class StopCommand extends VanillaCommand{
}
public function execute(CommandSender $sender, string $commandLabel, array $args){
if(!$this->testPermission($sender)){
return true;
}
Command::broadcastCommandMessage($sender, KnownTranslationFactory::commands_stop_start());
$sender->getServer()->shutdown();

View File

@ -59,10 +59,6 @@ class TeleportCommand extends VanillaCommand{
}
public function execute(CommandSender $sender, string $commandLabel, array $args){
if(!$this->testPermission($sender)){
return true;
}
switch(count($args)){
case 1: // /tp targetPlayer
case 3: // /tp x y z

View File

@ -47,10 +47,6 @@ class TellCommand extends VanillaCommand{
}
public function execute(CommandSender $sender, string $commandLabel, array $args){
if(!$this->testPermission($sender)){
return true;
}
if(count($args) < 2){
throw new InvalidCommandSyntaxException();
}

View File

@ -51,9 +51,6 @@ class TimeCommand extends VanillaCommand{
}
public function execute(CommandSender $sender, string $commandLabel, array $args){
if(!$this->testPermission($sender)){
return true;
}
if(count($args) < 1){
throw new InvalidCommandSyntaxException();
}

View File

@ -67,10 +67,6 @@ class TimingsCommand extends VanillaCommand{
}
public function execute(CommandSender $sender, string $commandLabel, array $args){
if(!$this->testPermission($sender)){
return true;
}
if(count($args) !== 1){
throw new InvalidCommandSyntaxException();
}

View File

@ -44,10 +44,6 @@ class TitleCommand extends VanillaCommand{
}
public function execute(CommandSender $sender, string $commandLabel, array $args){
if(!$this->testPermission($sender)){
return true;
}
if(count($args) < 2){
throw new InvalidCommandSyntaxException();
}

View File

@ -42,10 +42,6 @@ class TransferServerCommand extends VanillaCommand{
}
public function execute(CommandSender $sender, string $commandLabel, array $args){
if(!$this->testPermission($sender)){
return true;
}
if(count($args) < 1){
throw new InvalidCommandSyntaxException();
}elseif(!($sender instanceof Player)){

View File

@ -53,10 +53,6 @@ class VersionCommand extends VanillaCommand{
}
public function execute(CommandSender $sender, string $commandLabel, array $args){
if(!$this->testPermission($sender)){
return true;
}
if(count($args) === 0){
$sender->sendMessage(KnownTranslationFactory::pocketmine_command_version_serverSoftwareName(
VersionInfo::NAME

View File

@ -55,10 +55,6 @@ class WhitelistCommand extends VanillaCommand{
}
public function execute(CommandSender $sender, string $commandLabel, array $args){
if(!$this->testPermission($sender)){
return true;
}
if(count($args) === 1){
switch(strtolower($args[0])){
case "reload":