Skip to content

Fix: Prevent SQL injection in QueueJobModel priority sorting#94

Open
gr8man wants to merge 1 commit into
codeigniter4:developfrom
gr8man:fix-queuejobmodel-priority-sqli
Open

Fix: Prevent SQL injection in QueueJobModel priority sorting#94
gr8man wants to merge 1 commit into
codeigniter4:developfrom
gr8man:fix-queuejobmodel-priority-sqli

Conversation

@gr8man

@gr8man gr8man commented Jul 8, 2026

Copy link
Copy Markdown

This PR fixes a potential SQL injection vulnerability in the QueueJobModel by properly escaping priority values before using them in CASE and FIELD SQL statements. A unit test was also added to ensure correct behavior.

@michalsn michalsn left a comment

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.

Thanks for the PR. Could you please adjust the PR title, description, and commit message to avoid framing this as a security vulnerability?

As discussed, the affected priority list is normally controlled by the application developer/operator through config or CLI, not by untrusted end users in normal usage. This is better described as hardening the database queue ordering logic.

Comment on lines 134 to 135
$builder->whereIn('priority', $priority);

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.

Comment on lines +63 to +82
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);
}
}

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants