Welcome Guest, Not a member yet? Register   Sign In
GLOBAL XSS FILTERING on 2.0.2 and CKEDITOR
#25

[eluser]C4iO [PyroDEV][/eluser]
Found myself with this issue on a recent project and started a quest to find the solution.

Although the accepted answer from this Stackoverflow post is old, it turned out as a good starting point towards understanding why CI staff could have chosen to include style in evil attributes list.

I like to set global_xss_filtering to TRUE on my projects even if it's not a real hacker-proof measure.

So, why am I still trying to use it? Simple, it appears to me that it makes more difficult to an attack be successfull. Ok! I have to admit, setting that option to TRUE, also brings difficulties to my life also.

My point is that since I need to allow style attribute because I'm using an WYSIWYG editor (CKEditor, but tried with TinyMCE), it seems to be reasonable removing style from the evil attributes list, but I'll try to do that at applications/core folder and modify just _remove_evil_attributes method as follows:

In a file called MY_Security located at applications/core, I'll put no more than the following code:
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
* Security Class
*
*/
class MY_Security extends CI_Security {

/*
  * Remove Evil HTML Attributes (like evenhandlers and style)
  *
  * It removes the evil attribute and either:
  *  - Everything up until a space
  *  For example, everything between the pipes:
  *  <a >
  *  - Everything inside the quotes
  *  For example, everything between the pipes:
  *  <a  alert('world');" class="link">
  *
  * @param string $str The string to check
  * @param boolean $is_image TRUE if this is an image
  * @return string The string with the evil attributes removed
  */
protected function _remove_evil_attributes($str, $is_image)
{
  // All javascript event handlers (e.g. onload, onclick, onmouseover) and xmlns
  // removed STYLE attribute to allow it's use by WYSIWYG editors
  $evil_attributes = array('on\w*', 'xmlns', 'formaction');

  if ($is_image === TRUE)
  {
   /*
    * Adobe Photoshop puts XML metadata into JFIF images,
    * including namespacing, so we have to allow this for images.
    */
   unset($evil_attributes[array_search('xmlns', $evil_attributes)]);
  }

  do {
   $count = 0;
   $attribs = array();

   // find occurrences of illegal attribute strings without quotes
   preg_match_all('/('.implode('|', $evil_attributes).')\s*=\s*([^\s>]*)/is', $str, $matches, PREG_SET_ORDER);

   foreach ($matches as $attr)
   {

    $attribs[] = preg_quote($attr[0], '/');
   }

   // find occurrences of illegal attribute strings with quotes (042 and 047 are octal quotes)
   preg_match_all("/(".implode('|', $evil_attributes).")\s*=\s*(\042|\047)([^\\2]*?)(\\2)/is",  $str, $matches, PREG_SET_ORDER);

   foreach ($matches as $attr)
   {
    $attribs[] = preg_quote($attr[0], '/');
   }

   // replace illegal attribute strings that are inside an html tag
   if (count($attribs) > 0)
   {
    $str = preg_replace("/<(\/?[^><]+?)([^A-Za-z<>\-])(.*?)(".implode('|', $attribs).")(.*?)([\s><])([><]*)/i", '<$1 $3$5$6$7', $str, -1, $count);
   }

  } while ($count);

  return $str;
}
}

Please note that I just removed style attribute from $evil_attributes variable and changed the comment accordingly.

I know that's the best solution, but solves the issue until I find something better.


Messages In This Thread
GLOBAL XSS FILTERING on 2.0.2 and CKEDITOR - by El Forum - 04-10-2011, 10:16 AM
GLOBAL XSS FILTERING on 2.0.2 and CKEDITOR - by El Forum - 04-10-2011, 11:10 AM
GLOBAL XSS FILTERING on 2.0.2 and CKEDITOR - by El Forum - 04-12-2011, 12:05 AM
GLOBAL XSS FILTERING on 2.0.2 and CKEDITOR - by El Forum - 05-09-2011, 11:45 PM
GLOBAL XSS FILTERING on 2.0.2 and CKEDITOR - by El Forum - 05-12-2011, 02:49 AM
GLOBAL XSS FILTERING on 2.0.2 and CKEDITOR - by El Forum - 05-12-2011, 05:11 AM
GLOBAL XSS FILTERING on 2.0.2 and CKEDITOR - by El Forum - 05-19-2011, 11:05 AM
GLOBAL XSS FILTERING on 2.0.2 and CKEDITOR - by El Forum - 05-31-2011, 12:30 PM
GLOBAL XSS FILTERING on 2.0.2 and CKEDITOR - by El Forum - 06-10-2011, 06:11 PM
GLOBAL XSS FILTERING on 2.0.2 and CKEDITOR - by El Forum - 06-17-2011, 11:31 AM
GLOBAL XSS FILTERING on 2.0.2 and CKEDITOR - by El Forum - 06-17-2011, 11:38 AM
GLOBAL XSS FILTERING on 2.0.2 and CKEDITOR - by El Forum - 07-07-2011, 06:03 AM
GLOBAL XSS FILTERING on 2.0.2 and CKEDITOR - by El Forum - 07-21-2011, 10:14 PM
GLOBAL XSS FILTERING on 2.0.2 and CKEDITOR - by El Forum - 07-22-2011, 01:39 AM
GLOBAL XSS FILTERING on 2.0.2 and CKEDITOR - by El Forum - 07-22-2011, 03:10 AM
GLOBAL XSS FILTERING on 2.0.2 and CKEDITOR - by El Forum - 07-28-2011, 01:29 AM
GLOBAL XSS FILTERING on 2.0.2 and CKEDITOR - by El Forum - 07-30-2011, 05:09 AM
GLOBAL XSS FILTERING on 2.0.2 and CKEDITOR - by El Forum - 08-10-2011, 01:10 AM
GLOBAL XSS FILTERING on 2.0.2 and CKEDITOR - by El Forum - 08-12-2011, 12:50 AM
GLOBAL XSS FILTERING on 2.0.2 and CKEDITOR - by El Forum - 08-12-2011, 07:51 AM
GLOBAL XSS FILTERING on 2.0.2 and CKEDITOR - by El Forum - 08-13-2011, 08:44 PM
GLOBAL XSS FILTERING on 2.0.2 and CKEDITOR - by El Forum - 02-08-2012, 11:48 AM
GLOBAL XSS FILTERING on 2.0.2 and CKEDITOR - by El Forum - 07-24-2012, 09:03 AM
GLOBAL XSS FILTERING on 2.0.2 and CKEDITOR - by El Forum - 09-22-2012, 01:57 AM
GLOBAL XSS FILTERING on 2.0.2 and CKEDITOR - by El Forum - 08-20-2013, 02:29 PM



Theme © iAndrew 2016 - Forum software by © MyBB