spring-data-jpa SpringBoot数据JPA存储库对SQL注入安全吗?

qoefvg9y  于 2022-11-10  发布在  Spring
关注(0)|答案(2)|浏览(341)

我有一个Springboot应用程序,它使用Spring Data JPA模块进行数据库操作。当我们扫描代码时,checkmarx报告了许多关于SQL_Injection攻击的高和中等级别的问题。以下是其中一个用例,我需要帮助来确定是否将问题标记为误报。如果不是误报,我应该做什么来修复问题。

应用程序控制器.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";
    }
}

应用程序服务.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;
    }
}

应用程序存储库.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 Data 不会改变Hibernate处理实体的方式,这是公认的答案here。这是真的吗?是否适用于@Query(value="some query",nativeQuery=true)

0tdrvxhp

0tdrvxhp1#

只要您使用占位符(:instanceName)而不是带有附加参数的自定义SQL,您的SQL就不会受到SQL注入的攻击。
您很可能会在Spring Data JPA接口下使用Hibernate实现,因此在使用PreparedStatement时要小心。
我曾经在一些代码库上运行过Checkmarx,当涉及到REST API中的SQL注入和XSS时,它会给出很多误报。原因是它不能看到项目中使用的上下文或库。
它向我们报告了很多SQL注入漏洞,即使我们使用了大量使用PreparedStatement的Spring JDBC模板。

mf98qq94

mf98qq942#

只要我们在@Query(JPQL)或@Query(nativeQuery)中具有命名或索引/位置参数,SpringBoot数据JPA存储库就可以安全地抵御SQL_Injection攻击。

是,对于以下问题。它仅适用于@Query()条件,即查询应具有命名(:paramname)或位置(?1)参数。
我读到Spring Data不会改变Hibernate与****实体一起工作的方式,就像这里接受的答案一样。这是真的吗?它是否适用于@Query(值=“some query”,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 Data JPA在后台使用Hibernate,而Hibernate又使用PreparedStatements方式处理数据库。
有关PreparedStatements方式如何保护应用程序免受SQL_Injection攻击的详细说明,请参见herehere as well

相关问题