Skip to content
Open
Show file tree
Hide file tree
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
9 changes: 4 additions & 5 deletions src/Models/QueueJobModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,14 @@ private function setPriority(BaseBuilder $builder, array $priority): BaseBuilder
$builder->whereIn('priority', $priority);

Comment on lines 134 to 135

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add normalization:

Suggested change
$builder->whereIn('priority', $priority);
$priority = array_values($priority);
$builder->whereIn('priority', $priority);

Because array_map([$this->db, 'escape'], $priority) preserves string keys, a caller could pass an associative priority array and put unsafe SQL into the THEN position (for non-MySQL). Normal usage likely passes a list, but the model method accepts any array.

if ($priority !== ['default']) {
$escapedPriority = array_map([$this->db, 'escape'], $priority);

if ($this->db->DBDriver !== 'MySQLi') {
$builder->orderBy(
sprintf('CASE %s ', $this->db->protectIdentifiers('priority'))
. implode(
' ',
array_map(static fn ($value, $key) => "WHEN '{$value}' THEN {$key}", $priority, array_keys($priority)),
array_map(static fn ($value, $key) => "WHEN {$value} THEN {$key}", $escapedPriority, array_keys($escapedPriority)),
)
. ' END',
'',
Expand All @@ -148,10 +150,7 @@ private function setPriority(BaseBuilder $builder, array $priority): BaseBuilder
} else {
$builder->orderBy(
'FIELD(priority, '
. implode(
',',
array_map(static fn ($value) => "'{$value}'", $priority),
)
. implode(',', $escapedPriority)
. ')',
'',
false,
Expand Down
21 changes: 21 additions & 0 deletions tests/Models/QueueJobModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,25 @@ public function testSkipLockedFalse(): void

$this->assertSame($sql, $result);
}

public function testSetPriority(): void
{
$model = model(QueueJobModel::class);
$method = $this->getPrivateMethodInvoker($model, 'setPriority');
$builder = $model->builder();

$result = $method($builder, ['high', 'low']);

$sql = $result->getCompiledSelect();

$this->assertStringContainsString('priority', $sql);
if ($model->db->DBDriver === 'MySQLi') {
$this->assertStringContainsString('FIELD(priority, ', $sql);
} else {
$this->assertStringContainsString('CASE ', $sql);
$this->assertStringContainsString(' WHEN ', $sql);
$this->assertStringContainsString(' THEN ', $sql);
$this->assertStringContainsString(' END', $sql);
}
}
Comment on lines +63 to +82

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test seems a bit weak. It checks that SQL contains CASE/FIELD, but not that dangerous values are escaped or keys are normalized.

}