【问题标题】:PHP post removing styles attributePHP post删除样式属性
【发布时间】:2013-02-02 05:06:21
【问题描述】:

我有一个奇怪的情况,我似乎无法在 Google 上找到答案。

我正在获取一个 javascript 数组,对其应用 JSON.stringify,然后通过 AJAX 发布到 php 控制器,以将现在的 json_encoded 数组存储在表中。通过 ajax 发布后,$_POST 以某种方式剥离了正在提交的 html 上的样式属性。

这是通过 javascript/jquery 抓取的示例 html:

<"div class="blahblah" style="border:1px solid #000000;"><strong>test</strong></div>

这里是 AJAX 邮政编码:

var post_data = [];
    $("divclasshere").each(function(){
        post_data.push({html:$(this).html()});
    });
    var data = JSON.stringify(post_data); 
    $.ajax({
        type: "POST",
        url: "save",
        data: { content: data },
        success: function(result){
        }
    });

这是将其保存到数据库的控制器函数:

$data = array(
    'content' => $this->input->post('content')
);
$this->db->update('table', $data);

如果我在 PHP 控制器上的数据上打印_r,我会得到(示例)

<div class="blahblah"><strong>test</strong></div>

但 div class="blahblah" 元素上没有样式属性。如果这有什么不同,我正在使用 CodeIgniter?在某些情况下,它会去除第一部分:style="border:1px 并保留 #000000;"

编辑:

这是发布的内容(例如):

content:[{"html":"<div class=\"content\" style=\"border:1px solid #000000;\"></div>"}]

这是得到 print_r'd 的内容:

<pre>[{"html":"<div class=\"content\"  solid #000000;\"></div>"}]

【问题讨论】:

  • 您是否在配置中启用了global_xss_filtering
  • @Yan,我愿意...不过,这主要不是为了防止跨域的事情发生吗?
  • 很难说。通过在 AJAX 处理程序 PHP 脚本中输出变量开始调试...var_dump($yourVariable);。在 Firefox 中使用 Firebug(它是一个附加组件)中的控制台来查看输出。逐步检查,直到找到失败的地方。
  • 它也会删除样式。
  • 为什么要在标签中添加样式,而您已经添加了一个类?如果样式不同,则为这些样式创建一个类,因为类属性允许类列表。这个问题是由 XSS 过滤器引起的,但是您希望启用它来保护您,所以我个人会找到一种不会触发过滤的方法,例如放置两个类。

标签: javascript jquery ajax codeigniter styles


【解决方案1】:

核心 _remove_evil_attributes 函数从标签中删除样式属性。为了克服这个问题,你有一个解决方法。只需在应用程序的核心目录 (application/core/MY_security.php) 中创建一个文件名 My_Security.php 并将以下代码粘贴到其中以覆盖默认功能。

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class MY_Security extends CI_Security {
    function __construct()
    {
      parent::__construct();
    }

    // --------------------------------------------------------------------

    /*
        * Modified for cb_cms
     */
    protected function _remove_evil_attributes($str, $is_image)
    {
        // All javascript event handlers (e.g. onload, onclick, onmouseover), style, and xmlns
        $allowed = array("your allowed url's without domain like '/admin/edittext/'");
        if(in_array($_SERVER['REQUEST_URI'],$allowed)){
            $evil_attributes = array('on\w*', 'xmlns');
        }else{
            $evil_attributes = array('on\w*', 'style', 'xmlns');
        }

        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 {
            $str = preg_replace(
                "#<(/?[^><]+?)([^A-Za-z\-])(".implode('|', $evil_attributes).")(\s*=\s*)([\"][^>]*?[\"]|[\'][^>]*?[\']|[^>]*?)([\s><])([><]*)#i",
                "<$1$6",
                $str, -1, $count
            );
        } while ($count);

        return $str;
    }

} 
?>

【讨论】:

  • 感谢您指出这一点!帮助我解决了缺少样式属性的谜团。
猜你喜欢
  • 2022-01-03
  • 1970-01-01
  • 1970-01-01
  • 2013-10-21
  • 1970-01-01
  • 1970-01-01
  • 2019-09-08
  • 2012-08-12
  • 1970-01-01
相关资源
最近更新 更多