我刚刚开始为PostgreSQL数据库实现一些客户端软件。
查询将允许来自不受信任源的输入参数。因此,我需要清理我的交易,然后才能实际执行它们。
至于libpq,我找到了PQescapeStringConn,这可能是我需要的。但是,由于我的代码是用C++编写的,所以我更愿意使用libpqxx等价物。我找不到任何相关的东西。(除了逃亡者之外,它位于内部命名空间中.)
我希望能就最佳实践、阅读、文档链接等提出任何建议。
发布于 2012-08-09 08:15:53
使用基数::报价是可行的方法。
下面是一个简单的例子:
// connection to the database
std::string login_str = "TODO: add credentials";
pqxx::connection conn(login_str);
pqxx::work txn(conn);
// a potentially dangerous input string
std::string input = "blah'; drop table persons; --";
// no proper escaping is used
std::string sql_1 = "select * from persons where lastname = '%s'";
std::cout << boost::format(sql_1) % input << std::endl;
// this is how it's done
std::string sql_2 = "select * from persons where lastname = %s";
std::cout << boost::format(sql_2) % txn.quote(input) << std::endl; 产出如下:
select * from persons where lastname = 'blah'; drop table persons; --'
select * from persons where lastname = 'blah''; drop table persons; --'供参考:
发布于 2019-03-26 11:13:34
实际上,为了提供一个更好的视图,这周我遇到了一些问题,我们开始使用std::string pqxx::transaction_base::esc。
您只需将其添加到要插入或更新的参数中,它就能完成任务。上面提到的引号函数,它将引号添加到参数中,但它没有解决问题。
例如,如果您执行像UPDATE person set name = w.quote(name)其中id =1这样的操作,那么您将正确地使用引号,以便将参数放在引号之间。因此,为了插入单引号或避免SQL注入,您必须执行以下操作: UPDATE person set name =+“+”+ w.esc( name ) +“‘’”其中id =1或UPDATE person set name= w.quote(w.esc(name)),其中id = 1;为W,pqxx::work变量已经与数据库连接一起初始化。
https://stackoverflow.com/questions/11869267
复制相似问题