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
42 changed files with 3 additions and 164 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -40,10 +40,6 @@ class DumpMemoryCommand extends VanillaCommand{
} }
public function execute(CommandSender $sender, string $commandLabel, array $args){ 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); $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; return true;
} }

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -39,10 +39,6 @@ class SaveOffCommand extends VanillaCommand{
} }
public function execute(CommandSender $sender, string $commandLabel, array $args){ public function execute(CommandSender $sender, string $commandLabel, array $args){
if(!$this->testPermission($sender)){
return true;
}
$sender->getServer()->getWorldManager()->setAutoSave(false); $sender->getServer()->getWorldManager()->setAutoSave(false);
Command::broadcastCommandMessage($sender, KnownTranslationFactory::commands_save_disabled()); 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){ public function execute(CommandSender $sender, string $commandLabel, array $args){
if(!$this->testPermission($sender)){
return true;
}
$sender->getServer()->getWorldManager()->setAutoSave(true); $sender->getServer()->getWorldManager()->setAutoSave(true);
Command::broadcastCommandMessage($sender, KnownTranslationFactory::commands_save_enabled()); 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){ public function execute(CommandSender $sender, string $commandLabel, array $args){
if(!$this->testPermission($sender)){
return true;
}
if(count($args) === 0){ if(count($args) === 0){
throw new InvalidCommandSyntaxException(); throw new InvalidCommandSyntaxException();
} }

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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