Skip to content

Commit 7ac1b54

Browse files
committed
Improve security of plugin.
1 parent 362d806 commit 7ac1b54

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

src/Controller/Admin/QueuedJobsController.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,13 +142,27 @@ public function import() {
142142
/** @var \Laminas\Diactoros\UploadedFile|null $file */
143143
$file = $this->request->getData('file');
144144
if ($file && $file->getError() == UPLOAD_ERR_OK && $file->getSize() > 0) {
145+
$clientMediaType = $file->getClientMediaType();
146+
if ($clientMediaType !== 'application/json') {
147+
throw new RuntimeException('Only JSON files are allowed');
148+
}
149+
145150
$content = file_get_contents($file->getStream()->getMetadata('uri'));
146151
if ($content === false) {
147152
throw new RuntimeException('Cannot parse file');
148153
}
154+
149155
$json = json_decode($content, true);
156+
if (json_last_error() !== JSON_ERROR_NONE) {
157+
throw new RuntimeException('Invalid JSON: ' . json_last_error_msg());
158+
}
159+
150160
if (!$json || empty($json['queuedJob'])) {
151-
throw new RuntimeException('Invalid JSON content');
161+
throw new RuntimeException('Invalid JSON content: missing queuedJob data');
162+
}
163+
164+
if (!is_array($json['queuedJob'])) {
165+
throw new RuntimeException('Invalid JSON structure: queuedJob must be an array');
152166
}
153167

154168
$data = $json['queuedJob'];

src/Model/Table/QueuedJobsTable.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -983,7 +983,7 @@ public function key(): string {
983983
if ($this->_key !== null) {
984984
return $this->_key;
985985
}
986-
$this->_key = sha1(microtime() . mt_rand(100, 999));
986+
$this->_key = bin2hex(random_bytes(32));
987987
if (!$this->_key) {
988988
throw new RuntimeException('Invalid key generated');
989989
}

src/Queue/Task/ExecuteTask.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
use Cake\Console\CommandInterface;
1212
use Cake\Console\ConsoleIo;
13+
use Cake\Core\Configure;
1314
use Cake\Log\LogTrait;
1415
use Queue\Model\QueueException;
1516
use Queue\Queue\AddInterface;
@@ -85,6 +86,10 @@ public function run(array $data, int $jobId): void {
8586
'accepted' => [CommandInterface::CODE_SUCCESS],
8687
];
8788

89+
if (!$data['escape'] && !Configure::read('debug')) {
90+
throw new QueueException('Command escaping must be enabled when debug mode is off for security reasons');
91+
}
92+
8893
$command = $data['command'];
8994
if ($data['escape']) {
9095
$command = escapeshellcmd($data['command']);

0 commit comments

Comments
 (0)