Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In firebase, what security rules should I write to allow only push to object

I have messages object in root, which simply array of strings. I want to allow users push new objects. If they can push to messages root, they should have write permission to it, that's mean they can easily replace whole messages object with other valid data. What kind of security rules should I write to avoid this?

like image 577
user2757853 Avatar asked Apr 06 '15 06:04

user2757853


2 Answers

You can write a rule to allow adding data if data does not exist, but prevent data from being modified or deleted using the following rule.

".write": "!data.exists() && newData.exists()"

The 'data' variable is the current data in Firebase. 'newData' is the resulting data if the write is allowed.

For more detailed explanation, please check out Security & Rules API.

like image 67
Saeed D. Avatar answered Sep 26 '22 14:09

Saeed D.


The full rules definition should read

{
   "rules": {
        ".read": false,
        "feedback": {
            "$key": {
               ".write": "!data.exists()"
            }
        }
    }
}

This lets you push at the /feedback location. You can freely choose the variable name $key

like image 26
Thomas Handorf Avatar answered Sep 26 '22 14:09

Thomas Handorf