【问题标题】:How to securely add authenticated user UID into firestore document?如何安全地将经过身份验证的用户 UID 添加到 Firestore 文档中?
【发布时间】:2020-11-26 11:23:01
【问题描述】:

我们可以使用经过身份验证的用户的 UID 安全地查询文档,使用这样的 Firestore 规则:

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 填充数据。

以下文档工作正常,但它是从客户端发送的,我认为它不安全,因为请求可能已篡改了另一个用户 UID。

{
  title: "A Great Story",
  content: "Once upon a time...",
  author: user.uid,
  published: false
}

基本上,我需要来自 Firebase 服务器的用户 UID 值,就像我们可以使用 firestore.FieldValue.serverTimestamp() 从服务器插入时间戳一样

【问题讨论】:

    标签: firebase google-cloud-firestore firebase-security


    【解决方案1】:

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

    通过在write 安全规则中添加request.auth.uid == request.resource.data.author 子句,您将保证请求不会被其他用户uid 篡改。

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


    文档herehere 以及here 中有关如何验证 ID 令牌完整性的更多说明。


    重要提示:请注意,我们使用request.resource.data.author 而不是resource.data.author,因为正如doc 中所述:

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

    request.resource 变量包含未来状态 文件。

    因此,您很可能应该将 readwrite 规则分开。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-02
      • 1970-01-01
      • 2018-08-18
      • 2017-12-27
      • 2021-06-23
      • 2018-04-29
      • 2018-03-19
      • 2013-08-20
      相关资源
      最近更新 更多