首页 > 解决方案 > 没有身份验证的 Firestore 安全规则

问题描述

我使用 Firestore 和 firebase 存储存储了我的投资组合应用程序数据。我的应用程序中没有用户输入或注册,这是一个简单的作品集来展示我的作品。我希望任何用户都能够读取来自我的 firestore 和 firebase 存储的数据。

我目前的规则:

service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read: if true;
    }
  }
}

我对这个规则的问题是攻击者可以用请求来流动我的应用程序。我什至收到警告邮件说“因为你的项目没有强大的安全规则,任何人都可以访问你的整个数据库。攻击者可以读取你的所有数据,他们可以提高你的账单。”

我没有存储任何敏感数据,但我想防止谷歌收取额外费用。如何设置我的 Firestore 安全规则以使任何用户无需身份验证即可阅读但防止攻击?

标签: firebasegoogle-cloud-firestorefirebase-storagefirebase-security

解决方案


Firebase 安全规则不喜欢数据库点保持打开状态,而这通常通过安全规则和身份验证来完成,您可以根据文档内部的值定义文档的可读性。

在此示例中,文档的布尔值为Public

service cloud.firestore {
  match /databases/{database}/documents {
    // Allow the user to read data if the document has the 'visibility'
    // field set to 'public'
    match /{document=**} {
      allow read: if resource.data.public == true;
    }
  }
}

推荐阅读