Firebase rules allowing write when they shouldn't be

Viewed 327

I have the following database entry: "companies: 8": enter image description here

I have the following database rules, which do not allow a simulated write of "companies: 8" to the database.

{
  "rules": {
      ".read": "auth != null",
      ".write": "auth != null",
        "companies": {  
              ".validate": "(data.exists() && (newData.val() === data.val() + 1)) || (!data.exists() && newData.val() == 0)" 
        }
  }
}

enter image description here

However, when I try to write "companies: 20" to the database with the Firebase Python SDK, which also is not allowed under these rules, it works:

In [1]: import firebase_admin

In [2]: from firebase_admin import credentials, db

In [3]: cred = credentials.Certificate('serviceAccountCredentials_dev_async.json
   ...: ')

In [4]: firebase_admin.initialize_app(cred, {'databaseURL': 'https://async-testi
   ...: ng.firebaseio.com/'})
Out[4]: <firebase_admin.App at 0x7fc50c00c080>

In [5]: ref = db.reference()

In [6]: ref.update({'companies': 20})

What am I doing wrong?

1 Answers
Related