【问题标题】:How to restrict access to data that are not assigned to a specific user?如何限制对未分配给特定用户的数据的访问?
【发布时间】:2013-08-29 07:02:36
【问题描述】:

让我们以http://up2f.co/15euYdT 为例,通过检查是否只有评论的创建者可以更改评论,可以保护 Firebase 应用。

假设我们需要在另一个结构中保存 cmets 的总数,例如

"stats" :{
    "comments":{
      "count":2
    },
  }

我们需要保护这部分免受注册用户的直接访问。我们可以这样做

"stats" :{
    "$adminid":{
        "comments":{
          "count":2
        },
    },
  }

我们只能允许管理员访问那里。

为此,我们需要创建与 Firebase 的持久连接,该连接将监听 cmets 表中的更改并触发事件以更新 stats 表。

这可能吗?如果不是,我们还能如何保护未分配给特定用户的数据?

【问题讨论】:

    标签: firebase firebase-security


    【解决方案1】:

    由于您的管理进程将使用秘密令牌登录,因此安全规则将不适用。因此,您可以使用以下方法简单地保护客户端访问:

    // not applied to privileged server logging in with token
    ".write": false,
    

    如果您希望客户增加金额,您可以使用以下技巧,该技巧只允许他们增加计数器,并且只允许他们在计数器已更新时添加注释。 (查看工作演示 http://jsfiddle.net/katowulf/5ESSp/

    {
      "rules": {
        ".read": true,
        ".write": false,
        "incid": {
           "counter": {
              // this counter is set using a transaction and can only be incremented by 1
              ".write": "newData.isNumber() && ((!data.exists() && newData.val() === 1) || newData.val() === data.val()+1)"
           },
           "records": {
             "$id": {
                // this rule allows adds but no deletes or updates
                // the id must inherently be in the format rec# where # is the current value of incid/counter
                // thus, to add a record, you first create a transaction to update the counter, and then use that counter here
                // the value must be a string less than 1000 characters
                ".write": "$id >= 'rec'+root.child('incid/counter').val() && !data.exists() && newData.isString() && newData.val().length <= 1000"
             }
           }
        }
      }
    }
    

    【讨论】:

    • 是的,但仍然可以在不输入 cmets 的情况下增加计数器,请参阅 jsfiddle.net/rm6T9(注释掉第 35 行)。我正在尝试一种用户只添加评论和计数器因具有特定用户 ID 的持久监听连接而增加。我说的是这样的结构:gist.github.com/abinop/6379843 只有用户 333333 才能更改总数和平均值。
    • 您可以为此进行调整。使用特权工作者(即服务器进程)也没有什么可耻的。
    猜你喜欢
    • 2017-12-04
    • 1970-01-01
    • 2015-06-16
    • 1970-01-01
    • 1970-01-01
    • 2020-11-13
    • 2010-09-17
    • 2015-12-15
    • 1970-01-01
    相关资源
    最近更新 更多