【问题标题】:Firebase rules result in odd behaviourFirebase 规则导致奇怪的行为
【发布时间】:2017-07-21 04:56:52
【问题描述】:

对于下面设置的 Firebase 规则。

"users": {

    //".read": "(auth != null) && (auth.uid === newData.child('id').val())",

  // A list of users and their associated metadata, which can be updated by the single user or a moderator.
  "$userId": {
    ".write": "(auth != null) && (auth.uid === $userId)",
    ".read": "(auth != null) && (auth.uid === $userId)",
    ".validate": "($userId === newData.child('id').val())",
    "invites": {
      // A list of chat invitations from other users, append-only by anyone.
      "$inviteId": {
        // Allow the user who created the invitation to read the status of the invitation.
        ".read": "(auth != null) && (auth.uid === data.child('fromUserId').val())",
        ".write": "(auth != null) && (!data.exists() || $userId === auth.uid || data.child('fromUserId').val() === auth.uid)",
        ".validate": "newData.hasChildren(['fromUserId','fromUserName','roomId']) && (newData.child('inviteId').val() === $inviteId)"
      }
    }
    }   
}

当我以用户 2456gd 的身份尝试规则模拟器时,URL /users/2456gd/ - $userId 下的验证步骤成功。但是如果尝试/users/2456gd/invites/abcd2 - $userId 下的验证步骤会失败。

我使用的数据:

{
"id":"2456gd",
"fromUserId":"2456gd",
"fromUserName":"abc",
"roomId":"family",
"inviteId":"abcd2"
}

有什么可能的解释吗?以及如何处理它。

谢谢。

【问题讨论】:

  • 什么失败 readwriteboth
  • 第一个 url 读取成功:/users/2456gd/,但第二个也失败。
  • 解析确切的失败有点困难。你能分享一下你的模拟器的截图吗?
  • 我已经添加了截图。谢谢。
  • 嗯....我真的不明白这有什么问题。也许其他人发现了它。我通常会将这种类型的检查放在.write 规则中(这不是关于数据结构,而是更多关于授权)。但这不应该对结果产生影响。

标签: json firebase firebase-realtime-database firebase-security


【解决方案1】:

正如你在这张照片中看到的

验证在/users/$userId/ 失败,因为那里的数据没有id 作为其子项。 /user/$userId 的数据看起来像这样

{
    "invites": {
        "abcd2": {
            "id":"2456gd",
            "fromUserId":"2456gd",
            "fromUserName":"abc",
            "roomId":"family",
            "inviteId":"abcd2"
        }
    }
}

由于该数据没有id,因为它的子验证失败。它只有一个孩子invites

【讨论】:

  • @user2536851 将.validate$userId 移动到$inviteId 应该可以工作。
猜你喜欢
  • 2021-06-10
  • 2017-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-27
  • 2023-03-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多