Skip to content
This repository was archived by the owner on Oct 2, 2019. It is now read-only.

Commit ebf608f

Browse files
committed
Merge pull request zendframework#670 from ezimuel/fix/655
Fix for 655 issue - ZF2015-08 breaks binary data
2 parents 4b46967 + 70d8aba commit ebf608f

File tree

6 files changed

+62
-4
lines changed

6 files changed

+62
-4
lines changed

library/Zend/Db/Adapter/Pdo/Abstract.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,6 @@ protected function _quote($value)
292292
if (is_int($value) || is_float($value)) {
293293
return $value;
294294
}
295-
// Fix for null-byte injection
296-
$value = addcslashes($value, "\000\032");
297295
$this->_connect();
298296
return $this->_connection->quote($value);
299297
}

library/Zend/Db/Adapter/Pdo/Mssql.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,4 +420,19 @@ public function getServerVersion()
420420
return null;
421421
}
422422
}
423+
424+
/**
425+
* Quote a raw string.
426+
*
427+
* @param string $value Raw string
428+
* @return string Quoted string
429+
*/
430+
protected function _quote($value)
431+
{
432+
if (!is_int($value) && !is_float($value)) {
433+
// Fix for null-byte injection
434+
$value = addcslashes($value, "\000\032");
435+
}
436+
return parent::_quote($value);
437+
}
423438
}

library/Zend/Db/Adapter/Pdo/Sqlite.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,4 +294,18 @@ public function limit($sql, $count, $offset = 0)
294294
return $sql;
295295
}
296296

297+
/**
298+
* Quote a raw string.
299+
*
300+
* @param string $value Raw string
301+
* @return string Quoted string
302+
*/
303+
protected function _quote($value)
304+
{
305+
if (!is_int($value) && !is_float($value)) {
306+
// Fix for null-byte injection
307+
$value = addcslashes($value, "\000\032");
308+
}
309+
return parent::_quote($value);
310+
}
297311
}

tests/Zend/Db/Adapter/Pdo/MssqlTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,17 @@ public function testAdapterDescribeTableWithSchemaName()
361361
$this->assertArrayHasKey('product_name', $productsTableInfo);
362362
}
363363

364+
/**
365+
* test that quote() escapes null byte character
366+
* in a string.
367+
*/
368+
public function testAdapterQuoteNullByteCharacter()
369+
{
370+
$string = "1\0";
371+
$value = $this->_db->quote($string);
372+
$this->assertEquals("'1\\000'", $value);
373+
}
374+
364375
public function getDriver()
365376
{
366377
return 'Pdo_Mssql';

tests/Zend/Db/Adapter/Pdo/MysqlTest.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,17 @@ public function testAdapterIncludesCharsetInsideGeneratedPdoDsn()
315315
$adapter = new ZendTest_Db_Adapter_Pdo_Mysql(array('dbname' => 'foo', 'charset' => 'XYZ', 'username' => 'bar', 'password' => 'foo'));
316316
$this->assertEquals('mysql:dbname=foo;charset=XYZ', $adapter->_dsn());
317317
}
318-
318+
319+
/**
320+
* Test that quote() does not alter binary data
321+
*/
322+
public function testBinaryQuoteWithNulls()
323+
{
324+
$binary = pack("xxx");
325+
$value = $this->_db->quote($binary);
326+
$this->assertEquals('\'\0\0\0\'', $value);
327+
}
328+
319329
public function getDriver()
320330
{
321331
return 'Pdo_Mysql';
@@ -330,4 +340,3 @@ public function _dsn()
330340
return parent::_dsn();
331341
}
332342
}
333-

tests/Zend/Db/Adapter/Pdo/SqliteTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,4 +247,15 @@ protected function _testAdapterAlternateStatement($stmtClass)
247247
$this->assertTrue($stmt instanceof $stmtClass,
248248
'Expecting object of type ' . $stmtClass . ', got ' . get_class($stmt));
249249
}
250+
251+
/**
252+
* test that quote() escapes null byte character
253+
* in a string.
254+
*/
255+
public function testAdapterQuoteNullByteCharacter()
256+
{
257+
$string = "1\0";
258+
$value = $this->_db->quote($string);
259+
$this->assertEquals("'1\\000'", $value);
260+
}
250261
}

0 commit comments

Comments
 (0)