双重转义是否有可能对DB造成伤害?
如果我不小心对字符串进行了双重转义,数据库会受到损害吗?
就这个问题而言,假设我没有使用存储过程或参数化查询
例如,假设我得到以下输入:
bob's bike
并且我逃脱了:
bob\'s bike
但是我的代码很糟糕,并且逃脱了再说一遍:
bob\\\'s bike
现在,如果我将其插入数据库中,数据库中的值将是
bob\'s bike
Which,虽然不是我想要的,但不会损害数据库。假设我采取了所有其他必要的安全预防措施,任何双重转义的输入是否有可能对数据库进行恶意操作?
If I accidentally double escape a string, can the DB be harmed?
For the purposes of this question, let's say I'm not using stored procedures or parametrized queries
For example, let's say I get the following input:
bob's bike
And I escape that:
bob\'s bike
But my code is horrible, and escapes it again:
bob\\\'s bike
Now, if I insert that into a DB, the value in the DB will be
bob\'s bike
Which, while is not what I want, won't harm the DB. Is it possible for any input that's double escaped to do something malicious to the DB assuming that I take all other necessary security precautions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在安全性方面,单次转义与双重转义同样有害/无害。
最大的问题是你需要双重转义。否则,如果您仅进行单次转义,则会在数据库输出中出现反斜杠。
例如,如果您通过unescape()函数运行
bob\\\'s bike
,它将输出bob\'s bike
,然后将其打印到页面,除非你再次取消转义。但不要多次转义,因为这会消除故意的反斜杠(并可能造成更多伤害)。这个问题是否与 PHP 的魔术引号功能有任何关系?只是好奇...
Single escaping is equally as harmful/harmless as double escaping in terms of security.
The biggest issue is that you need to double-unescape. Otherwise, if you only single-unescape, you will end up with backslashes in database output.
For example, if you run
bob\\\'s bike
through the unescape() function, it will outputbob\'s bike
which will then be printed to the page, unless you unescape it again. But don't unescape too many times, because this can remove intentional backslashes (and possibly do more harm).Does this question have anything to do with PHP's magic quotes feature by chance? Just curious...
将 SQL 和/或参数值硬编码到应用程序中决不应该被视为“采取必要的安全预防措施”,因为您将始终受到 SQL 注入攻击(在 Web 应用程序的情况下)。
如果可以的话,最好使用存储过程,如果这不是一个选项,那么至少应该使用参数化查询(绑定变量是另一个术语)。
但要回答你的问题,将
bob\'s bike
存储在数据库中本身不会造成任何损害,但请注意考虑上面提到的其他几点,它们非常重要从安全角度来看。Hardcoding SQL and/or parameter values into an application should never be considered "taking the necessary security precautions", because you will always be subject to SQL injection attacks (in the case of a web application).
It's best to use stored procedures if you can, and if that's not an option, at a minimum you should be using parameterized queries (bind variables is another term for this).
But to answer your question, storing
bob\'s bike
in the database isn't going to do any harm in and of itself, but take care to consider the other points mentioned above, they are vitally important from a security perspective.