knex.where 容易受到 sql 注入攻击吗?

Is knex.where prone to sql injection attacks?

这是 的后续问题。

它提到 knex('table').where('description', 'like', '%${term}%') 容易受到 sql 注入攻击。甚至评论都提到第一种情况容易受到注入攻击。然而,提供的参考资料从未提及 .where 容易受到注入攻击。

这是一个错误吗?为什么 knex 允许 .where 容易受到注入攻击而不是 .whereRaw('description like \'%??%\'', [term]) 。在这两种情况下参数都没有被参数化吗?

This is a follow up question to .

It mentions knex('table').where('description', 'like', '%${term}%') as prone to sql injection attacks. Even a comment mentions the first case as prone to injection attacks. Yet the reference provided never mentions .where being prone to injection attacks.

我是 knex 的维护者,我在那里评论说

knex('table').where('description', 'like', `%${term}%`)

不易受到 SQL 注入攻击。

Is this a mistake? Why would knex allow .where to be prone to injection attacks but not .whereRaw('description like \'%??%\'', [term]) . Aren't the arguments being parameterized in both cases?

当您将值直接插入 sql 字符串时,.whereRaw 很容易受到攻击(例如 ?? 标识符替换)。

在这种情况下 .whereRaw 的正确用法是例如:

.whereRaw("?? like '%' || ? || '%'", ['description', term])

所有标识符都被正确引用并且 term 作为参数绑定发送到 DB。

所以答案和添加到该答案的大部分评论都是完全错误的。