【问题标题】:Firebase Rules ConfusionFirebase 规则混乱
【发布时间】:2017-03-13 03:09:05
【问题描述】:

我对 Firebase 规则结构感到非常困惑。基本上,我有一个名为“Transactions”的根文件夹,其中包含许多 childByAutoID,它们又包含数据,就像这样:

Transactions {
    autoID1 {
        transactionID: whatever,
        timeCreated: whatever,
        "amount": whatever,
        "sender": whatever,
        "receiver:whatever"
    }
}

我现在正在尽最大努力实施最好的安全规则。因此,我尝试做这样的事情:

"Transactions": {
    ".write": "auth !== null",
    "$transactionAutoID": {
        "timeCreated": {
            ".write": "auth !== null && data.child('sender').val() === auth.uid || data.child('receiver').val() === auth.uid",
            ".read": "auth !== null && data.child('sender').val() === auth.uid || data.child('receiver').val() === auth.uid"
        }
    }
}

所以基本上,根据我所学到的以及我正在努力解决的问题 - 用户能够编写任何内容,即使在“通配符 ID ($transactionAutoID)”中也是如此。我知道这是因为我已将 Transactions 中的 '.write' 设置为 'auth !== null' - 但如果我没有此设置,用户可能无法读取或写入任何交易数据。 (我将规则默认设置为 false)。

如果我只希望用户能够在 Transactions 中创建新子项,但不写入任何事务密钥(如果他们不是发送方或接收方),我将如何进行?

【问题讨论】:

    标签: firebase-realtime-database firebase-security


    【解决方案1】:

    一旦在节点上授予权限,就不能在较低级别的节点上取消。

    所以如果你看这个:

    "Transactions": {
        ".write": "auth !== null",
        "$transactionAutoID": {
            "timeCreated": {
                ".write": "auth !== null && data.child('sender').val() === auth.uid || data.child('receiver').val() === auth.uid",
                ".read": "auth !== null && data.child('sender').val() === auth.uid || data.child('receiver').val() === auth.uid"
            }
        }
    }
    

    Transactions/$transactionId/timeCreated 上的写规则毫无意义,因为它试图收紧来自Transactions 的权限。

    所以现在它的功能是这样的:

    "Transactions": {
        ".write": "auth !== null",
        "$transactionAutoID": {
            "timeCreated": {
                ".read": "auth !== null && data.child('sender').val() === auth.uid || data.child('receiver').val() === auth.uid"
            }
        }
    }
    

    如果您想对用户可以在 Transactions/$transactionId/timeCreated 上执行的操作进行具体限制,您必须在验证规则中执行此操作,或者(更有可能)将所有规则放在事务本身上:

    "Transactions": {
        "$transactionAutoID": {
            ".write": "auth !== null && (
                !data.exists() || (
                    newData.child('sender').val() === auth.uid ||
                    newData.child('receiver').val() === auth.uid)"
                )
            )"
        }
    }
    

    简而言之:当您通过身份验证并且或者该交易尚不存在您是该交易的发送者或接收者时,您可以编写一个交易.

    【讨论】:

    • 嗨弗兰克。谢谢你整理出来。但是,考虑到我的默认设置为 false,那么在“交易”下尝试创建新孩子是否会被拒绝?因为我在 wildCardID INSIDE Transactions 里面只有一个写规则?
    • 我提供的规则将允许您添加特定的交易。试试吧。如果您无法使其正常工作,请显示失败操作的代码(或至少显示您写入的位置和您正在编写的 JSON)。
    • 我明白了。但是,我也很好奇(如果)可以在唯一事务中定义新的写权限吗?顺便说一句,不幸的是我没有让它工作。错误消息:“模拟器请求被拒绝”。
    猜你喜欢
    • 2020-07-09
    • 1970-01-01
    • 2011-02-19
    • 1970-01-01
    • 2011-11-01
    • 2020-09-28
    • 2018-04-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多