为什么 CodeIgniter 的 XSS 过滤器不能清除所有内容?
为什么 CodeIgniter 的 XSS 过滤器仅通过正则表达式对特定事物做出反应,而不是首先清理所有输入,无论内容是否受到污染?另外,为什么这是在输入期间完成而不是在输出期间完成(就像应该的那样?)
Why does CodeIgniter's XSS filter only react through regular expressions on specific things instead of sanitizing all input in the first place regardless if the content is tainted or not? Also, why is this done during input and not on output (like it's supposed to be?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这没有多大意义。在不先检查的情况下,我们如何判断某物是否被“污染”?
根据 CI 的
xss_clean() 的定义
,我们并不总是想要清理输入。正如您所提到的,重要的是输出 - 这就是我们需要注意 XSS 攻击。如果我们总是使用 CI 的xss_clean()
“清理”输入,那么我如何能够在我的博客上发布 javascript 或 PHP 代码示例,或者让用户在评论中这样做?它最终会得到[removed]
。您确实可以选择在 CI 配置中启用全局 xss 过滤器,该过滤器将在以下位置运行
xss_clean()
$_POST
、$_GET
和$_COOKIE
数据会在您获得之前自动生成。这是保护您免受自身伤害的最低级别,但始终可以使用该选项来显式清理数据。例如:由于您仍然可以简单地使用
$_POST['username']
来代替,因此通过启用全局过滤器,它已经被 xss_cleaned 了。这是一种懒惰的方法,不幸的是,一旦这些全局变量被清理,就无法撤消它。如果您已经知道 XSS 攻击可能发生的时间和地点 - 如果您愿意,您就可以轻松使用该功能。请记住,这并不能神奇地使所有数据“安全”,它只是防止一些更恶意的代码注入。像
这样无害的东西会通过这个过滤器。您应该始终以适合使用输入的上下文的方式显式地清理输入。
This doesn't make much sense. How are we to tell whether or not something is "tainted" without checking it first?
By the definition of CI's
xss_clean()
, we don't always want to sanitize input. As you mentioned, it's the output that matters - and that's where we need to be mindful of XSS atacks. If we always "sanitize" input with CI'sxss_clean()
, then how would I, for one example, be able to post javascript or PHP code examples on my blog, or let users do it in the comments? It would end up getting[removed]
.You do have the option to enable the global xss filter in your CI config, which will run
xss_clean()
on$_POST
,$_GET
, and$_COOKIE
data automatically before you can get your hands on it. This is the lowest level possible to protect you from yourself, bu the option is always available to instead clean the data explicitly. For example:Since you could still simply use
$_POST['username']
instead, by enabling the global filter it will already be xss_cleaned for you. This is the lazy way to do it, and unfortunately once those globals are cleaned, there's no way to undo it.If you are already aware of when and where XSS attacks can happen - you have the function easily available to use if you wish. Keep in mind that this does not magically make all data "safe", it merely prevents some of the more malicious code injection. Something more harmless like
</div>
will get past this filter. You should always be sanitizing input explicitly in an appropriate way for the context in which it is used.