Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase security rule: compare two objects for equality?

So I want to write a security rule to disallow writes if a child node is different then it previously was. E.g. imagine a situation where you want a node to only be writable on creation but never thereafter. Given this requirement, the most obvious solution would seem to be to verify that the new data is equal to the old data.

Sadly, this does not work:

".write": "data.child('someNode').val() === newData.child('someNode').val()"

Nor a more sophisticated approach where I try to cast the object to a string:

".write": "!data.exists() || (data.child('someNode').val() + '') === (newData.child('someNode').val() + '')"

Any way this use case can be supported?

Important Note: The value at someNode has to be an object and can't simply be a string or other primitive. In the case of primitives, either of these methods work fine.

like image 522
Scottmas Avatar asked May 01 '15 21:05

Scottmas


1 Answers

Firebase's security rules do not support comparing JSON fragments. You'll have to compare every leaf node in your rules to test for equality.

E.g.

".validate": "
  data.child('someNode/username').val() == newData.child('someNode/username').val()
  && data.child('someNode/displayName').val() == newData.child('someNode/displayName').val()"
"
like image 78
Frank van Puffelen Avatar answered Oct 13 '22 06:10

Frank van Puffelen