正则表达式足以阻止注入攻击吗?
我有一个处理用户输入的脚本,在继续与数据库交互之前,它会使用正则表达式验证输入。我唯一的问题是,正则表达式是否足以消除注入攻击,还是我仍然需要应用 mysql_real_escape_string() ?
I have a script that processes user input and before it continues with database interaction it verifies input with regex. My only question is, Is regex enough to weed out injection attacks or do I still need to apply mysql_real_escape_string()
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这实际上取决于表达的“好”程度;例如,“你是否涵盖了所有基础?”为了安全起见,将其放入
mysql_real_escape_string
并没有什么坏处。如果您在脚本中使用几次,不会对性能产生影响。It really depends on how "good" the expression is; as in, "did you cover all your bases?" It doesn't hurt to put it through
mysql_real_escape_string
to be safe. There is no performance impact if you use this a few times in your script.如果正则表达式为
^$
则为如此。对于所有其他输入,这取决于正则表达式是否允许转义字符通过。由于这些取决于数据库和连接设置,因此您应该真正使用准备好的语句 或者,如果这不是一个选项,则每次都使用mysql_real_escape_string
。It is if the regex is
^$
. For all other inputs, it depends on whether the regexp lets escape characters through. Since those depend on the database and connection settings, you should really use prepared statements or, if that's not an option,mysql_real_escape_string
every time.如果你的正则表达式足够好,那么是的,但是,为什么要冒这个风险呢?
If your regex is good enough then yes, however, why take the risk?
除非你有一个令人惊叹的正则表达式,否则我个人不相信它是完整的。
错误总是在于保护太多而不是太少。包括对
mysql_real_escape_string
的调用。Unless you have an amazing RegEx expression, I personally wouldn't trust it to be complete.
Always error on the side of too much protection rather than too little. Include the call to
mysql_real_escape_string
.