graphql - Absinthe Graphql 嵌套查询安全性
问题描述
我一直在阅读,使用最大查询深度来保护您的应用程序很重要。意思是,限制查询的“级别”数量。一个非常深的查询示例:
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/
解决方案
您可以编写一个中间件来检查 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()
推荐阅读
- php - 如何获取 json 中对象的值以进行一些操作并使用新的对象值重建 json?
- typescript - 如何从对象中选择一个值?
- angular - 以编程方式重新验证表单
- javascript - HTML 表单提交和表单不刷新时显示 HTML
- apache-kafka-streams - Kafka Streams 是否忽略不带前缀的 ProducerConfig 属性?
- mysql - 如何计算当前订单之前的订单数量
- c# - 如何解决从 UWP 应用程序读取文件时 Permission denied 的问题?
- java - 让 Thymeleaf 读取对象模型数组
- amazon-web-services - CloudWatch 日志定价混乱
- python - 运行 npm install 时,使用 pxssh 建立的 ssh 连接会中断。为什么?