Skip to content

Commit 6e4a0c9

Browse files
committed
Validate "Command options" #222
1 parent b0656c8 commit 6e4a0c9

File tree

3 files changed

+23
-8
lines changed

3 files changed

+23
-8
lines changed

doc/commandsValidator.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,20 @@ If you want that all commands are available, change the $strategy to "all". Then
1515
$whiteList: ["router:match","valid:command"]
1616
$blackList: ["process-manager:maintenance","do-no-execute:command"]
1717
```
18+
19+
### Validating commands / command options before they get saved
20+
21+
In the pimcore admin you can set the "Commands options". By default there is no validation for the command options (the whole command is passed through "https://www.php.net/escapeshellcmd").
22+
If you need certain logic to validate the command options you could implement public methods in your command, which validates the options:
23+
24+
E.g:
25+
26+
```php
27+
28+
public function validatedCommandOptions(string $commandOptions, \Elements\Bundle\ProcessManagerBundle\Model\Configuration $configuration): void
29+
{
30+
# throw new \Exception('invalid command options');
31+
}
32+
```
33+
34+
Or you overwrite the "validateCommandConfiguration" method of the Elements\Bundle\ProcessManagerBundle\Service\CommandsValidator

src/Helper.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ public static function executeJob(string $configId, array $callbackSettings = []
7676
$item = $monitoringItem->save();
7777

7878
$command = $executor->getCommand($callbackSettings, $monitoringItem);
79+
$command = escapeshellcmd($command); //prevent os command injection
7980

8081
putenv(ElementsProcessManagerBundle::MONITORING_ITEM_ENV_VAR . '=' . $item->getId());
8182

src/Service/CommandsValidator.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
use Elements\Bundle\ProcessManagerBundle\ExecutionTrait;
1111
use Elements\Bundle\ProcessManagerBundle\Model\Configuration;
12-
use Exception;
1312
use Pimcore\Console\Application;
1413
use Symfony\Component\Console\Command\Command;
1514
use Symfony\Component\Console\Command\LazyCommand;
@@ -42,19 +41,17 @@ public function __construct(string $strategy = 'default', array $whiteList = [],
4241
$this->setBlackList($blackList);
4342
}
4443

45-
public function validateCommandConfiguration(LazyCommand | Command $command, Configuration $configuration): void
44+
public function validateCommandConfiguration(LazyCommand|Command $command, Configuration $configuration): void
4645
{
4746

4847
$settings = $configuration->getExecutorSettingsAsArray();
4948
$values = $settings['values'];
5049

5150
$commandOptions = $values['commandOptions'] ?? '';
5251

53-
//Todo: check if command options are valid
54-
//and throw an error if they are not valid
55-
56-
// throw new Exception('Command options are not valid');
57-
52+
if (is_callable([$command, 'validatedCommandOptions'])) {
53+
$command->validatedCommandOptions($commandOptions, $configuration);
54+
}
5855
}
5956

6057
/**
@@ -116,7 +113,7 @@ protected function getCommandsDefault(array $commands): array
116113
/**
117114
* @return array<string>
118115
*/
119-
protected function classUsesTraits(LazyCommand | Command $class, bool $autoload = true): array
116+
protected function classUsesTraits(LazyCommand|Command $class, bool $autoload = true): array
120117
{
121118
if ($class instanceof LazyCommand) {
122119
$class = $class->getCommand();

0 commit comments

Comments
 (0)