【问题标题】:Codeigniter global_xss_filteringCodeigniter global_xss_filtering
【发布时间】:2011-04-23 10:07:51
【问题描述】:

在我的 codeigniter 配置中,我有 $config['global_xss_filtering'] = TRUE;。在我的管理部分,我有一个生成前端内容的 ckeditor。

在编辑器中输入和放置的所有内容都可以正常工作,图像显示良好,html 可以正常工作。除了闪光灯。每当我切换到 html 模式并粘贴 youtube 代码片段时,它都会被转义,并且代码在首页上可见,而不是显示 youtube 电影。

如果我设置$config['global_xss_filtering'] = FALSE;,则 youtube 代码将按应有的方式传递。这是因为 'object'、'embed' 等被 CI 标记为“naughty”并因此被转义。

我怎样才能绕过这个控制器方法的 xss 过滤?

【问题讨论】:

    标签: codeigniter xss


    【解决方案1】:

    我的情况是我希望 global_xss_filtering 默认开启,但有时我需要 $_POST (您可以对任何全局 php 数组执行此操作,例如 $_GET...) 从浏览器发送的原始数据,所以我的解决方案是:

    1. 打开项目根目录下的index.php
    2. $application_folder = 'application'; 之后添加了以下代码行 $unsanitized_post = $_POST;(第 92 行)
    3. 然后,每当我需要原始 $_POST 时,我都会执行以下操作:

      全球 $unsanitized_post;

      print_r($unsanitized_post);

    【讨论】:

      【解决方案2】:

      在 CodeIgniter 2.0 中,最好的办法是覆盖核心 CI 库上的 xss_clean,使用 MY_Security.php 将其放在 application/core 文件夹中,然后使用 /application/config.php

      $config['xss_exclude_uris'] = array('controller/method');
      

      这里是 MY_Security.php https://gist.github.com/slick2/39f54a5310e29c5a8387

      <?php
      
      /**
       * CodeIgniter version 2
       * Note: Put this on your application/core folder
       */
      
      class MY_Security extends CI_Security {
      
          /**
           * Method: __construct();
           * magic
           */
          function __construct()
          {
              parent::__construct();
          }
      
          function xss_clean($str, $is_image = FALSE)
          {
      
              $bypass = FALSE;
      
              /** 
               * By pass controllers set in /application/config/config.php
               * config.php
               * $config['xss_exclude_uris'] = array('controller/method')
               */
      
              $config = new CI_Config;
              $uri = new CI_URI;
              $uri->_fetch_uri_string();
              $uri->_explode_segments();
      
              $controllers_list = $config->item('xss_exclude_uris');
      
              // we need controller class and method only
              if (!empty($controllers_list))
              {
                  $segments = array(0 => NULL, 1 => NULL);
                  $segments = $uri->segment_array();
                  if (!empty($segments))
                  {
                      if (!empty($segments[1]))
                      {
                          $action = $segments[0] . '/' . $segments[1];
                      }
                      else
                      {
                          $action = $segments[0];
                      }
                      if (in_array($action, $controllers_list))
                      {
                          $bypass = TRUE;
                      }
                  }
      
                  // we unset the variable
                  unset($config);
                  unset($uri);
              }
      
      
      
              if ($bypass)
              {
                  return $str;
              }
              else
              {
                  return parent::xss_clean($str, $is_image);
              }
          }
      
      }
      

      【讨论】:

        【解决方案3】:

        全局 XSS 过滤只是转义(或转换)某些“危险”的 html 标签,例如 &lt;html&gt;

        简单的解决方法:

        1. 设置$config['global_xss_filtering'] = TRUE;
        2. 通过 HTMLPurifier 运行 POST 数据以删除任何讨厌的 &lt;script&gt; 标记或 javascript。
        3. 在您收到表单POST 数据的页面上,使用html_entity_decode() 撤消 XSS 过滤所做的操作。

          //by decoding first, we remove everything that XSS filter did
          //then we encode all characters equally.
          $content = html_entity_decode($this->input->post('template_content'))
          
        4. 然后立即通过 htmlentities() 运行它

          $content = htmlentities($content);
          
        5. 在 MySQL 数据库中存储为 Blob

        6. 当您想要显示 信息给用户编辑运行html_entity_decode()

        我就是这样做的。如果有人知道我所做的一个重大缺陷,请告诉我。它似乎对我来说工作得很好。没有出现任何意外错误。

        【讨论】:

        • 您为攻击者提供了嵌入 XSS 的简单途径。他所要做的就是对攻击进行 HTML 编码。它将绕过所有 XSS 过滤器,然后通过 html_entity_decode 转换回攻击。
        • 我还应该如何允许人们在文本区域中使用 HTML?我通过 HTMLPurifier 运行它,这还不够吗?
        • 这也是剥离内联样式属性,如果我使用任何
        【解决方案4】:

        在显示来自 YouTube 等的嵌入对象代码时,在视图上简单地执行以下操作:

        echo str_replace(array('&lt;', '&gt;'), array('<', '>'), $embed_filed);
        

        【讨论】:

          【解决方案5】:

          默认关闭它,然后在真正需要它的地方启用它。

          例如,我为所有控制器关闭它,然后为 cmets、页面等启用它。

          您可以做的一件事是创建一个 MY_Input(或 CI 2 中的 MY_Security),就像 PyroCMS 中的那个一样,并用一个精确的副本覆盖 xss_clean 方法,减去 object|embed|正则表达式的一部分。

          http://github.com/pyrocms/pyrocms/blob/master/system/pyrocms/libraries/MY_Security.php

          这是一个很长的路要走,但它确实有效。

          也许我们可以创建一个配置选项来列出 2.0 的不良元素?

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-12-21
            • 2012-04-03
            • 2011-05-30
            • 2014-07-29
            • 2018-12-14
            • 2012-09-28
            相关资源
            最近更新 更多