我一直在读到,用最大的查询深度保护你的应用程序是很重要的。也就是说,限制查询的“级别”数量。一个非常深入的查询示例:
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/
发布于 2018-11-23 04:25:21
您可以编写一个中间件来检查并阻止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()发布于 2019-10-07 18:36:14
处理这一问题的一种方法是使用Absinthe的复杂性分析功能。您可以为每个字段声明静态复杂度或用于计算动态复杂度的函数,并设置最大复杂度。
当查询达到最大复杂度时,会返回错误。
有关详细信息,请参阅官方文档:https://hexdocs.pm/absinthe/complexity-analysis.html
https://stackoverflow.com/questions/53287893
复制相似问题