无需身份验证的 Firestore 安全规则

时间:2021-04-30 04:46:52

标签: firebase google-cloud-firestore firebase-storage firebase-security

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

我目前的规则:

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

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

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

1 个答案:

答案 0 :(得分:0)

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;
    }
  }
}