【问题标题】:Firebase Security rules, disable write on specific nodeFirebase 安全规则,禁用特定节点上的写入
【发布时间】:2018-03-14 16:04:34
【问题描述】:

我的 firebase 结构看起来像这样:

{
"post": {
    "uid": {
        "text": "Name";
    }
},
"games": {
    "id": {
        "title": "buttons",
        "text": "(user id string)"
    },
    "id": {
        "title": "navbars",
        "text": "(id string)"
    }
},
"guides": {
    "1": {
        "title": "guide",
        "text": "unwriteable string"
    }
}
}

(值无所谓..)

我想允许在每个节点上进行读写,执行指南节点,

所以我尝试了以下规则:

{
  "rules": {
    ".read": "auth == null",
    ".write": "auth == null",
      "guides": {
        ".write": false
      }
  }
}

但是。不幸的是,由于“父亲”津贴,firebase 并不关心指南的具体规则,

知道如何实现我的目标吗?

【问题讨论】:

  • 您的意思是auth != nullauth == nulltrue 是一回事
  • @Kato 我不认为这是一样的。如果我的这条规则是正确的,只有没有身份验证的人才能写。

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


【解决方案1】:

因为firebase security rules cascade 你不能说某人有权在任何地方写作,然后说但不是在这里。

因此,在您的情况下,您必须为其他路径添加规则,如下所示:

{
  "rules": {
    ".read": "auth == null",
      "guides": {
        ".write": false
      },
      "games": {
        ".write": "auth == null"
      },
      "post": {
        ".write": "auth == null"
      }
  }
}

正如加藤所说,这也可以通过以下规则来完成:

{
  "rules": {
    ".read": "auth == null",
    ".write": "auth == null && !newData.hasChild('guides')"
  }
}

第一个示例只允许您在gamespost 节点中写入,而第二个示例将允许您在除guides 节点之外的任何地方写入。

【讨论】:

  • 绝对是最好的方法,但如果我们想分割头发,在技术上并不准确。你可以把这样的东西放在根目录:".write": "auth != null && !newData.hasChild('guides')"
  • @Kato 你是对的。我根据问题中的结构来回答这个问题。我个人喜欢限制滥用的机会。明天我有更多时间时会更新答案,或者如果您愿意,可以这样做。
  • 我不认为这种方法是滥用,尽管它可能很容易陷入困境。想象一下我有 50 条路径但只想限制读取其中一条的用例。必须写 49 条".read": true 规则绝对是矫枉过正:)
  • @Kato 我更新了答案。随时纠正我犯的任何错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-12
  • 1970-01-01
相关资源
最近更新 更多