Firestore 文档shows https://firebase.google.com/docs/firestore/security/rules-query examples https://firebase.google.com/docs/firestore/solutions/role-based-access如何使用基于 Firestore 的安全规则来保护数据request.auth.uid
场地。这些通常看起来像这样:
service cloud.firestore {
match /databases/{database}/documents {
match /stories/{storyid} {
// Only the authenticated user who authored the document can read or write
allow read, write: if request.auth.uid == resource.data.author;
}
}
}
这完全有道理。
我不明白(并且似乎没有在任何地方显示)是如何设置resource.data.author
安全地进行现场操作。
显然,这不能仅仅基于客户端,因为任何经过身份验证的用户都可以篡改请求以将其作者设置为任何值。
我想也许我们应该使用 CloudFunctions 来设置该字段,但是目前这不起作用 https://stackoverflow.com/questions/49825566/firebase-function-context-auth-and-context-authtype-undefined/49827227#49827227.
其影响在基于角色的访问示例 https://firebase.google.com/docs/firestore/solutions/role-based-access:
{
user: "alice",
content: "I think this is a great story!"
}
当然,必须有一种防篡改的方法来设置用户字段——否则任何用户都可以让他们的评论看起来像是来自其他人。这看起来很糟糕。
在 Firestore 示例 Web 应用程序中,似乎设置了客户端的userId字段 https://github.com/firebase/quickstart-js/blob/master/firestore/scripts/FriendlyEats.View.js#L164我认为它正在做Android版本也一样 https://github.com/firebase/quickstart-android/blob/1791a58ffe13f364dfc1b39356ca0ec8fbf0d7f5/firestore/app/src/main/java/com/google/firebase/example/fireeats/RatingDialogFragment.java#L73.
我缺少什么?
编辑:正如 @imjared 指出的,这条规则意味着“alice”user: "alice"
实际上是一个uid,所以我认为这是安全的。
我知道我错过了一些东西。
match /comments/{comment} {
allow read: if isOneOfRoles(get(/databases/$(database)/documents/stories/$(story)),
['owner', 'writer', 'commenter', 'reader']);
allow create: if isOneOfRoles(get(/databases/$(database)/documents/stories/$(story)),
['owner', 'writer', 'commenter'])
&& request.resource.data.user == request.auth.uid;