PostgresSQL - SQL Ready 语句与字符串转义,防止 SQL 注入攻击
PostgresSQL - SQL Prepared Statement vs String Escaping preventing SQL injection attacks
嗨,我正在使用libpqxx编写一个c ++应用程序,将行插入Postgres SQL表中,并且正在写入的数据是用户输入的,因此我需要防范SQL注入攻击。从我在网上看到的情况来看,我可以采取两种方法:
- 准备好的声明
std::string name_str = "Bob"; \! User input unsafe!!
std::string email_str = "bob@gmail.com"; \! User input unsafe!!
pqxx::connection con(c_string);
std::string insert_str = "INSERT INTO users(name, email) VALUES ($1, $2)";
con.prepare("insert_to_users", insert_str);
pqxx::work insert_work(con);
insert_work.exec_prepared("insert_to_users", name_str, email_str)
- 字符串转义
std::string name_str = "Bob"; \! User input unsafe!!
std::string email_str = "bob@gmail.com"; \! User input unsafe!!
pqxx::connection con(c_string);
pqxx::work insert_work(con);
std::string insert_str = "INSERT INTO users(name, email)"
"VALUES ('" + insert_work.esc(name_str) + "' , '" + insert_work.esc(email_str) + "')";
insert_work.exec(insert_str)
我的应用程序不会使数据库连接保持活动状态,因此预准备语句只会使用一次然后销毁,那么使用预准备语句是否过分?
字符串转义是否提供针对所有 SQL 注入漏洞的保护?还是有更好的方法?
我的应用程序不会使数据库连接保持活动状态,因此预准备语句只会使用一次
如果您担心性能,则应修复此一次性连接问题。如果你不担心性能,那你为什么要关心准备好的陈述是否"矫枉过正"呢?
虽然两者都应该有效,但第一个更干净,将来不太可能有人搞砸。
相关文章:
- 防止主数据类型C++的隐式转换
- 如何防止 c++ 在从浮点型转换为双精度型(不适用于 IO)时添加额外的小数?
- 将指针设置为"nullptr"并不能防止双重删除?
- 如何防止C++遗留代码中的挂起指针
- 如何防止clang格式在流运算符调用之间添加换行符<<
- 如何使用c++在VS 2019上运行SQL查询
- System.InvalidCastException - SQL to C++ - safe_cast<float>
- 如何防止初始值设定项列表中的错误值?
- OpenCV 我应该使用智能指针来防止内存泄漏吗?
- C++ 防止在映射中放置()时调用析构函数
- 是否可以防止省略聚合初始化成员?
- 如何在多个线程中创建 QSql数据库连接时防止名称冲突
- 防止在C++中选择错误文件时提升 xml 解析器崩溃
- 在C++中调用 MS SQL 存储过程
- std::mutex 如何防止线程修改?
- 防止C++中的硬编码数字
- 防止在复制构造函数中隐式调用基构造函数
- 尝试创建表会给出 SQL 逻辑错误
- PostgresSQL - SQL Ready 语句与字符串转义,防止 SQL 注入攻击
- 如何防止Visual Studio 2010在我的c++项目中添加大型SQL文件