【发布时间】:2019-11-11 12:59:46
【问题描述】:
我正在尝试弄清楚如何在现场级别保护一对多 @connection 和 @auth 免受不应允许的突变。 (即:拒绝特定用户运行突变,最终以另一个用户的身份插入帖子。)
从在字段级别保护突变的示例开始:https://aws-amplify.github.io/docs/cli/graphql#field-level-authorization
我试着做这样的事情:
type User @model @auth(rules: [{ allow: owner, ownerField: "id" }]) {
id: ID!
posts: [Post]
@connection(name: "UserPosts")
@auth(rules: [{ allow: owner, ownerField: "id" }])
}
type Post @model {
title: String!
user: User!
@connection(name: "UserPosts")
@auth(rules: [{ allow: owner, ownerField: "userPostId" }])
}
然后说已经有一个 id 为regular-user-id 的用户
显然,我的身份验证规则不会阻止其他用户,例如使用 id 为:malicious-user-id 来运行此突变:
mutation {
createPost(input:{
title:"Oh this is BAD!"
postUserId: "regular-user-id"
}) {
title
}
}
运行一个简单的查询以确保这确实发生了:
query {
getUser(id:"regular-user-id"){
posts{
items
{
title
}
}
}
}
=>
{
"data": {
"getUser": {
"posts": {
"items": [
{
"title": "Regular User title"
},
{
"title": "Oh this is BAD!"
},
]
}
}
}
}
我尝试了各种方法来解决这个问题,但找不到任何有关双向字段级别身份验证的文档。我对 AppSync 还很陌生,所以我想我一定没有得到任何东西,但这是如此常见的用例场景,我真的很惊讶没有更多关于它的文档。
我们将不胜感激。
【问题讨论】:
标签: graphql aws-amplify aws-appsync