首页 > 解决方案 > Firebase Flutter:Cloud Firestore 数据库的规则不安全

问题描述

Firebase 不断告诉我

我们检测到您的安全规则存在以下问题:任何用户都可以读取您的整个数据库

我已经更改了规则,但该规则在我的应用程序中不起作用,因为所有用户都可以从 db 读取,并且只有经过身份验证的用户才能写入 db。

Firebase 说应该在我们登录之前执行写入和读取。但在我的情况下,每个用户都可以读取,只有登录用户可以写入。

任何想法如何解决这个问题?还是我做错了?

rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
  allow read;
  allow write: if request.auth != null;
  }
 }
} 

标签: firebasegoogle-cloud-firestorefirebase-securityrules

解决方案


您可以将 read 明确设置为 false 吗?

rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
  allow read: if false;
  allow write: if request.auth != null;
  }
 }
} 

那应该这样做。让我知道它是否持续存在。根本原因是,即使您只允许经过身份验证的用户读取或写入,但他们可以访问整个数据库,如Google Cloud Firestore 文档中所述。这也意味着任何经过身份验证的用户都可以在您的数据库中写入任何内容。

如果您的数据库对每个用户都有单独的文档,我建议使用以下规则,允许用户仅写入/读取自己的数据。

rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /users/{userId} {
  allow read, write: if request.auth.uid === userId;
  }
 }
} 

推荐阅读