将部分用户数据存储在文件中以防止SQL注入
我是网络编程新手,一直在探索与网络安全相关的问题。
我有一个表单,用户可以在其中发布两种类型的数据 - 让我们称它们为“安全”和“不安全”(从 sql 的角度来看)。
大多数地方建议在清理“不安全”部分(以使其“安全”)后将数据的两个部分都存储在数据库中。
我想知道一种不同的方法 - 将“安全”数据存储在数据库中,将“不安全”数据存储在文件中(数据库外部)。 当然,这种方法会产生一系列与维护文件和数据库条目之间的关联相关的问题。 但是这种方法还有其他重大问题吗,特别是与安全相关的问题?
更新:感谢您的回复! 抱歉不清楚我是谁 考虑到“安全”,所以需要进行一些澄清。 我正在使用 Django,表单 我认为“安全”的数据是通过表单的“cleaned_data”访问的 字典执行所有必要的转义。
出于此问题的目的,让我们考虑一个 wiki 页面。 标题为 wiki 页面不需要附加任何样式。 所以,这个可以访问 通过表单的“cleaned_data”字典,它将用户输入转换为 “安全”格式。 但由于我希望为用户提供任意样式的能力 他们的内容,我可能无法使用“cleaned_data”字典访问内容部分。
文件方法是否解决了此问题的安全问题? 或者还有其他的 我忽略的安全问题?
I am new to web programming and have been exploring issues related to web security.
I have a form where the user can post two types of data - lets call them "safe" and "unsafe" (from the point of view of sql).
Most places recommend storing both parts of the data in database after sanitizing the "unsafe" part (to make it "safe").
I am wondering about a different approach - to store the "safe" data in database and "unsafe" data in files (outside the database). Ofcourse this approach creates its own set of problems related to maintaining association between files and DB entries. But are there any other major issues with this approach, especially related to security?
UPDATE: Thanks for the responses! Apologies for not being clear regarding what I am
considering "safe" so some clarification is in order. I am using Django, and the form
data that I am considering "safe" is accessed through the form's "cleaned_data"
dictionary which does all the necessary escaping.For the purpose of this question, let us consider a wiki page. The title of
wiki page does not need to have any styling attached with it. So, this can be accessed
through form's "cleaned_data" dictionary which will convert the user input to
"safe" format. But since I wish to provide the users the ability to arbitrarily style
their content, I can't perhaps access the content part using "cleaned_data" dictionary.Does the file approach solve the security aspects of this problem? Or are there other
security issues that I am overlooking?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您知道您所说的“安全”数据吗? 事实并非如此。 这是完全不安全的,你应该这样对待它。 不是通过将其存储在文件中,而是通过正确构建 SQL 语句。
正如其他人提到的,使用准备好的语句或模拟它们的库是正确的方法,例如
You know the "safe" data you're talking about? It isn't. It's all unsafe and you should treat it as such. Not by storing it al in files, but by properly constructing your SQL statements.
As others have mentioned, using prepared statements, or a library which which simulates them, is the way to go, e.g.
您认为什么是“安全”和“不安全”? 您是否认为带有斜线的数据转义为“安全”? 如果是这样,请不要。
使用带有 SQL 占位符的绑定变量。 这是防止 SQL 注入的唯一明智的方法。
What do you consider "safe" and "unsafe"? Are you considering data with the slashes escaped to be "safe"? If so, please don't.
Use bound variables with SQL placeholders. It is the only sensible way to protect against SQL injection.
分割数据并不能保护你免受 SQL 注入的侵害,它只会限制通过它暴露的数据,但这并不是攻击的唯一风险。 他们还可以删除数据、添加虚假数据等等。
我认为没有理由使用您的方法,特别是考虑到使用准备好的语句(在许多(如果不是全部)开发平台和数据库中受支持)。
甚至没有进入你的方法最终将成为的噩梦。
最后,如果您不信任数据库,为什么要使用它?如果您愿意,就可以使用纯文件,混合是不行的。
Splitting your data will not protect you from SQL injection, it'll just limit the data which can be exposed through it, but that's not the only risk of the attack. They can also delete data, add bogus data and so on.
I see no justification to use your approach, especially given that using prepared statements (supported in many, if not all, development platforms and databases).
That without even entering in the nightmare that your approach will end up being.
In the end, why will you use a database if you don't trust it? Just use plain files if you wish, a mix is a no-no.
SQL注入不仅可以针对用户,还可以针对整个数据库,而且这是查询(中毒查询)的问题,所以对我来说,避免SQL注入攻击的最好方法(如果不是唯一)是控制你的查询,保护它免受注入的可能性恶意字符而不是分割存储。
SQL injection can targeted whole database not only user, and it is the matter of query (poisoning query), so for me the best way (if not the only) to avoid SQL injection attack is control your query, protect it from possibility injected with malicious characters rather than splitting the storage.