Making Sanitize::stripScripts() to remove multi-line script and style blocks. Fixes #657

This commit is contained in:
Mark Story 2010-05-03 22:31:55 -04:00
parent 95dbae8acf
commit ce10c85367
2 changed files with 27 additions and 1 deletions

View file

@ -156,7 +156,7 @@ class Sanitize {
* @static * @static
*/ */
function stripScripts($str) { function stripScripts($str) {
return preg_replace('/(<link[^>]+rel="[^"]*stylesheet"[^>]*>|<img[^>]*>|style="[^"]*")|<script[^>]*>.*?<\/script>|<style[^>]*>.*?<\/style>|<!--.*?-->/i', '', $str); return preg_replace('/(<link[^>]+rel="[^"]*stylesheet"[^>]*>|<img[^>]*>|style="[^"]*")|<script[^>]*>.*?<\/script>|<style[^>]*>.*?<\/style>|<!--.*?-->/is', '', $str);
} }
/** /**

View file

@ -346,6 +346,32 @@ class SanitizeTest extends CakeTestCase {
$expected = ''; $expected = '';
$result = Sanitize::stripScripts($string); $result = Sanitize::stripScripts($string);
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
$string = <<<HTML
text
<style type="text/css">
<!--
#content { display:none; }
-->
</style>
text
HTML;
$expected = "text\n\ntext";
$result = Sanitize::stripScripts($string);
$this->assertEqual($result, $expected);
$string = <<<HTML
text
<script type="text/javascript">
<!--
alert('wooo');
-->
</script>
text
HTML;
$expected = "text\n\ntext";
$result = Sanitize::stripScripts($string);
$this->assertEqual($result, $expected);
} }
/** /**