質問

このようなセキュリティ設定を備えた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を使用すると、FireBaseからerror: permission deniedが戻ります。私はまったく同じデータ(コピー&ペースト)を送ります。

my 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"
}
.

役に立ちましたか?

解決

OK、そう髪の毛を引っ張るのに何時間もの後、私は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