我有一个 Firebase,其安全配置如下:

{
    "rules": {
        "serviceproviders": {
            ".read": "auth != null",
            ".write": "auth != null"
        },
        "bookings": {
            ".read": "auth != null",
            ".write": true,
            ".validate": "newData.hasChildren(['phone', 'time', 'date', 'apikey'])",
            "apikey": {
                // only allow valid apikey
                ".validate": "root.child('serviceproviders/' + newData.val()).exists()"
            }
        },
        "status": {
            ".read": "auth != null",
            ".write": true
        }
    }
}

这个想法是用户只能发帖 /bookings/ 具有有效的 apikey, ,即一个 apikey 可以在 /serviceproviders/.

在 Firebase 模拟器中,这可以按预期工作。但是,当我使用 curl 从终端,或者从 html 页面的 Javascript,我得到 error: permission denied 从 Firebase 回来。我发送完全相同的数据(复制和粘贴)。

我的 curl 命令看起来像这样:

$ curl -X POST -d '{"phone":"004512345678", "date":"2014-07-31","time":"10:00","apikey":"AA227D80-122C-4E5D-AEDF-24A829FA6403"}'  https://example.firebaseIO.com/bookings/.json

我回来了:

{
  "error" : "Permission denied"
}
有帮助吗?

解决方案

好吧,经过几个小时的纠结,我意识到在 Firebase.com 的指南中, ".validate" 规则位于表示该路径下 ID 的块内,因此:

{
    "rules": {
        "serviceproviders": {
            ".read": "auth != null",
            ".write": "auth != null"
        },
        "bookings": {
            ".read": "auth != null",
            ".write": true,
            "$bookings_id": {
                "apikey": {
                    // only allow valid apikey
                    ".validate": "root.child('serviceproviders/' + newData.val()).exists()"
                },
                ".validate": "newData.hasChildren(['apikey','date','time','phone'])"
            }
        },
        "status": {
            ".read": "auth != null",
            ".write": true
        }
    }
}

按预期工作,因为 "$bookings_id" 堵塞。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top