Procházet zdrojové kódy

ZF-11344: handle cases where no characters translate

- If the filtered string is empty, but the original had a value, we have
  to assume we ran into errors and that the resulting string may fail
  validation criteria. Raise an exception in such cases.

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@24006 44c647ce-9c0f-0410-b52a-842ac1e357ba
matthew před 14 roky
rodič
revize
f1553122ad

+ 5 - 1
library/Zend/Filter/HtmlEntities.php

@@ -201,11 +201,15 @@ class Zend_Filter_HtmlEntities implements Zend_Filter_Interface
         if (strlen((string) $value) && !strlen($filtered)) {
             if (!function_exists('iconv')) {
                 require_once 'Zend/Filter/Exception.php';
-                throw new Zend_Filter_Exception(sprintf('Encoding mismatch has resulted in htmlentities errors'));
+                throw new Zend_Filter_Exception('Encoding mismatch has resulted in htmlentities errors');
             }
             $enc      = $this->getEncoding();
             $value    = iconv('', $enc . '//IGNORE', (string) $value);
             $filtered = htmlentities($value, $this->getQuoteStyle(), $enc, $this->getDoubleQuote());
+            if (!strlen($filtered)) {
+                require_once 'Zend/Filter/Exception.php';
+                throw new Zend_Filter_Exception('Encoding mismatch has resulted in htmlentities errors');
+            }
         }
         return $filtered;
     }

+ 19 - 0
tests/Zend/Filter/HtmlEntitiesTest.php

@@ -242,6 +242,25 @@ class Zend_Filter_HtmlEntitiesTest extends PHPUnit_Framework_TestCase
     }
 
     /**
+     * @group ZF-11344
+     */
+    public function testRaisesExceptionIfEncodingMismatchDetectedAndFinalStringIsEmpty()
+    {
+        $string = file_get_contents(dirname(__FILE__) . '/_files/latin-1-dash-only.txt');
+
+        // restore_error_handler can emit an E_WARNING; let's ignore that, as 
+        // we want to test the returned value
+        // Also, explicit try, so that we don't mess up PHPUnit error handlers
+        set_error_handler(array($this, 'errorHandler'), E_NOTICE | E_WARNING);
+        try {
+            $result = $this->_filter->filter($string);
+            $this->fail('Expected exception from single non-utf-8 character');
+        } catch (Zend_Filter_Exception $e) {
+            $this->assertTrue($e instanceof Zend_Filter_Exception);
+        }
+    }
+
+    /**
      * Null error handler; used when wanting to ignore specific error types
      */
     public function errorHandler($errno, $errstr)