使用 GraphQL,是否可以通过数据类型为 Int 的属性进行 SQL 注入?

Using GraphQL, Is SQL injection possible via an attribute with a datatype of Int?

如果我有一个输入类型:

input EntityInput {
  id: Int!
  vals: [Int!]!
}

我有一个突变,它是通过从输入数组

在 JavaScript 中构建一个 SQL 字符串来实现的
type Mutation {
  updateEntity(input: [EntityInput!]!): [Entity!]!
}

考虑到输入是数字,是否可以SQL注入?我知道对于 String 类型的属性,SQL 注入是可能的。

来自spec

When expected as an input type, only integer input values are accepted. All other input values, including strings with numeric content, must raise a query error indicating an incorrect type. If the integer input value represents a value less than -2³¹ or greater than or equal to 2³¹, a query error should be raised.

换句话说,提供的任何非整数值都会在验证期间抛出错误,然后再触及任何解析器代码。根本不会执行无效查询,因此不可能进行注入攻击。

但是,您不应该依赖 GraphQL 的类型系统来防止注入。相反,您应该确保正确转义在您执行的任何 SQL 查询中使用的所有用户输入。大多数流行的 ORM、查询构建器等已经为您完成了这项工作,或者至少提供了一种 运行 安全、参数化查询的方法。否则,您应该使用更流行、经过充分测试的库来这样做。