Codeigniter 输出和输入安全
如果用户提交文本,当输出到页面时,您在输入和输出中使用什么文本过滤器?
据我了解,使用 $this->input->post('something',true)
将从输入数据中清除 XSS 内容,因此无需执行其他操作即可确保安全?像 htmlspecialchars()
、strip_tags()
等?
另外我想知道 htmlspecialchars() 是否很好用,为什么 CI 安全库默认不将 htmlspecialchars() 应用到传递的字符串?
In case of user submitted text, when outputting to the page, what text filter do you use both in input and output?
As I understand it, using $this->input->post('something',true)
will clean XSS content from the input data, so there is no other thing to do to be secure? Something like htmlspecialchars()
, strip_tags()
, etc.?
Also i would like to know if for example htmlspecialchars() is good to use, why CI security library doesn't applyes htmlspecialchars() by default to the passed string?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该使用 form_validation 库。您可以进行基于规则的检查和过滤。这是验证输入数据的更可靠的方法。
以下是内置规则,任何采用一个参数的定义函数都可以用作过滤器/规则。
You should use the form_validation library. You can do rule based checking and filtering. This is a much more robust way of validating input data.
Here are the built in rules and any defined function that takes one parameter can be used as a filter/rule.
有点取决于您对此输入执行的操作,但很可能您还希望通过
htmlspecialchars()
运行该字符串。Kinda depends on what you're doing with this input, but most likely you're going to want to run the string through
htmlspecialchars()
also.据我了解,您希望将用户提交的文本存储在数据库中,然后将其显示在页面上 - 有点像基本的评论系统或其他东西。您只是不希望输出时任何顽皮/不完整的 HTML 字符破坏您的页面。
每当您有用户提交的数据时,您都希望利用 form_validation 库来尽可能地清理和净化它,作为一种良好的安全措施。如果它进入您的数据库,您应该使用 Active Records 或 Query Binding 从 Codeigniter 获得额外的安全性,例如转义字符串等。
让我展示一下在网站上提交和输出用户输入的解决方案。可能有更好的方法来做到这一点,但这将完成工作。
此代码假设您自动加载表单验证、会话、数据库库和表单助手。现在,在表单验证期间,使用自定义正则表达式回调将所有用户输入的数据剥离为最少的纯文本。所有顽皮的 HTML 字符都已完全消失/清理。现在,您可以放心地将提交的数据输出到网页上的任何位置,而不会破坏或产生安全问题。
仅执行 HTMLSpecialChars() 和 html 解码的问题是它没有考虑不完整的 HTML 标记。希望这会有所帮助,祝你好运,而且一如既往,没有什么是完全安全的。
To my understanding, you would like to store user submitted text in a database, and then later display it on a page -- kind of like a basic commenting system or something. You just don't want any naughty/incomplete HTML characters breaking your page when outputting it.
Whenever you have user submitted data, you want to utilize the form_validation library to clean it up and sanitize it as much as possible as a good security measure. If it goes to your database, you should use Active Records or Query Binding to get additional security from Codeigniter, such as escaping the strings, etc.
Let me show my solution on submitting and outputting user's input on a website. There are probably better ways to do this, but this will get the job done.
This code assumes you autoload the Form Validation, Sessions, and Database Libraries and the Form Helper. Now, all your user inputed data is stripped to a bare minimum of plain text using a custom Regular Expression call back during form validation. All naughty HTML characters are gone/sanitized, completely. You can now be worry-free to output the submitted data anywhere you'd like on a webpage without it breaking or being a security concern.
The problem with just doing HTMLSpecialChars() and html decode is it doesn't account for incomplete HTML tags. Hopefully this helps, best of luck dude, and as always, nothing is ever completely secure.