【问题标题】:sql injection in createNativequerycreateNativequery 中的 sql 注入
【发布时间】:2021-09-22 04:52:27
【问题描述】:
String qstr = "select * from users where user_type=?1 offset ?2 limit ?3"
Query query = entityManager.createNativeQuery(qstr, User.class);
query.setParameter(1, type);
query.setParameter(2, offset);
query.setParameter(3, limit);

这里的类型、偏移量和限制是用户输入。

Sonarcube 在 createNativeQuery 中显示 sql 注入。 谁能说一下防止sql注入的解决办法是什么?

【问题讨论】:

  • 不确定,但传递文字字符串可能值得,也许 Sonarqube 担心您正在动态构建 qstr

标签: java sql postgresql spring-boot sql-injection


【解决方案1】:

当分配你的类型是“1 和 1 = 1”时会导致 sql 注入

【讨论】:

  • 使用 JDBC PreparedStatements 和类似的类(例如这个类)的要点在于,当它们替换参数时,它们会转义它们以防止 SQL 注入攻击。
  • 不,它不会因为 JPA/Hibernate 会正确地转义这些值。在下面它将使用PreparedStatement 来执行此操作。
猜你喜欢
  • 1970-01-01
  • 2018-03-24
  • 2013-11-24
  • 2019-09-07
  • 2018-01-30
  • 1970-01-01
  • 2012-07-17
  • 2012-03-15
  • 1970-01-01
相关资源
最近更新 更多