php - 过滤、存储到数据库并输出文本区域的文本? Zend 框架 2.3.5

标签 php mysql security zend-framework2

我大家,

我有一个文本区域。我需要将其文本存储到数据库Mysql中并输出它,但输出必须是安全的并保留断线。

在存储之前我使用InputFilter验证:

       $inputFilter = new InputFilter();
       $inputFilter->add(array(
             'name'     => 'Description',
             'required' => true,
             'filters'  => array(
                 array('name' => 'StripTags'),
                 array('name' => 'StringTrim'),
             ),
             'validators' => array(
                 array(
                     'name'    => 'StringLength',
                     'options' => array(
                         'encoding' => 'UTF-8',
                         'min'      => 1,
                         'max'      => 30000,
                     ),
                 ),
             ),

         ));

在存储文本输出后,我使用:

echo nl2br(strip_tags($Description));

strip_tags 用于防止 XSS 攻击,nl2br 用于保留断线。 那么,它安全吗?我已尝试保存

"hi, how are yoy? <a href="www.hackered.com"> hackered </a>"

数据库中的文本是:

"hi, how are yoy? hackered"

我尝试将第一个短语直接存储到数据库中,输出如上。看起来很安全。

这个方法安全吗?有一些方法很安全吗? 我有这个疑问,因为在 Zend Filter 引用中它说:

Warning

Zend\Filter\StripTags is potentially unsecure

Be warned that Zend\Filter\StripTags should only be used to strip all available tags.

Using Zend\Filter\StripTags to make your site secure by stripping some unwanted tags will lead to unsecure and dangerous code.

Zend\Filter\StripTags must not be used to prevent XSS attacks. This filter is no replacement for using Tidy or HtmlPurifier.

http://framework.zend.com/manual/current/en/modules/zend.filter.set.html#striptags

非常感谢

最佳答案

我认为你说的是​​对的:

There are some methods much secure? I have this doubt because into Zend Filter reference it says:

Zend\Filter\StripTags must not be used to prevent XSS attacks

当然Zend\InputFilter是可让您提高应用程序安全性的重要组件之一。 然而,它被认为是预防和阻止 XSS 攻击的错误策略

Zend\InputFilter: this component takes care of scanning input to escape or remove illegal strings (HTML elements or filesystem paths), as well as transforming it to meet formatting requirements (stripping whitespace and newlines or changing string case).

Zend Framework 提供的专门用于削弱 XSS 攻击的组件是 Zend\Escaper 。它提供了五种转义输出的方法,具体取决于输出在呈现的页面中出现的位置。存在用于对 HTML 页面内容、HTML 元素属性、脚本元素、样式元素和 URI 中的内容进行转义/编码的方法。

Zend\Escaper : this component offers a way to escape output and defend from XSS and related vulnerabilities by introducing contextual escaping based on peer-reviewed rules.

这些是 Zend\Escaper 提供的转义方法:

  • escapeHtml:转义 HTML Body 上下文的字符串。
  • escapeHtmlAttr:转义 HTML 属性上下文的字符串。
  • escapeJs:转义 Javascript 上下文的字符串。
  • escapeCss:转义 CSS 上下文的字符串。
  • escapeUrl:转义 URI 或参数上下文的字符串。

您还可以引用文档Zend Escaper Theory of Operation对于每种方法的用法。

请参阅此article关于 ZF2 的 Web 应用程序安全性,特别是本节:Blunting XSS attacks with output escaping

关于php - 过滤、存储到数据库并输出文本区域的文本? Zend 框架 2.3.5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28930402/

相关文章:

php - Laravel 5.4 事件广播不适用于 vuejs

JavaScript 条件中的 PHP header ?

php - 数组正确打印数据但查询只显示一半

java - 使用Java从数据库中动态获取记录

php - MySQL 列每列有多个项目。如何分别检索?

security - 在 Electron 中使用 webview 标签安全吗?

php - 从 $_GET/$_POST 获取信息并将其保存到数据库的做法?

PHP MySQL JSON 登录系统

php - 查找具有整数的随机时间

silverlight - 如何修复 HTTPS Silverlight 应用程序上下文中的 WCF maxClockSkew 问题?