Skip to content

Commit

Permalink
Fixed potential SQL injections in order() and group()
Browse files Browse the repository at this point in the history
  • Loading branch information
ezimuel authored and weierophinney committed Jul 13, 2016
1 parent d2560a5 commit bf3f406
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
8 changes: 5 additions & 3 deletions library/Zend/Db/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ class Zend_Db_Select
const SQL_ASC = 'ASC';
const SQL_DESC = 'DESC';

const REGEX_COLUMN_EXPR = '/^([\w]*\s*\(([^\(\)]|(?1))*\))$/';
const REGEX_COLUMN_EXPR = '/^([\w]*\s*\(([^\(\)]|(?1))*\))$/';
const REGEX_COLUMN_EXPR_ORDER = '/^([\w]+\s*\(([^\(\)]|(?1))*\))$/';
const REGEX_COLUMN_EXPR_GROUP = '/^([\w]+\s*\(([^\(\)]|(?1))*\))$/';

/**
* Bind variables for query
Expand Down Expand Up @@ -511,7 +513,7 @@ public function group($spec)
}

foreach ($spec as $val) {
if (preg_match(self::REGEX_COLUMN_EXPR, (string) $val)) {
if (preg_match(self::REGEX_COLUMN_EXPR_GROUP, (string) $val)) {
$val = new Zend_Db_Expr($val);
}
$this->_parts[self::GROUP][] = $val;
Expand Down Expand Up @@ -603,7 +605,7 @@ public function order($spec)
$val = trim($matches[1]);
$direction = $matches[2];
}
if (preg_match(self::REGEX_COLUMN_EXPR, (string) $val)) {
if (preg_match(self::REGEX_COLUMN_EXPR_ORDER, (string) $val)) {
$val = new Zend_Db_Expr($val);
}
$this->_parts[self::ORDER][] = array($val, $direction);
Expand Down
8 changes: 8 additions & 0 deletions tests/Zend/Db/Select/StaticTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -834,6 +834,10 @@ public function testSqlInjectionWithOrder()
$select = $this->_db->select();
$select->from(array('p' => 'products'))->order('MD5(1);drop table products; -- )');
$this->assertEquals('SELECT "p".* FROM "products" AS "p" ORDER BY "MD5(1);drop table products; -- )" ASC', $select->assemble());

$select = $this->_db->select();
$select->from('p')->order("MD5(\";(\");DELETE FROM p2; SELECT 1 #)");
$this->assertEquals('SELECT "p".* FROM "p" ORDER BY "MD5("";("");DELETE FROM p2; SELECT 1 #)" ASC', $select->assemble());
}

public function testSqlInjectionWithGroup()
Expand All @@ -845,6 +849,10 @@ public function testSqlInjectionWithGroup()
$select = $this->_db->select();
$select->from(array('p' => 'products'))->group('MD5(1); drop table products; -- )');
$this->assertEquals('SELECT "p".* FROM "products" AS "p" GROUP BY "MD5(1); drop table products; -- )"', $select->assemble());

$select = $this->_db->select();
$select->from('p')->group("MD5(\";(\");DELETE FROM p2; SELECT 1 #)");
$this->assertEquals('SELECT "p".* FROM "p" GROUP BY "MD5("";("");DELETE FROM p2; SELECT 1 #)"', $select->assemble());
}

public function testSqlInjectionInColumn()
Expand Down

0 comments on commit bf3f406

Please sign in to comment.