Firebase 如何安全地将经过身份验证的用户UID添加到firestore文档中?

Firebase 如何安全地将经过身份验证的用户UID添加到firestore文档中?,firebase,google-cloud-firestore,firebase-security,Firebase,Google Cloud Firestore,Firebase Security,我们可以使用以下firestore规则,使用经过身份验证的用户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

我们可以使用以下firestore规则,使用经过身份验证的用户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 != null && request.auth.uid == resource.data.author;
    }
  }
}
但是我找不到如何用经过身份验证的用户UID填充数据

{
  title: "A Great Story",
  content: "Once upon a time...",
  author: user.uid,
  published: false
}
下面的文档工作正常,但它是从客户端发送的,我认为它不安全,因为请求可能篡改了另一个用户UID

{
  title: "A Great Story",
  content: "Once upon a time...",
  author: user.uid,
  published: false
}
基本上,我需要Firebase服务器的用户UID值,就像我们可以使用
firestore.FieldValue.serverTimestamp()从服务器插入时间戳一样

我认为它不安全,因为请求可能会被篡改 另一个用户UID

{
  title: "A Great Story",
  content: "Once upon a time...",
  author: user.uid,
  published: false
}
通过在
write
安全规则中添加
request.auth.uid==request.resource.data.author
子句,您将保证请求不会被其他用户uid篡改

此子句验证字段
author
的值是否对应于执行写入操作的用户的uid


文档中有更多解释,以及如何验证ID令牌的完整性


重要提示:请注意,我们使用
request.resource.data.author
而不是
resource.data.author
,原因如下:

resource
变量引用请求的文档,并且
resource.data
是存储在 文件

request.resource
变量包含 文件

因此,您很可能应该将
读取
写入
规则分开