GraphQL关系泄漏数据,即使已设置context.user解析器。如何通过关系防止数据泄露?

GraphQL关系泄漏数据,即使已设置context.user解析器。如何通过关系防止数据泄露?,graphql,graphql-js,graphene-python,Graphql,Graphql Js,Graphene Python,每个人如何跨关系进行身份验证以防止数据通过关系被遍历 例如,我们有一家拥有用户的商店 // Returns error as i've set custom resolver to allow only context.user.is_shop_owner { shops { name users { email ... } } } 此查询通常被诸如context.user.is\u shop\u owner之类的自定义解析程序阻止,因此

每个人如何跨关系进行身份验证以防止数据通过关系被遍历

例如,我们有一家拥有用户的商店

// Returns error as i've set custom resolver to allow only context.user.is_shop_owner
{
  shops {
    name
    users {
      email
      ...
    }
  }
}
此查询通常被诸如context.user.is\u shop\u owner之类的自定义解析程序阻止,因此无法从根查询执行此查询

然而,如果一个恶意的人通过关系到达用户对象,他就能够获得敏感的用户数据

// Data exposed un-intendedly due to relation traversal. How to prevent this?
{
  products {
    name
    price
    shop {
      users { ... } // boom, exposed
    }
  }
}
这是graphql中的一个缺陷吗?你们是怎么解决这个问题的

这是一个python石墨烯堆栈


编辑:顺便说一句,我知道我们可以排除_字段,但是我将无法从ShopNode访问用户,这是ShopNode查询的一个重要信息,因此限制字段可能不是一个好主意。已编辑

这可能应该控制在商店类型中,当用户没有正确的权限时,返回null。否则,如果从第二个字段访问商店,则必须重复检查。

我为每个节点设置自定义解析程序,以根据context.user阻止您要限制访问的关系。请参阅下面的代码以回答我的上述问题

class ProductNode(DjangoObjectType):
    class Meta:
        model = Product
        interfaces = (graphene.relay.Node)
        # Exclude nodes where you do not need any access at all
        exclude_fields = ['users']

    # For nodes where you need specific/limited access, define custom resolvers.
    # This prevents the relation traversal loophole
    def resolve_shop(self, args, context, info):
        """ Allow access to nodes only for staff or shop owner and manager """
        if get_is_staff_user(context.user, self):
            return self.shop

        Operations.raise_forbidden_access_error()

您是否使用Django进行此操作,返回用户时是否可以显示您的视图?@MauricioCortazar我不使用Django视图,抱歉!模式*我的意思是*你将如何实现它?