note 45429 added to function.preg-replace
| From: | Richard at osu1 dot php dot net | Date: | Tue, 07 Sep 2004 02:51:30 +0000 |
| Subject: | note 45429 added to function.preg-replace | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-76120@lists.php.net to get a copy of this message | ||
A warning: if you are trying to sanitise dangerous html, as I was, you should know that recent
versions of Mozilla will add an implicit closing > to an unterminated 'dodgy' tag. For
example, the following is NOT good enough:
$allowed='\s*\/?\s*(b|i|u|s|pre|tt|ul|ol|li|p|)\s*';
$memo=preg_replace("/<((?!($allowed>))[^<>]*)>/is",
"<\\1>", $memo);
An attacker can enter this:
<B ONCLICK="window.open('http://www.example.com')"
resulting in part of the site's own content suddenly becoming a link to example.com.
I now use a second regexp to break any unterminated nasties:
$memo = preg_replace('/<([^>]*)(?=(<|$))/',"<\\1",$memo);
----
Manual Page -- http://www.php.net/manual/en/function.preg-replace.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+45429
Delete -- http://master.php.net/manage/user-notes.php?action=delete+45429&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+45429&report=yes
Search -- http://master.php.net/manage/user-notes.php