【发布时间】:2020-09-30 13:47:43
【问题描述】:
代码示例
// Creates an Objection query.
// I have no control over the creation of the query. I can only modify the query after it has been created.
// Example: "select `todos`.* from `todos` where `text` = ?"
const objectionQuery = thirdPartyService.createQuery(userControlledInput);
// Adds an access check. Example "select `todos`.* from `todos` where `text` = ? and `userId` = ?"
objectionQuery.andWhere("userId", currentUser.id);
上面的例子有一个安全漏洞。如果thirdPartyService 生成这样的查询:
select `todos`.* from `todos` where `text` = ? or `id` = ?
那么在添加访问检查后我们会得到如下查询:
select `todos`.* from `todos` where `text` = ? or `id` = ? and `userId` = ?
而且这个查询可以返回不属于当前用户的数据。 要修复此错误,我们需要将用户控制的条件括在括号中:
select `todos`.* from `todos` where (`text` = ? or `id` = ?) and `userId` = ?
但是我如何使用异议查询生成器来做到这一点?我想像这样:
const objectionQuery = thirdPartyService.createQuery(userControlledInput);
wrapWhereClauses(objectionQuery);
objectionQuery.andWhere("userId", currentUser.id);
【问题讨论】:
标签: node.js orm query-builder objection.js