使用 php 保护简单所见即所得的最佳方法 [英] best way to secure simple wysiwyg with php

查看:36
本文介绍了使用 php 保护简单所见即所得的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的网站中添加了一个简单的所见即所得编辑器.(它只允许 B/I/U - 没有更多)
我目前将所有内容作为 html 存储在我的数据库中 - 但添加 或其他恶意代码很简单)

I have added a simple wysiwyg editor in my website. (it only allows B / I / U - no more)
I currently store all content as html in my database - but it's simple to add <a onclick='...'> or other malicious code)

PHP 中安全解析此输入的最佳方法是什么?如何将<b></b><i></i>等实现为白名单并对其他所有内容进行编码?

What is the best way in PHP to parse this input safely? How to implement <b></b>, <i></i> and so on as whitelist and encode everything else?

推荐答案

HTMLPurifier

我只是要把这个扔出去,可能会不可避免地受到鞭打.我不会使用 strip_tags 来保护您所见即所得的表单...永远(除非您想激怒您的用户).

HTMLPurifier

I'm just going to throw this one out there and probably get the inevitable lashing. I would not use strip_tags to secure your WYSIWYG form... ever (Unless you want to piss off your users).

它不会保护您的表单,而且您可能会扼杀您的用户体验.

It won't secure your form, and you may be killing your user's experience.

Chris Shiftlett 在他的博文中写道优秀的段落

Chris Shiftlett in his blog post wrote an excellent paragraph

我讨厌在博客上发表评论,我的评论是通过诸如 strip_tags() 之类的东西传递的,从而有效地破坏了我想说的内容.这让我想起了使用 IM 客户端尝试识别笑脸并用图像替换它们,通常使响应难以破译.

I detest commenting on blogs where my comment is passed through something like strip_tags(), effectively mangling what I'm trying to say. It reminds me of using an IM client that tries to identify smilies and replace them with images, often making responses difficult to decipher.

另一个原因

另一个答案中的其他人也写了我喜欢的这个:

Another Reason

Someone else in another answer also wrote this which I like:

  $str = "10 appels is <than 12 apples";
  var_dump(strip_tags($str));

我得到的输出是:

string '10 appels is ' (length=13)

我个人不会使用除了 HTMLPurifier 之外的任何东西

I personally would not use anything other than HTMLPurifier

HTMLPurifier

在此处尝试演示:http://htmlpurifier.org/demo.php

看看这个类似的问题

这篇关于使用 php 保护简单所见即所得的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆