dragon8837 2015-03-17 16:12
浏览 59

没有htmlspecialchars的HTMLPurifier

I am using HTMLPurifier for simple Tinymce WYSIWYG.If I don't use htmlspecialchars,would it be open to XSS Attack?This is what I'm doing

$detail = $purifier->purify($detail);

to purify data for that textarea.If I use htmlspecialchars,it strips all basic tags as well which is not user friendly for an WYSIWYG editor.But the problem is,this allows <script> tag as well.

And if I change conf setting to

$config->set('ExtractStyleBlocks.1', true);

It doesn't allow < and > for <script> tag.Convert < and > for <script> only.But it shows <p>This is paragraph</p> ,<strong>This text is bold</strong> and so on.It shouldn't show <p> and other simple tags to user,but only the text.

How can I get rid of this problem.

Please help.Thanks for your time.

Edit

Here is my HTMLPurifier initialization

$config = HTMLPurifier_Config::createDefault();
//$config->set('ExtractStyleBlocks', true);
$config->set('HTML.ForbiddenElements', array('script','style','applet'));
$purifier = new HTMLPurifier($config);

getting data from database

while(mysqli_stmt_fetch($stmt1)){
  $id=htmlspecialchars($id);
  $title=htmlspecialchars($title);
  $detail = $purifier->purify($detail);

 $posts.="<div id='date_news'><div id='news_holder$id' class='news_holder'><h3 id='show_title'>".htmlspecialchars($title)."</h3>".$detail."</div>";

HTML for $detail

At Database

<p><strong>Alu Vazi</strong></p>
<p>I love alu vazi with&lt;script&gt;alert("XSS")&lt;/script&gt;</p>

User screen

Alu Vazi

I love alu vazi with<script>alert("XSS")</script>

  • 写回答

1条回答 默认 最新

  • duanjiao6731 2015-03-17 16:20
    关注

    OK, following my comment try adding this to your HTML Purifier config, it should be enabled by default, but worth a shot.

    $config = HTMLPurifier_Config::createDefault();
    $config->set('HTML.ForbiddenElements', array('script','style','applet'));
    $purifier = new HTMLPurifier($config);
    

    Edit

    <p>I love alu vazi with&lt;script&gt;alert("XSS")&lt;/script&gt;</p>
    

    You've already escaped the <script> tag here so HTML Purifier has nothing to parse. It will be output on the page as a result but you have effectively neutralised the XSS attempt.

    In your code something is already escaping HTML characters before saving to the database.

    评论

报告相同问题?

悬赏问题

  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 MATLAB中streamslice问题
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序