Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you use security rules to prevent a user from faking Firebase.ServerValue.TIMESTAMP?

My app requires a reliable timestamp value for the creation of new records. I'm using Firebase.ServerValue.TIMESTAMP to ensure this. Here's my code:

var ref = new Firebase("https://test-firebase-please-ignore.firebaseio.com/foo");
ref.set(Firebase.ServerValue.TIMESTAMP);

or in REST:

$ curl -X PUT -d '{"foo":{".sv":"timestamp"}}' \
  https://test-firebase-please-ignore.firebaseio.com/.json 

How do I prevent an abusive user from crafting a request that looks valid, but is actually a fake timestamp in the past or future? Here's code they might use:

var ref = new Firebase("https://test-firebase-please-ignore.firebaseio.com/foo);
ref.set(1408643272324); //A timestamp in the past
like image 953
mimming Avatar asked Aug 21 '14 18:08

mimming


1 Answers

You can enforce this using a .validate rule and the now built in variable.

Here's a security rule that does this:

{
    "rules": {
        ".read": true,
        ".write": true,
        "foo" : {
          ".validate": "newData.val() == now"
          }
    }
}

You can verify it using the REST API with these cURL commands. First, a negative case:

$ curl -X PUT -d '{"foo":"1408638610143"}' \
https://test-firebase-please-ignore.firebaseio.com/.json

{
  "error" : "Permission denied"
}

And then a positive case:

$  curl -X PUT -d '{"foo":{".sv":"timestamp"}}' \
https://test-firebase-please-ignore.firebaseio.com/.json

{"foo":1408638609500}
like image 121
mimming Avatar answered Nov 15 '22 07:11

mimming