Browse Source

Fix for 655 issue

Enrico Zimuel 10 years ago
parent
commit
70d8aba8c5

+ 0 - 2
library/Zend/Db/Adapter/Pdo/Abstract.php

@@ -292,8 +292,6 @@ abstract class Zend_Db_Adapter_Pdo_Abstract extends Zend_Db_Adapter_Abstract
         if (is_int($value) || is_float($value)) {
             return $value;
         }
-        // Fix for null-byte injection
-        $value = addcslashes($value, "\000\032");
         $this->_connect();
         return $this->_connection->quote($value);
     }

+ 15 - 0
library/Zend/Db/Adapter/Pdo/Mssql.php

@@ -420,4 +420,19 @@ class Zend_Db_Adapter_Pdo_Mssql extends Zend_Db_Adapter_Pdo_Abstract
             return null;
         }
     }
+
+    /**
+     * Quote a raw string.
+     *
+     * @param string $value     Raw string
+     * @return string           Quoted string
+     */
+    protected function _quote($value)
+    {
+        if (!is_int($value) && !is_float($value)) {
+            // Fix for null-byte injection
+            $value = addcslashes($value, "\000\032");
+        }
+        return parent::_quote($value);
+    }
 }

+ 14 - 0
library/Zend/Db/Adapter/Pdo/Sqlite.php

@@ -294,4 +294,18 @@ class Zend_Db_Adapter_Pdo_Sqlite extends Zend_Db_Adapter_Pdo_Abstract
         return $sql;
     }
 
+    /**
+     * Quote a raw string.
+     *
+     * @param string $value     Raw string
+     * @return string           Quoted string
+     */
+    protected function _quote($value)
+    {
+        if (!is_int($value) && !is_float($value)) {
+            // Fix for null-byte injection
+            $value = addcslashes($value, "\000\032");
+        }
+        return parent::_quote($value);
+    }
 }

+ 11 - 0
tests/Zend/Db/Adapter/Pdo/MssqlTest.php

@@ -361,6 +361,17 @@ class Zend_Db_Adapter_Pdo_MssqlTest extends Zend_Db_Adapter_Pdo_TestCommon
         $this->assertArrayHasKey('product_name', $productsTableInfo);
     }
 
+    /**
+     * test that quote() escapes null byte character
+     * in a string.
+     */
+    public function testAdapterQuoteNullByteCharacter()
+    {
+        $string = "1\0";
+        $value  = $this->_db->quote($string);
+        $this->assertEquals("'1\\000'", $value);
+    }
+
     public function getDriver()
     {
         return 'Pdo_Mssql';

+ 11 - 2
tests/Zend/Db/Adapter/Pdo/MysqlTest.php

@@ -315,7 +315,17 @@ class Zend_Db_Adapter_Pdo_MysqlTest extends Zend_Db_Adapter_Pdo_TestCommon
         $adapter = new ZendTest_Db_Adapter_Pdo_Mysql(array('dbname' => 'foo', 'charset' => 'XYZ', 'username' => 'bar', 'password' => 'foo'));
         $this->assertEquals('mysql:dbname=foo;charset=XYZ', $adapter->_dsn());
     }
-    
+
+    /**
+     * Test that quote() does not alter binary data
+     */
+    public function testBinaryQuoteWithNulls()
+    {
+        $binary = pack("xxx");
+        $value  = $this->_db->quote($binary);
+        $this->assertEquals('\'\0\0\0\'', $value);
+    }
+
     public function getDriver()
     {
         return 'Pdo_Mysql';
@@ -330,4 +340,3 @@ class ZendTest_Db_Adapter_Pdo_Mysql extends Zend_Db_Adapter_Pdo_Mysql
         return parent::_dsn();
     }
 }
-

+ 11 - 0
tests/Zend/Db/Adapter/Pdo/SqliteTest.php

@@ -247,4 +247,15 @@ class Zend_Db_Adapter_Pdo_SqliteTest extends Zend_Db_Adapter_Pdo_TestCommon
         $this->assertTrue($stmt instanceof $stmtClass,
             'Expecting object of type ' . $stmtClass . ', got ' . get_class($stmt));
     }
+
+    /**
+     * test that quote() escapes null byte character
+     * in a string.
+     */
+    public function testAdapterQuoteNullByteCharacter()
+    {
+        $string = "1\0";
+        $value  = $this->_db->quote($string);
+        $this->assertEquals("'1\\000'", $value);
+    }
 }