为什么 CodeIgniter 的 XSS 过滤器不能清除所有内容?

发布于 2024-11-15 03:36:44 字数 108 浏览 0 评论 0原文

为什么 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吹梦到西洲 2024-11-22 03:36:44

为什么 CodeIgniter 的 XSS 过滤器仅通过正则表达式对特定事物做出反应,而不是首先清理所有输入,无论内容是否受到污染?

这没有多大意义。在不先检查的情况下,我们如何判断某物是否被“污染”?

根据 CI 的 xss_clean() 的定义,我们并不总是想要清理输入。正如您所提到的,重要的是输出 - 这就是我们需要注意 XSS 攻击。如果我们总是使用 CI 的 xss_clean()“清理”输入,那么我如何能够在我的博客上发布 javascript 或 PHP 代码示例,或者让用户在评论中这样做?它最终会得到[removed]

另外,为什么这是在输入期间完成而不是在输出时完成(就像应该的那样?)

您确实可以选择在 CI 配置中启用全局 xss 过滤器,该过滤器将在以下位置运行 xss_clean() $_POST$_GET$_COOKIE 数据会在您获得之前自动生成。这是保护您免受自身伤害的最低级别,但始终可以使用该选项来显式清理数据。例如:

// With the Input class on $_POST data
$this->input->post('username', TRUE); // Second parameter runs xss_clean

 // Using the Security class on any data
$this->security->xss_clean($username);

// Using the Form Validation class to automatically clean the input
$this->form_validation->set_rules('username', '', 'xss_clean');

由于您仍然可以简单地使用 $_POST['username'] 来代替,因此通过启用全局过滤器,它已经被 xss_cleaned 了。这是一种懒惰的方法,不幸的是,一旦这些全局变量被清理,就无法撤消它。

如果您已经知道 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?

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's xss_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].

Also, why is this done during input and not on output (like it's supposed to be?)

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:

// With the Input class on $_POST data
$this->input->post('username', TRUE); // Second parameter runs xss_clean

 // Using the Security class on any data
$this->security->xss_clean($username);

// Using the Form Validation class to automatically clean the input
$this->form_validation->set_rules('username', '', 'xss_clean');

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文