Skip to content
This repository was archived by the owner on May 16, 2018. It is now read-only.
Merged
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
Improved regex for SQL group, order, from
  • Loading branch information
ezimuel committed Aug 18, 2014
commit d8785057e1d4c76600f8917c56e64edca41d6abf
6 changes: 3 additions & 3 deletions library/Zend/Db/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ public function group($spec)
}

foreach ($spec as $val) {
if (preg_match('/\(.*\)/', (string) $val)) {
if (preg_match('/^[\w]*\([^\)]*\)$/', (string) $val)) {
$val = new Zend_Db_Expr($val);
}
$this->_parts[self::GROUP][] = $val;
Expand Down Expand Up @@ -601,7 +601,7 @@ public function order($spec)
$val = trim($matches[1]);
$direction = $matches[2];
}
if (preg_match('/^[\w]*\(.*\)$/', $val)) {
if (preg_match('/^[\w]*\([^\)]*\)$/', $val)) {
$val = new Zend_Db_Expr($val);
}
$this->_parts[self::ORDER][] = array($val, $direction);
Expand Down Expand Up @@ -943,7 +943,7 @@ protected function _tableCols($correlationName, $cols, $afterCorrelationName = n
$alias = $m[2];
}
// Check for columns that look like functions and convert to Zend_Db_Expr
if (preg_match('/\(.*\)/', $col)) {
if (preg_match('/^[\w]*\([^\)]*\)$/', $col)) {
$col = new Zend_Db_Expr($col);
} elseif (preg_match('/(.+)\.(.+)/', $col, $m)) {
$currentCorrelationName = $m[1];
Expand Down
22 changes: 22 additions & 0 deletions tests/Zend/Db/Select/StaticTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,28 @@ public function testSqlInjectionWithOrder()
$select = $this->_db->select();
$select->from(array('p' => 'products'))->order('name;select;MD5(1)');
$this->assertEquals('SELECT "p".* FROM "products" AS "p" ORDER BY "name;select;MD5(1)" ASC', $select->assemble());

$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());
}

public function testSqlInjectionWithGroup()
{
$select = $this->_db->select();
$select->from(array('p' => 'products'))->group('ABS("weight")');
$this->assertEquals('SELECT "p".* FROM "products" AS "p" GROUP BY ABS("weight")', $select->assemble());

$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());
}

public function testSqlInjectionInColumn()
{
$select = $this->_db->select();
$select->from(array('p' => 'products'), array('MD5(1); drop table products; -- )'));
$this->assertEquals('SELECT "p"."MD5(1); drop table products; -- )" FROM "products" AS "p"', $select->assemble());
}

/**
Expand Down