【问题标题】:Absinthe Graphql nested queries securityAbsinthe Graphql 嵌套查询安全性
【发布时间】:2019-04-16 16:37:42
【问题描述】:

我一直在读到,使用最大查询深度来保护您的应用程序非常重要。意思是,限制查询的“级别”数量。一个非常深的查询示例:

query IAmEvil {
  author(id: "abc") {
    posts {
      author {
        posts {
          author {
            posts {
              author {
                # that could go on as deep as the client wants!
              }
            }
          }
        }
      }
    }
  }
}

如何知道查询的深度?并最终不允许执行深度超过 4 的查询。 我可以获得完整的查询以手动计算深度吗?还是他们已经实现了?

这里也描述了这个问题:https://www.howtographql.com/advanced/4-security/

【问题讨论】:

    标签: graphql absinthe


    【解决方案1】:

    您可以编写一个中间件来检查 selection_depth 并阻止它。类似的东西:

    @impl Absinthe.Middleware
    def call(res, _config) do
      IO.inspect(selection_depth(res.definition.selections))
      res
    end
    
    def selection_depth([], depth), do: depth + 1
    def selection_depth(selections, depth \\ 0),
      do: selections |> Enum.map(&selection_depth(&1.selections, depth + 1)) |> Enum.max()
    

    【讨论】:

      【解决方案2】:

      解决此问题的一种方法是使用苦艾酒的复杂性分析功能。您可以为每个字段声明静态复杂度或用于计算动态复杂度的函数,并设置最大复杂度。

      如果查询达到最大复杂度,将返回错误。

      查看官方文档了解更多信息:https://hexdocs.pm/absinthe/complexity-analysis.html

      【讨论】:

        猜你喜欢
        • 2021-01-29
        • 2020-12-02
        • 1970-01-01
        • 2018-06-13
        • 2019-05-30
        • 2021-01-14
        • 2017-06-16
        • 2017-07-17
        • 1970-01-01
        相关资源
        最近更新 更多