【问题标题】:Firebase Security Rules allowing conditional .readFirebase 安全规则允许有条件的 .read
【发布时间】:2017-06-13 12:42:54
【问题描述】:

我想创建一个 firebase 规则,如果该子项的属性具有特定值,则该规则允许用户读取和写入子项。假设我的数据库如下所示:

"tasks": {
    "SDkh7s62jnd23d9": {
        "uid": "someuserid",
        "other": "datagoes here"
    }
}

这是我当前的安全规则:

"tasks": {
    ".indexOn": "_state",
    "$registerQueueKey": {
        ".read": "data.child('uid').val() == auth.uid",
        ".write": "newData.child('uid').val() == auth.uid"
    }
}

此规则限制用户写入权限,但它绝不允许用户读取,即使他们尝试读取的子级具有等于 auth.iduid 属性。

然后我测试了以下规则集:

"tasks": {
    ".indexOn": "_state",
    "$registerQueueKey": {
        ".read": true,
        ".write": "newData.child('uid').val() == auth.uid"
    }
}

尽管将.read 权限设置为永久true 值,用户仍然无法读取$registerQueueKey 子级。

然后我测试了以下规则集:

"tasks": {
    ".indexOn": "_state",
    ".read": true,
    "$registerQueueKey": {
        ".write": "newData.child('uid').val() == auth.uid"
    }
}

现在我可以很好地阅读孩子,所以我尝试了这个最终的安全规则:

"tasks": {
    ".indexOn": "_state",
    ".read": "data.child($registerQueueKey").child('uid').val() == auth.uid",
    "$registerQueueKey": {
        ".write": "newData.child('uid').val() == auth.uid"
    }
}

但此规则会引发错误,因为变量 $registerQueueKey 在它正在使用的范围内未定义。

如何完成这样的规则?

【问题讨论】:

  • Firebase 在您附加侦听器的位置检查读取授权。因此,您要么可以从该位置读取(从而读取其下的所有内容),要么您无法从该位置读取。这意味着您不能使用安全规则来过滤您在此处尝试的内容。这在文档中称为rules are not filters,在this answerthese other questions 中。
  • @FrankvanPuffelen 那么我是否需要重组我的数据库,以便我尝试检查的值是那个孩子的关键?
  • 那将是一种确实可行的模型。

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


【解决方案1】:

我认为错误是因为您没有放置通配符:

"tasks": {
  "$uid": {
    ".indexOn": "_state",
    ".read": "data.child($registerQueueKey").child('$uid').val() == auth.uid",
    "$registerQueueKey": {
        ".write": "newData.child('$uid').val() == auth.uid"
    }
  }
}

【讨论】:

    猜你喜欢
    • 2020-07-11
    • 1970-01-01
    • 2021-07-14
    • 2016-02-26
    • 1970-01-01
    • 2021-07-08
    • 1970-01-01
    • 1970-01-01
    • 2015-03-25
    相关资源
    最近更新 更多