我的 Checkmarx 报告在我的 Spring Boot 应用程序中将此方法标记为高严重性二阶 SQL 注入(inject):
public void updateEmployees(int storeId) {
List<Employee> employees = employeeRepo.findByStoreId(storeId);
employees.stream().forEach(e->{
e.setActive('Y');
employeeRepo.save(s);
});
}
根据 Checkmarx:
... The application constructs this SQL query by embedding an untrusted string into the query without proper sanitization. The concatenated string is submitted to the database, where it is parsed and executed accordingly.
setActive(‘Y’) 行似乎是问题所在。
我找到的有关 Spring Data JPA 使用的数据清理的指南建议使用从EntityManager实例创建的TypedQuery。像这样的东西:
String jql = "from Employee where storeId = :storeId";
TypedQuery<Account> q = em.createQuery(jql, Employee.class)
.setParameter("storeId", storeId);
但是,在我的应用程序中,EntityManager 是由 Spring Boot 隐式创建的。
因此,为了在这里实现“适当的清理”,我是否需要显式创建 EntityManager 实例(以访问其 createQuery 方法)?或者是否有更直接的方法来实现这一点,不需要显式的 EntityManager 实例化?
最佳答案
这是误报,您需要联系供应商并通知他们此情况。当Hibernate生成SQL时,它使用PreparedStatement。您更改的所有实体字段都将与查询分开发送 - 不会有任何串联。
Spring Data 不会改变 Hibernate 处理实体的方式。因此,这要么是误报,要么您正在查看错误的代码行。
关于java - Spring Boot 中的 SQL 注入(inject)预防,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72001344/