首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用libpq / libpqxx输入消毒

使用libpq / libpqxx输入消毒
EN

Stack Overflow用户
提问于 2012-08-08 16:45:47
回答 2查看 2.4K关注 0票数 2

我刚刚开始为PostgreSQL数据库实现一些客户端软件。

查询将允许来自不受信任源的输入参数。因此,我需要清理我的交易,然后才能实际执行它们。

至于libpq,我找到了PQescapeStringConn,这可能是我需要的。但是,由于我的代码是用C++编写的,所以我更愿意使用libpqxx等价物。我找不到任何相关的东西。(除了逃亡者之外,它位于内部命名空间中.)

我希望能就最佳实践、阅读、文档链接等提出任何建议。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-08-09 08:15:53

使用基数::报价是可行的方法。

下面是一个简单的例子:

代码语言:javascript
复制
// 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;  

产出如下:

代码语言:javascript
复制
select * from persons where lastname = 'blah'; drop table persons; --'
select * from persons where lastname = 'blah''; drop table persons; --'

供参考:

  • http://pqxx.org/devprojects/libpqxx/doc/development/Reference/a00196.html#details
票数 3
EN

Stack Overflow用户

发布于 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变量已经与数据库连接一起初始化。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11869267

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档