【问题标题】:Firestore security rule not workingFirestore 安全规则不起作用
【发布时间】:2019-01-31 00:49:53
【问题描述】:

我的 Firestore 中有以下规则

service cloud.firestore {
      match /databases/{database}/documents {
        match /users/{userId}/{documents=**} {
          // Only the authenticated user who authored the document can read or write
          allow read: if request.auth.uid == userId;
          allow write;
        }
      }
    }

这似乎不起作用,我正在使用 Rest API 来获取数据 对于身份验证,我调用: https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyPassword?key=[API_KEY]

一旦通过身份验证,我们将获得 idToken 并作为下一个 URL 的授权标头传递 https://firestore.googleapis.com/v1beta1/projects//databases/(默认)/documents/users

users 集合以 id 作为文档名称,而 value 只是一堆虚拟键。

当我运行客户端时,我得到的错误是

{u'status': u'PERMISSION_DENIED', u'message': u'权限缺失或不足。', u'code': 403}

如果我硬编码用户 ID 的值,它就可以工作。因此,{userid} 中返回的值由于某种原因似乎与 UID 不匹配。

有人能帮忙解释一下为什么会这样吗?

谢谢 公羊

【问题讨论】:

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


    【解决方案1】:

    你不需要 document=** 选择器

    服务云.firestore { 匹配 /databases/{database}/documents { //禁止所有访问 匹配 /{documents=**} { 允许读、写:如果为假; } // 确保请求用户的 uid 与用户名匹配 // 文档。通配符表达式 {userId} 使 userId 变量 // 在规则中可用。 匹配 /users/{userId} { 允许读取、更新、删除:如果 request.auth.uid == userId; 允许创建:如果 request.auth.uid != null; } } }

    https://firebase.google.com/docs/firestore/security/rules-conditions

    【讨论】:

    • 感谢您的回复,我已经尝试了在 false 之前添加“if”的解决方案,我得到了与 PERMISSION_DENIED 相同的结果。
    • 那我们先看看简单的解决方案。你在控制台中测试吗?如果是这样,请确保您传递的是授权用户 ID。您的身份验证 ID 是该节点上的 userId 吗?如果没有,那么您正确地获得了 permission_denied
    猜你喜欢
    • 2018-03-19
    • 2021-05-12
    • 2021-11-22
    • 2019-09-06
    • 2018-05-28
    • 2020-08-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多