CURLとJavaScriptを使用してFireBaseから許可されています
-
21-12-2019 - |
質問
このようなセキュリティ設定を備えた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"
ブロックのために、予想通りに機能します。
所属していません StackOverflow