如何防止恶意用户将数据推送到 Firebase 中的文档中?

How to prevent a malicious user from pushing data into a document in Firebase?

此时我正在使用前端 Firebase 创建一个未与节点集成的文档,我试图解决防止恶意用户在我从前端创建的文档中发布的问题-最后,我的意思是我的应用程序中的前端可以创建数据并将它们推送到 Firestore,但我担心的是用户会故意将数据推送到集合中,甚至导致不必要的文档写入一个云功能,它将检查应用程序的模型是否处于特定形状(我的默认形状,因为你不能像 mongo 中那样制作模型),如果某人的模型不同,则帐户和整个文档将被删除用户将被视为恶意。我也想知道如果超过文档大小会怎样?超过大小会阻止数据库向该文档添加数据,或者在这种情况下可接受的行为是什么?

  const AddGroup = async (//some data) => {
    try {
      const docRefCol = doc(db, //some collection, //some document in side colletion);
      // Add
      await updateDoc(docRefCol, {
        //Some melicious data or data that the user decied on pushing by abusing the front end logic
      });
    } catch (error) {
      errorHandeling(error, 'An error has happened');
    }
  };

看看 Firebase 身份验证并实现它。

Link - https://firebase.google.com/products/auth 文档 - https://firebase.google.com/docs/auth

或者在您的 Node 后端实施自定义身份验证系统

您无法阻止任何人向 Firestore 发出更新请求,但如果您设置了安全规则,那应该不是问题。如果您指的是文档的结构,那么您可以使用 hasOnly() 来防止用户在您的文档中创建任何新字段。

allow write: if request.resource.data.keys().hasOnly(['name', 'age'])

例如,上面的规则将只允许更新文档中的姓名和年龄字段。

在字段是数组的情况下,您将需要一些其他逻辑,例如最大数组大小或某种速率限制,以防止垃圾邮件数据被推送到数组中。

what will happen if the document size is exceeded?

不会更新,SDK 会报错。