简体   繁体   English

xss_clean 删除 tinymce 的格式

[英]xss_clean removes formatting of tinymce

In Codeigniter 3.x, if I use $data = $this->security->xss_clean($data);在 Codeigniter 3.x 中,如果我使用$data = $this->security->xss_clean($data); before saving $data to database, it removes all the formatting of TINYMCE Editor and places xss=removed as below:在将$data保存到数据库之前,它会删除TINYMCE 编辑器的所有格式并将xss=removed放置如下:

<span xss=removed><strong>1. Joint pain </strong></span>

As a result, I cannot see font colors, background colors, font size change in the output.结果,我看不到输出中的字体颜色、背景颜色、字体大小的变化。

If I remove $data = $this->security->xss_clean($data);如果我删除$data = $this->security->xss_clean($data); before saving $data to the database and implement only following security measures, then formatting works fine:在将$data保存到数据库并仅实施以下安全措施之前,格式化工作正常:

$data = trim($data);
//$data = $this->security->xss_clean($data);
$data = html_escape($data);
$data = strip_tags($data);

Now it stores the following in the database:现在它将以下内容存储在数据库中:

<span style="font-size: 14pt;"><strong>1. Joint pain </strong></span>

I have two questions here:我在这里有两个问题:

  1. Is the above code sufficient to ensure that the data send to database is fully sanitized and will not allow any attacks.上面的代码是否足以确保发送到数据库的数据被完全清理并且不允许任何攻击。 I am using Active Records of Codeigniter for executing SQL.我正在使用 Codeigniter 的 Active Records 来执行 SQL。

  2. Should I use xss_clean while showing output on the screen like this: echo $this->security->xss_clean(htmlspecialchars_decode($data))我应该在屏幕上显示输出时使用xss_clean 吗echo $this->security->xss_clean(htmlspecialchars_decode($data))

Please help.请帮忙。

The xss_clean function in Codeigniter has been removed in newer versions as it was not the right approach. xss_clean中的xss_clean函数已在较新版本中删除,因为它不是正确的方法。 It tried to do too much but not in a reliable way.它试图做得太多,但不是以可靠的方式。 It can be replaced with either escaping or filtering the input.它可以用转义或过滤输入来代替。 This is done either with the HTML escape functions, or a library like HTMLPurifier.这是通过 HTML 转义函数或 HTMLPurifier 之类的库完成的。

HTML escaping is for when the input is plain text, and you want to insert it into an HTML document. HTML 转义适用于当输入是纯文本,并且您想将其插入到 HTML 文档中时。 In that case, you can escape the data when outputting it on a page.在这种情况下,您可以在页面上输出数据时对数据进行转义。 This can depend on the context of where it's inserted (see OWASP XSS Prevention Cheat Sheet ).这取决于插入位置的上下文(请参阅OWASP XSS 预防备忘单)。

If your input is HTML though, as you get from some editors, then instead you want something like HTMLPurifier .但是,如果您的输入是 HTML,正如您从某些编辑器中获得的那样,那么您想要的是HTMLPurifier 之类的东西。 This can be applied both before saving the data to the database, and when displaying the data.这既可以在将数据保存到数据库之前应用,也可以在显示数据时应用。 This filters the HTML against a whitelist of allowed elements, so some formatting can be allowed while preventing scripts.这会根据允许元素的白名单过滤 HTML,因此可以在阻止脚本的同时允许某些格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM