【问题标题】:Adding Security to Firebase to prevent the insertion of additional data向 Firebase 添加安全性以防止插入其他数据
【发布时间】:2016-05-23 16:08:16
【问题描述】:

我在 Firebase 中有一个名为“pending_members”的字段,其中包含等待“所有者”授予权限的成员列表,因此,“pending_members”需要以下规则:

  1. 当前用户只能添加自己(uid)
  2. 当前用户只能从列表中删除自己 (uid)
  3. “所有者”可以从列表中删除任何成员
  4. 只有“所有者”才能读取列表

我尝试了各种安全规则,但似乎遗漏了许多极端情况,例如,用户被授予写入权限,因为数据包含他们的 uid,但随后他们可以提交其他人的 uid。

任何人都可以针对这种情况提出适当的规则吗?非常感谢

"pending_members" : {
    ".write" : "auth !== null && 
        // The user is authenticated AND
        (newData.child(auth.uid).exists() ||
        // The new data contains either the current user's id OR
        (!newData.exists() &&  
        // There's no new data (a delete operation) AND
        data === auth.uid))",
        // The old data is the current user's id

"$member" : {
    ".validate" : "newData.isString()",
        "$other": { ".write": false, ".read": false }
    }
}

编辑: 结构示例:

users       ->
                personal_data   ->
                                    email                   (user email address)
                                    first_name              (user first name)
                                    last_name               (user last name)
                networks_index  ->
networks    ->
                members                 (list of uids of users linked to the network)
                owner                   (uid of the owner/primary user)
                pending_members         (list of uids of users wishing to link to the network)

Data Example (image)

【问题讨论】:

  • 你能添加一个你的firebase结构的例子吗?
  • @AndréKool 按要求添加。

标签: firebase firebase-security


【解决方案1】:

你有复杂的结构,但我会试一试: 请记住,读写的标准值是 false。

    {
  "rules": {
    "networks": {
      "$networkid": {
//Give read and write access to the owner of the network 
        ".read": "auth != null && "root.child('networks').child($networkid).child('owner').val() == auth.uid",
        ".write": "auth != null && "root.child('networks').child($networkid).child('owner').val() == auth.uid",
        "pending_members": {
           "$uid": {
              //Give members write access to their own node inside pending_members
              ".write": "auth != null && auth.uid == $uid",
              //Use validate to check if the value is a bool or emty(removal)
              ".validate": newData.isBoolean() || !newData.exists()
      }
    }
  }
}

我这里只关注pending_members,希望够了,也够清楚了。如果它不起作用,我建议单独测试每条规则,看看是哪一条导致了问题,以便我(或其他人)可以帮助解决它。

【讨论】:

    猜你喜欢
    • 2016-12-18
    • 1970-01-01
    • 2011-11-20
    • 1970-01-01
    • 2013-07-29
    • 2012-05-19
    • 1970-01-01
    • 2016-02-25
    • 2017-12-10
    相关资源
    最近更新 更多