Welcome, Guest. Register Now!
   
Mark Forums Read Mark Forums Read Mark Forums Read


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-11-2007, 09:25 AM
Junior Member
 
Join Date: Feb 2007
Posts: 26
Default Filtering certain HTML tag

Hai, I use some Free WYSIWYG editor, and I would like to know how to filter tags such as: SCRIPT, or STYLE to prevent XSS...

Thanks !!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 06-11-2007, 04:35 PM
Maugrim The Reaper's Avatar
Junior Member
 
Join Date: Jun 2007
Location: Ireland
Posts: 20
Default

First step should be looking at PHP's strip_tags(). If you're using Zend_Filter_Input, there's a StripTags filter you could try. There's also the more heavy weight HTMLPurifier library available for PHP which can help.

Either way I suggest reading up on stripping tags. There are a few exploits where malformed HTML is enough is allow for an XSS (Cross Site Scripting) exploit. Something to definitely check for are any tags containing "javascript:" for example (there's also other's like "chrome:", etc.).

A good way is to collect some known exploits, and check (perhaps using Unit Testing for regression testing) whether your form successfully blocks them or not.
__________________
Pádraic Brady

http://blog.astrumfutura.com
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 06-12-2007, 02:32 AM
Junior Member
 
Join Date: Feb 2007
Posts: 26
Default

Thanks. I just played a little bit with Zend StripTags, and it's great.
All I do right now is creating whitelist for allowed tags and attributes.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 06-12-2007, 12:28 PM
Maugrim The Reaper's Avatar
Junior Member
 
Join Date: Jun 2007
Location: Ireland
Posts: 20
Default

I'd also suggest building a list of XSS exploits which use malformed tags. I know it seems a little redundant but better safe than sorry .

Good luck with your project!
__________________
Pádraic Brady

http://blog.astrumfutura.com
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 06-13-2007, 09:06 AM
Junior Member
 
Join Date: Feb 2007
Posts: 26
Default

Hi again, I have no problem in declaring allowed Tags but I have difficulties in declaring allowed Attributes in Zend_Filter_StripTags.

I use this code:
Code:
$allowedTags = array(
   'b','i','br','font'
);
$allowedAttributes = array(
   'size','face'
);

$filter = new Zend_Filter_StripTags($allowedTags,$allowedAttributes);
but when I input this code:
Code:
<font size="7" face="impact">foobar</font>
only
Code:
<font>foobar</font>
is displayed.

Any help about the rule in creating allowed Attributes and use it since I didn't find anywhere in the web.

and by the way, I've tried HTMLPurifier and it can solve all my problem. But I just wanna do all the Zend way hehe...

Thanks again!

Last edited by Tommy1402 : 06-13-2007 at 09:13 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 06-13-2007, 04:14 PM
Maugrim The Reaper's Avatar
Junior Member
 
Join Date: Jun 2007
Location: Ireland
Posts: 20
Default

Attribute names are definitely passed an array. You can also pass a single string if only one attribute (it's typed to an array internally in that case). Haven't tested it for a while so I'll check it later from my development PC.
__________________
Pádraic Brady

http://blog.astrumfutura.com
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 06-14-2007, 03:39 AM
Junior Member
 
Join Date: Feb 2007
Posts: 26
Default

Hi again...
I think I found problem. It's because the magic_quotes_gpc is On.

But, if I add line php_flag magic_quotes_gpc Off in my .HTACCESS file, I got Internal Server Error.

I google for it, then I found out that I have to put file PHP.INI in the same directory with my bootstrap file.

It then displayed
Code:
<font face="7">foobar</font>
still more googling... but it works...

btw, I tried to insert malformed input like
Code:
<font size="7 face="impact>test</font>
I remove one of the quote, the I got
Code:
<font>test</font>
well, it's nice...

Last edited by Tommy1402 : 06-14-2007 at 03:52 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 06-14-2007, 09:25 AM
Maugrim The Reaper's Avatar
Junior Member
 
Join Date: Jun 2007
Location: Ireland
Posts: 20
Default

Magic quotes strikes again .

Using a .htaccess does work - but not if you run PHP as a CGI process. Fortunately, magic_quotes just adds slashes. You can add a screening function to your bootstrap (something like below).

PHP Code:
/**
 * Strip slashes in the event magic_quotes_gpc is enabled
 */
$magicQuotesEnabled = (bool) ini_get('magic_quotes_gpc');
if(
$magicQuotesEnabled === true) {
    
superglobal_strip_slashes(); 
}

// rest of bootstrap


/**
 * Function Definitions
 */

function superglobal_strip_slashes()
{
    if (isset(
$_GET) && !empty($_GET)) {
        
strip_slashes_recursive($_GET);
    }
    if (isset(
$_POST) && !empty($_POST)) {
        
strip_slashes_recursive($_POST);
    }
    if (isset(
$_COOKIE) && !empty($_COOKIE)) {
        
strip_slashes_recursive($_COOKIE);
    }
}

/**
 * Recursively strip slashes from a value
 */
function strip_slashes_recursive(&$value)
{
    
$value is_array($value) ? array_map('strip_slashes_from'$value) : stripslashes($value);
    return 
$value;

It's an annoying addition but essential for porting applications to environments where magic quotes cannot be disabled by the user (e.g. distributing an open source application useable by those on shared hosts).
__________________
Pádraic Brady

http://blog.astrumfutura.com

Last edited by Maugrim The Reaper : 06-14-2007 at 09:32 AM. Reason: missed a function!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 06-15-2007, 03:34 AM
Junior Member
 
Join Date: Feb 2007
Posts: 26
Default

Great ! Thx a lot!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 06-15-2007, 08:03 AM
Maugrim The Reaper's Avatar
Junior Member
 
Join Date: Jun 2007
Location: Ireland
Posts: 20
Default

No problem .

Also keep an eye out for "magic_quotes_runtime". You can disable this from within any file (it's not php.ini only) if it's causing any issues.
__________________
Pádraic Brady

http://blog.astrumfutura.com
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 03:29 AM.