【问题标题】:Firebase database security rulesFirebase 数据库安全规则
【发布时间】:2016-09-16 05:17:59
【问题描述】:

我真的在为 firebase 数据库安全规则而苦苦挣扎。

我正在开发一个简单的应用程序,用户可以在其中维护记录列表,任何人都可以阅读,但只有创建记录的用户可以编辑或删除。

只需将其视为待办事项应用程序,经过身份验证的用户可以管理任务列表。用户可以互相阅读任务列表,但只有创建任务的用户才能删除或编辑它。

我尝试了不同的选项,但未能提出正确的安全策略。

是否有任何我可以遵循的示例或关于如何构建数据以简化安全策略的任何建议?

安全规则示例

我尝试过这样的事情-

"rules": {
"records" : {
  ".read" : true,
    "$user_id": {
         ".write": "auth.uid === $user_id"
        }
    }
  }
}

这样任何人都可以读取记录,但只有具有匹配 user_id 的登录用户才能添加、编辑或删除记录。

但是,当我在模拟器中尝试写入操作时,我总是收到 write denied 错误。我在请求正文/数据中将身份验证 uid 作为“user_id”传递。

我也试过了

{
 "rules": {
 "records" : {
   ".read" : true,
    ".write": "auth.uid === data.child('user_id').val()"
     }
   }
}

【问题讨论】:

  • 你应该发布你尝试过的东西以及它有什么问题。
  • @api55 我已经用示例安全规则更新了问题。

标签: security firebase firebase-realtime-database


【解决方案1】:

我在文档中找到了这个。节点用户的子节点以 auth.uid 作为键存储。因此,您基本上在写入该条目之前将用户 uid 与密钥进行比较:

{
  "rules": {
    "users": {
      "$user_id": {
        // grants write access to the owner of this user account
        // whose uid must exactly match the key ($user_id)
        ".write": "$user_id === auth.uid",
        ".read": true
      }
    }
  }
}

https://www.firebase.com/docs/security/guide/user-security.html

【讨论】:

    【解决方案2】:

    尝试添加以下节点 -> "$record_id": {}

    {
     "rules": {
     "records" : {
       "$record_id": {
          ".read" : true,
           ".write": "auth.uid === data.child('user_id').val()"
            }
          }
       }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-08-08
      • 2018-12-15
      • 1970-01-01
      • 2020-04-15
      • 2020-12-28
      • 2021-10-26
      • 2021-07-14
      • 2021-02-13
      相关资源
      最近更新 更多