【发布时间】:2020-05-27 11:11:34
【问题描述】:
我正在使用云 Firestore 数据库,我的根数据库上有“用户”集合,其中包含所有用户文档(命名为 uid),其中包含我收集的数据。 我想让用户只能读/写他们自己的集合,所以我开始我的安全规则
service cloud.firestore {
match /databases/{database}/documents {
// Allow only authenticated content owners access
match /users/{userId}/{documents=**} {
allow read, write: if request.auth.uid == userId
}
}
}
然后我意识到用户没有创建自己的主文档的权限(它将其创建为“幽灵”文档)。我还想给它添加数据,例如显示名称和电子邮件,所以我添加了
// Allow users to read/write their own main user object
match /users/{userId} {
allow read, write: if request.auth.uid == userId
}
一切正常,但现在我想允许管理员用户读取/写入所有用户的集合和文档。
firebase's documentations suggests 向用户的文档添加一个键值对,例如 (admin:true),所以像这样编辑我的安全规则应该可以解决问题:
match /users/{userId}/{documents=**} {
allow read, write: if request.auth.uid == userId
allow read, write: if get(/users/$(request.auth.uid)).data.admin == true; //-security breach?
}
但这不会造成安全漏洞,用户可以在技术上使用(admin:true) 更新他自己的文档并获得管理员权限吗?
如果是这样,我猜我的方法应该是创建一个单独的 admins 集合并在那里添加我的管理员,然后在
allow read, write: if get(/admins/$(request.auth.uid)) != null
?或者有什么更漂亮的我可以使用?
【问题讨论】:
标签: firebase google-cloud-firestore firebase-authentication firebase-security