Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase security time rule using 'now'

I have a Firebase security rule trying to allow for writes if the field timstamp has not yet passed i.e modifications should be allowed until time has moved past the time set in the timestamp data field.

"tips": {
    ".read": true,
    "$user_id": {
    "tips": {
      "$game_id": {
      ".write": "root.child('/games/' + $game_id + '/timestamp/').val() > now"
      }
    } 
  }

In trying to get this rule to work I have 2 sample timestamp fields of:

timestamp: 1593840696 
timestamp: 1393840696

Although I'm not verifying what 'now' is I understand it to be epoch time and current date/time is around 1493840696. When writing to the Firebase I would expect the first entry to succeed with an update (it's timestamp is after 'now' time) and the second to fail as it is in the past.

What I am seeing though is that neither record is allowed to update with this rule? If I change the greater than '>' to less than '<' they both work. Strange? It would seem the value of 'now' may be greater than the 1593840696.

".write": "root.child('/games/' + $game_id + '/timestamp/').val() < now"

What would the value of 'now' be in this context? Any suggestions on how to work through/debug and achieve the required timestamp protection? Essentially need to protect/allow data changes until after the timestamp field has elapsed.

I can't get this rule to fire and protect the data so any help would be most appreciated.

Thanks for any responses.

like image 862
ngwp Avatar asked Oct 14 '25 10:10

ngwp


1 Answers

The Firebase Database stores timestamps in milliseconds since the epoch. An easy way to get such a value is with JavaScript's Date.now() like in this snippet:

console.log(Date.now());

As I write this the value is:

1493869428756

For comparison, your highest value is:

1593840696

So it seems like you're counting seconds since the epoch, instead of milliseconds.

like image 175
Frank van Puffelen Avatar answered Oct 17 '25 03:10

Frank van Puffelen