Firestore Security Rules: If timestamp (FieldValue.serverTimestamp) equals now

那年仲夏 提交于 2020-01-01 04:50:06

问题


How do I check if user on client sided created document with only firebase.firestore.FieldValue.serverTimestamp()?

I have following:

allow create: if request.resource.data.timestamp == ??

What should I have instead of ??. I have tried serverTimestamp() firebase.firestore.FieldValue.serverTimestamp(), now or now() but it doesn't work.

It is possible to do it in Firebase like this:

".validate": "newData.child('timestamp').val() === now"

I am looking for the same solution. Any ideas? Thanks


回答1:


You can access the current request timestamp in Security Rules using the request.time attribute (docs), which is the Firestore equivalent to the Realtime Databases's now. You'll therefore want something like:

allow create: if request.resource.data.timestamp == request.time;

For serverTimestamp() this should evaluate to true.

You should always validate client input in Security Rules, even if you're using serverTimestamp(). Security Rules doesn't automatically know the server input the value instead of the client, so without this check, a malicious client could create a different created at time.



来源:https://stackoverflow.com/questions/48783812/firestore-security-rules-if-timestamp-fieldvalue-servertimestamp-equals-now

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!