簡體   English   中英

SQL Spring 引導中的注入預防

[英]SQL Injection Prevention in Spring Boot

我的 Checkmarx 報告在我的 Spring 啟動應用程序中將此方法標記為嚴重性二階 SQL 注入

public void updateEmployees(int storeId) {
  List<Employee> employees = employeeRepo.findByStoreId(storeId);
  employees.stream().forEach(e->{
    e.setActive('Y');
    employeeRepo.save(s);
  });
}

根據 Checkmarx:

... 應用程序通過在沒有適當清理的情況下將不受信任的字符串嵌入到查詢中來構造此 SQL 查詢。 連接后的字符串被提交到數據庫,在那里它被相應地解析和執行。

setActive('Y') 行似乎是問題所在。

我找到的關於 Spring 數據 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 數據不會改變 Hibernate 處理實體的方式。 因此,這要么是誤報,要么您正在查看錯誤的代碼行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM