Skip to content

fix: encapsulation violation in BasePreparedQuery class #9603

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
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
16 changes: 13 additions & 3 deletions system/Database/BaseConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -652,9 +652,7 @@ public function query(string $sql, $binds = null, bool $setEscapeFlags = true, s
$query->setDuration($startTime, $startTime);

// This will trigger a rollback if transactions are being used
if ($this->transDepth !== 0) {
$this->transStatus = false;
}
$this->handleTransStatus();

if (
$this->DBDebug
Expand Down Expand Up @@ -904,6 +902,18 @@ public function resetTransStatus(): static
return $this;
}

/**
* Handle transaction status when a query fails
*
* @internal This method is for internal database component use only
*/
public function handleTransStatus(): void
{
if ($this->transDepth !== 0) {
$this->transStatus = false;
}
}

/**
* Begin Transaction
*/
Expand Down
4 changes: 1 addition & 3 deletions system/Database/BasePreparedQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,7 @@ public function execute(...$data)
$query->setDuration($startTime, $startTime);

// This will trigger a rollback if transactions are being used
if ($this->db->transDepth !== 0) {
$this->db->transStatus = false;
}
$this->db->handleTransStatus();

if ($this->db->DBDebug) {
// We call this function in order to roll-back queries
Expand Down
40 changes: 40 additions & 0 deletions tests/system/Database/Live/PreparedQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,4 +304,44 @@ public function testInsertBinaryData(): void

$this->assertSame(strlen($fileContent), strlen($file));
}

public function testHandleTransStatusMarksTransactionFailedDuringTransaction(): void
{
$this->db->transStart();

// Verify we're in a transaction
$this->assertSame(1, $this->db->transDepth);

// Prepare a query that will fail (duplicate key)
$this->query = $this->db->prepare(static fn ($db) => $db->table('without_auto_increment')->insert([
'key' => 'a',
'value' => 'b',
]));

$this->disableDBDebug();

$this->assertTrue($this->query->execute('test_key', 'test_value'));
$this->assertTrue($this->db->transStatus());

$this->seeInDatabase($this->db->DBPrefix . 'without_auto_increment', [
'key' => 'test_key',
'value' => 'test_value'
]);

$this->assertFalse($this->query->execute('test_key', 'different_value'));
$this->assertFalse($this->db->transStatus());

$this->enableDBDebug();

// Complete the transaction - should rollback due to failed status
$this->assertFalse($this->db->transComplete());

// Verify the first insert was rolled back
$this->dontSeeInDatabase($this->db->DBPrefix . 'without_auto_increment', [
'key' => 'test_key',
'value' => 'test_value'
]);

$this->db->resetTransStatus();
}
}
1 change: 1 addition & 0 deletions user_guide_src/source/changelogs/v4.6.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Bugs Fixed

- **Cache:** Fixed a bug where a corrupted or unreadable cache file could cause an unhandled exception in ``FileHandler::getItem()``.
- **Database:** Fixed a bug where ``when()`` and ``whenNot()`` in ``ConditionalTrait`` incorrectly evaluated certain falsy values (such as ``[]``, ``0``, ``0.0``, and ``'0'``) as truthy, causing callbacks to be executed unexpectedly. These methods now cast the condition to a boolean using ``(bool)`` to ensure consistent behavior with PHP's native truthiness.
- **Database:** Fixed encapsulation violation in ``BasePreparedQuery`` when accessing ``BaseConnection::transStatus`` protected property.
- **Email:** Fixed a bug where ``Email::getHostname()`` failed to use ``$_SERVER['SERVER_ADDR']`` when ``$_SERVER['SERVER_NAME']`` was not set.
- **Security:** Fixed a bug where the ``sanitize_filename()`` function from the Security helper would throw an error when used in CLI requests.
- **Session:** Fixed a bug where using the ``DatabaseHandler`` with an unsupported database driver (such as ``SQLSRV``, ``OCI8``, or ``SQLite3``) did not throw an appropriate error.
Expand Down
Loading