spring 用于 mongodb 转义参数以避免 SQL 注入

spring for mongodb escaping parameters to avoid SQL injection

我是新手-通过使用 spring 和 MongoDB。

我正在使用使用 MongoOperations 对象的 Dao 模式。

因为我在 RDBMS 和 JPA 方面有很多经验,所以我们通常使用 setParameter,它也负责转义和避免 SQL 注入。

我想知道 spring 中是否有针对 MongoDB 的这种想法。我找不到它,但我可能会遗漏一些东西。我不得不说我仍然不确定使用 Mongo.

是否存在 SQL 注入风险

另外,有没有办法在 MongoDB 中创建命名查询?

谢谢大家

在Mongo中没有SQL注入这样的东西,因为Mongo根本不使用SQL语言。

Spring Data MongoDB 中没有命名查询的概念,而是使用 @Query:

注释存储库方法
public interface PersonRepository extends MongoRepository<Person, String>

  @Query(value="{ 'firstname' : ?0 }", fields="{ 'firstname' : 1, 'lastname' : 1}")
  List<Person> findByThePersonsFirstname(String firstname);
}

有一些与 SQL 注入非常相似的东西是 NOSQL 注入 .
特殊字符不同但概念相同:用户可以control/modify/corrupt请求。

Yet these databases are still potentially vulnerable to injection attacks, even if they aren't using the traditional SQL syntax. Because these NoSQL injection attacks may execute within a procedural language , rather than in the declarative SQL language, the potential impacts are greater than traditional SQL injection

有一种方法可以验证,here is an OWASP page可以帮助您进行测试。

基本是验证您的请求是否正确转义 ' " \ ; { } 甚至更多。

似乎 spring 数据 mongodb 正确地转义了那些,但我不知道它是否完全安全。

至于命名查询,我认为 是正确的,它们不存在,但如果您有存储库,您仍然有一个 @Query 注释。

并且因为您使用的是自定义存储库并不意味着您也不能使用存储库接口,Spring 数据允许您在不实现 repostiroy 的情况下实现它,see here.