[DC-944] Precedence problem in SQL generation allows bypass of pending joins Created: 03/Dec/10 Updated: 10/Dec/11 |
|
| Status: | Open |
| Project: | Doctrine 1 |
| Component/s: | Query |
| Affects Version/s: | 1.2.3 |
| Fix Version/s: | 1.2.4 |
| Type: | Bug | Priority: | Major |
| Reporter: | Walter Hop | Assignee: | Guilherme Blanco |
| Resolution: | Unresolved | Votes: | 0 |
| Labels: | None | ||
| Environment: |
PHP 5.2, 5.3 |
||
| Attachments: |
|
| Description |
|
'Pending join conditions' are used by listeners to inject extra SQL conditions into a query. They are often used to add basic constraints on every query. An example is the bundled SoftDelete template. Its listener adds extra constraints such as s.deleted_at IS NULL to a query, to make sure that deleted rows are never retrieved on a query. However, in the emitted SQL, Doctrine_Query does not use parentheses to group normal SQL conditions together. The pending join condition is simply added to the string without encapsulating existing expressions. This makes it possible to bypass the pending join conditions entirely by using the OR operator. Example For instance, the following query exhibits this problem:
This query emits the following SQL:
which returns also a deleted row. Expected behavior One would expect the pending join conditions always to hold, and to have precedence over regularly added SQL conditions. This could be accomplished in the most simple fashion by:
As the existing expressions are now encapsulated by parentheses, it is no longer possible to bypass the pending join conditions injected by the query listener. Full test case details: init.sql create database softdelete;
grant all privileges on softdelete.* to softdelete@localhost identified by 'uahwqeruwer';
use softdelete;
CREATE TABLE soft_delete_test (name VARCHAR(255),
deleted_at DATETIME DEFAULT NULL,
PRIMARY KEY(name)) ENGINE = INNODB;
insert into soft_delete_test values ('fine', null);
insert into soft_delete_test values ('faulty', now());
run.php <?php
require "./1.2.3/lib/Doctrine.php";
spl_autoload_register(array('Doctrine', 'autoload'));
require "SoftDeleteTest.php";
$conn = Doctrine_Manager::connection("mysql://softdelete:uahwqeruwer@localhost/softdelete");
$conn->setAttribute(Doctrine::ATTR_USE_DQL_CALLBACKS, true);
$query = Doctrine_Query::create()
->from("SoftDeleteTest")
->where("name=?", "faulty")
->orWhere("name=?", "faulty");
$found = $query->execute();
foreach ($found as $f) {
echo "ERROR! Found a deleted row: $f->name\n";
}
echo "Done.\n";
SoftDeleteTest.php (copied from Doctrine manual) <?php
class SoftDeleteTest extends Doctrine_Record
{
public function setTableDefinition()
{
$this->hasColumn('name', 'string', null, array(
'primary' => true
)
);
}
public function setUp()
{
$this->actAs('SoftDelete');
}
}
|
| Comments |
| Comment by Walter Hop [ 03/Dec/10 ] |
|
Fixing quote formatting |
| Comment by Walter Hop [ 03/Dec/10 ] |
|
Final formatting fixes. |