sql - 可以接受客户端 SQL 吗?如果可以,如何验证?

标签 sql mongodb postgresql sql-injection

我有一个应用程序,我想在其中接受来自前端查询构建器 (http://querybuilder.js.org/) 的用户提供的 SQL 查询。该查询最终需要使其成为在 postgres 数据库中运行以返回数据子集的方式。

上面链接的查询生成器可以导出 SQL 或 mongo 查询。我想使用 mongo 查询是相对安全的,因为我可以简单地在服务器上添加它:

query.owner_of_document = userId

限制结果(到用户拥有的文档)。

而如果有人试图存储恶意的 SQL 字符串以供执行,则 SQL 语句可能会在注入(inject)攻击中被劫持。

直接接受来自客户端的 SQL 是不好的做法吗?如何确保提供的 SQL 是安全的?

谢谢!

最佳答案

为什么需要接受整个 SQL 语句?

您可以只接受参数然后运行预定义的查询吗?

SO 上有很多与 SQL 注入(inject)相关的问题/答案,使用参数是避免注入(inject)攻击的第一步,例如“Are Parameters really enough to prevent Sql injections?

但我认为this answer一个不同的问题总结得很好:

Don't try to do security yourself. Use whatever trusted, industry standard library there is available for what you're trying to do, rather than trying to do it yourself. Whatever assumptions you make about security, might be incorrect. As secure as your own approach may look ... there's a risk you're overlooking something and do you really want to take that chance when it comes to security?

关于sql - 可以接受客户端 SQL 吗?如果可以,如何验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40064879/

相关文章:

javascript - wait 仅在异步函数中有效 - nodejs

php - 拉维尔 5 : how to insert data in database and return field back

php - 在linux服务器上安装dlib并尝试将symfony2与sql服务器连接

node.js - 将范围传递给 db.collection.group

MYSQL:根据另外两个表的连接计算一个表中的行数

node.js - 如何让一段代码同步工作

sql - PostgreSQL 获取每天的平均、最小、最大延迟值

java - 无法创建外键mysql

mysql - 最快和/或最简单的方法是什么?

c# - 使用 Active Directory 密码从 C# 连接到 Sql Azure Db 时遇到问题