使用 Hibernate 的 Restrictions.eq() 方法可以安全地防止 SQL 注入吗?

问题描述 投票:0回答:1

使用 Hibernate 的 Restrictions.eq() 方法(如下面的示例中所示)是否可以安全地防止 SQL 注入?或者类似的方法,如 Restrictions.in()、Restrictions.ge()、...

String vulnerable = //parameter from user interface 
Criteria ct = this.getCriteria();
ct.add(Restrictions.eq("propertyName", vulnerable));

我发现可能是这样,正如这个答案

中所解释的那样

但是查看 OWASP 文档,有一个示例显示了令我困惑的评论 (

// This should REALLY be validated too
)。需要验证输入,还是原样安全?

OWASP文档中的示例

// Criteria API
// This should REALLY be validated too
String userSuppliedParameter = request.getParameter("Product-Description");
// Perform input validation to detect attacks
Inventory inv = (Inventory) session.createCriteria(Inventory.class).add
(Restrictions.eq("productDescription", userSuppliedParameter)).uniqueResult();
hibernate sql-injection hibernate-criteria hibernate-restrictions
1个回答
1
投票

是的,Hibernate 的 Criteria API 可以保护您免受 SQL 注入,因为它参数化了查询 - 您可以通过启用 sql 日志记录来查看其实际情况。

您需要注意的是连接用户输入等。我认为文档可能指的是在客户端和服务器端验证用户输入。

© www.soinside.com 2019 - 2024. All rights reserved.