我有一个Springboot应用程序,它使用Springboot模块进行数据库操作。当我们扫描代码时,checkmarx报告了许多高和中等级别的问题w.r.t SQL_Injection攻击。下面是用例之一,我需要帮助是否将问题标记为假阳性。如果不是假阳性,我该怎么办才能解决这个问题呢?
AppController.Java
@Controller
public class AppController
{
private static final Logger logger = LoggerFactory.getLogger(AppController.class);
@Autowired
private AppService appService;
@RequestMapping(value = "/propertiesHistory", method = RequestMethod.POST)
public String getPropertiesHistory(@ModelAttribute("propSearchForm") @Validated PropertiesSearch propertiesSearch, BindingResult result, Model model, final RedirectAttributes redirectAttributes)
{
String instanceName = propertiesSearch.getInstanceName();
if (!propertiesSearch.getInstanceName().equalsIgnoreCase("NONE"))
{
List<String> propVersionDates = appService.getPropertyHistoryDates(instanceName);
//Some Businees Logic
}
if (result.hasErrors())
{
logger.warn("getPropertiesHistory() : Binding error - " + result.getAllErrors());
}
else
{
//Some Businees Logic
}
return "app/prophist";
}
}AppService.java
@Service
public class AppService
{
private static final Logger logger = LoggerFactory.getLogger(AppService.class);
@Autowired
private AppRepository appRepository;
public List<String> getPropertyHistoryDates(String instanceName)
{
List<String> list = new ArrayList<String>();
try
{
list = appRepository.findAllMDateDESCByProNotEmptyAndInstanceName(instanceName);
}
catch (Exception e)
{
logger.error("getPropertyHistoryDates(): Error while fetching data from database - ", e);
}
return list;
}
}AppRepository.java
public interface AppRepository extends JpaRepository<AppDataEntity, Long>
{
@Query(value="SELECT mdate FROM tablexyz WHERE properties IS NOT NULL AND instanceName =:instanceName ORDER BY mdate DESC",nativeQuery=true)
List<String> findAllMDateDESCByProNotEmptyAndInstanceName(@Param("instanceName") String instanceName);
}我还在存储库中使用了一些方法,比如List<AppDataEntity> findAllByInstanceName(String instanceName);,它使用的是代理类实现,而不是原生查询。在这种情况下,我还得到了这个Checkmarx问题-- SQL_Injection。
我看到Spring数据并没有改变Hibernate按照公认的答案here与实体的工作方式。它适用于@Query(value="some query",nativeQuery=true)吗?
发布于 2022-09-06 10:04:37
只要我们在@SpringBoot(JPQL)或@Query(NativeQuery)中指定了或索引了/位置参数,那么JPA就可以安全地抵御SQL_Injection攻击。
是的,下面的问题。它只适用于@ query (),唯一的条件是查询应该具有名称(:参数)或位置参数(?1)。
我读到,Spring数据并没有改变Hibernate与 entities的工作方式,正如这里所接受的答案。它适用于@query(value=“SomeQuery”,nativeQuery=true)吗?
以下内容对于sql注入是安全的。
@Query(value="SELECT mdate FROM tablexyz WHERE properties IS NOT NULL AND instanceName =:instanceName ORDER BY mdate DESC",nativeQuery=true)
List<String> findAllMDateDESCByProNotEmptyAndInstanceName(@Param("instanceName") String instanceName);简单的原因是Spring在后台使用hibernate,而hibernate又使用PreparedStatements方式处理数据库。
关于PreparedStatements方法如何保护我们的应用程序免受SQL_Injection攻击的非常详细的解释可以找到here和here as well。
发布于 2022-09-06 07:02:44
只要您使用占位符(:instanceName)而不是带有附加参数的自定义SQL,SQL就不会受到SQL注入的攻击。
很可能您将在Spring接口下使用Hibernate实现,因此在使用PreparedStatement时要注意它。
我有在我以前编写的一些存储库上运行Checkmarx的经验,当涉及到REST中的SQL注入和XSS时,它会给出很多错误的结果。原因是它无法看到项目中使用的上下文或库。
它为我们报告了许多SQL注入漏洞,尽管我们使用了Spring模板,该模板大量使用PreparedStatement。
https://stackoverflow.com/questions/73617743
复制相似问题