firebase - 使用 Firestore 安全地增加投票

标签 firebase google-cloud-firestore firebase-security

我阅读了很多有关此问题的帖子,但没有找到适当建议的官方方法来确保计票。

我们如何确保安全;

  1. FieldValue.increment(1)以便恶意用户无法增加 5,6 等?
  2. 如果我们检查 userID(例如:postID/likes/userID),检查它是否不存在的确定性如何?
  3. 您能否展示创建基本用户点赞/投票安全结构示例的建议正确方法?

云功能建议;仅出于任何形式的点赞或投票而运行云功能真的实用吗?

谢谢,

最佳答案

FieldValue.increment(1) so that malicious users cannot increment by 5,6 etc?

您可以比较 security rules 中的当前值和新值如下图:

allow update: if request.resource.data.fieldName == resource.data.fieldName + 1;

根据文档,request.resource变量包含文档的 future 状态,而 resource.data是当前值。

但是,这不会阻止用户使用increment(1)多次。您必须将用户的 UID 存储在某处以进行检查。

If we check for userID's (for ex; postID/likes/userIDs) How certain it is to check if it does not exist?

您可以使用 exists() 函数来检查用户是否已经投票,但您必须确保首先添加文档。用户可以增加计数但阻止 create运算符(operator)添加他们的投票记录,允许他们投多票。

一种解决方法是仅添加投票文档,而不是增加计数,但随后您必须稍后阅读所有文档才能获取投票总数,或者使用 Cloud Functions 的 Firestore 触发器来保持计数更新背景。


Could you please show the suggested, right way of creating a basic user likes/vote security structure example?

使用Cloud Functions时,可以确保计数会增加并添加用户的投票记录,并且没有人可以对代码进行逆向工程。

关于firebase - 使用 Firestore 安全地增加投票,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73887255/

相关文章:

javascript - Firebase 理解 snapshot.child()

javascript - 在 Firebase 中,我收到未捕获的类型错误 : Cannot read property 'initializeApp' of undefined,,但不确定为什么未定义 'firebase'?

android - 火力地堡错误 : Can´t add ValueEventListener

Angular 通用 Firebase : no provider for InjectionToken MODULE_MAP

firebase - 在 Firebase 云函数中获取空值 onDelete()

javascript - 具有身份验证的 Cloud Firestore

objective-c - Firebase,无法查询数组

javascript - 适用于 Web 应用的 Cloud Firestore

firebase - 在 Firebase 上获取最近创建的用户的 UID

angular - 如何处理 google firebase 网络应用程序安全性?