【问题标题】:Unable to validate child nodes with Firebase security无法使用 Firebase 安全性验证子节点
【发布时间】:2016-02-16 22:58:29
【问题描述】:

我显然错过了 firebase 安全的一些基本方面,因为这不应该工作。我希望它在尝试推送无效数据时会引发验证错误。 (在 /nodes 中插入一个新节点)

规则:

{
  "rules": {
    "nodes": {
      ".read": "auth !== null && auth.provider === 'google'",
      ".write": "auth !== null && auth.provider === 'google'",
      "user": {
        ".validate": "newData.val() === auth.uid"
      },
      "ts": {
        ".validate": "newData.val() <= now && newData.val() >= (now-1000*60*60*24)"
      }
    }
  }
}

然后在我的控制台中我尝试故意插入无效数据:

ref.child('nodes').push({
  'user': 'abc',
  'ts': 123
}, function(err){console.log(err);});

哪个记录为空,当我检查我的数据库是否已插入时,没有验证错误!我知道我有一些根本性的错误,因为下面的 .read 和 .write 行之后的验证规则不允许任何写入。 .validate": "newData.hasChildren(['user', 'ts'])",

{
  "nodes" : {
    "-KAgH0BLneWfGu8NymBo" : {
      "ts" : 123,
      "user" : "abc"
    }
  }
}

【问题讨论】:

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


    【解决方案1】:

    哎呀。缺少“$node_id”

    {
      "rules": {
        "nodes": {
           "$node_id":{
             ".read": "auth !== null && auth.provider === 'google'",
             ".write": "auth !== null && auth.provider === 'google'",
             "user": {
               ".validate": "newData.val() === auth.uid"
             },
             "ts": {
               ".validate": "newData.val() <= now && newData.val() >= (now-1000*60*60*24)"
             }
           }
        }
      }
    }
    

    【讨论】:

    • 感谢本杰明的反馈。错过其中的通配符/$ 变量确实是一个常见的错误。
    猜你喜欢
    • 1970-01-01
    • 2019-08-31
    • 1970-01-01
    • 2018-12-31
    • 1970-01-01
    • 2021-06-12
    • 2020-12-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多