如何配置 Firebase 项目的规则才安全?
How to configure the rules of a Firebase project to be safe?
您好,我有 Firebase 项目,当我创建数据库时,我创建了一些测试规则。
现在,他们到期了,他们关闭了我的项目。
这是我第一次使用 Firebase 项目,我没有任何经验。我将向您展示我是如何为 Cloud Firestore 和实时数据库定义规则的。
该项目是一个应用程序,用户可以在其中注册并发表评论。
我应该如何为我的数据库设置安全规则?
我应该如何编写我的规则代码?
我有几天没有参加我的项目,他们从 Google 写信给我,两天后我的项目就结束了。我已经查找过信息,但我不知道如何创建规则才能使它们正确并且我的项目也能正常工作
我编辑我的问题以添加详细信息
在我的应用程序中,我只希望注册用户能够发表评论。
Firebase 向我显示的警报如下:
“其安全规则定义为 public,因此任何人都可以从其数据库中窃取、修改或删除数据。”
数据库是空的,所以还没有记录。
你能帮帮我吗?如果我没有正确编写规则,Firebase 将关闭我的项目,规则不应该是 public.
我阅读了 documentation that Firebase offers,但我并不真正了解如何创建我的规则。
对于经过身份验证的用户,他们会显示类似这样的内容:
// Allow read/write access on all documents to any user signed in to the application
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if request.auth != null;
}
}
}
另一方面,他们展示了这些规则:
**// Allow read/write access to all users under any conditions
// Warning: **NEVER** use this rule set in production; it allows
// anyone to overwrite your entire database.
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if true;
}
}
}**
我不知道我到底应该使用哪一个,以及我应该如何编写它们,以便在我的 React Native 应用程序中用户可以留下反馈。
你能帮帮我吗?
我展示我的数据库规则的代码
//REALTIME DATABASE
{
"rules": {
".read": true,
".write": true
}
}
//CLOUD FIRESTORE
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
// This rule allows anyone with your database reference to view, edit,
// and delete all data in your Firestore database. It is useful for getting
// started, but it is configured to expire after 30 days because it
// leaves your app open to attackers. At that time, all client
// requests to your Firestore database will be denied.
//
// Make sure to write security rules for your app before that time, or else
// all client requests to your Firestore database will be denied until you Update
// your rules
match /{document=**} {
allow read, write: if request.time < timestamp.date(2020, 9, 2);
}
}
}
理想情况下,您需要只允许经过身份验证的用户访问资源。来自你上面的代码
{
"rules": {
".read": true,
".write": true
}
}
以上内容将允许任何人读取和写入数据库,甚至是未经身份验证的用户。
如您所见,对于 firestore,规则规定只有在日期未过 (2020,9,2) 的情况下,它才允许对云 firestore 进行完全权限读写
访问 link To learn more about firebase database rules
并访问
to learn about firestore rules
您可以为您的用户使用 firebase 身份验证,如果他们通过身份验证,他们就可以访问数据库。
根据经验,保护数据安全的主要方式有两种:
- 在文档中设置一个字段,例如“userID”,并且仅当 auth.uid 值与该字段匹配时才允许 CRUD。
- 使用云 Firestore 的 collection-document-collection 特性并编写一个规则,允许用户对他们自己的所有集合进行 CRUD。例如
match /users/{userID}{
allow read: if request.auth.uid ==userID;
allow write: if request.auth.uid == userID;
match /userDocs/{docID}{
allow read: if request.auth.uid == userID;
allow write: if request.auth.uid == userID;
}
}
您可以使用以下规则,只有经过身份验证的用户才能写入和读取数据库。
对于 Cloud Firestore:
// Allow read/write access on all documents to any user signed in to the application
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if request.auth != null;
}
}
}
对于实时数据库:
// Only authenticated users can access/write data
{
“rules”: {
“.read”: “auth != null”,
“.write”: “auth != null”
}
}
您好,我有 Firebase 项目,当我创建数据库时,我创建了一些测试规则。 现在,他们到期了,他们关闭了我的项目。 这是我第一次使用 Firebase 项目,我没有任何经验。我将向您展示我是如何为 Cloud Firestore 和实时数据库定义规则的。 该项目是一个应用程序,用户可以在其中注册并发表评论。 我应该如何为我的数据库设置安全规则? 我应该如何编写我的规则代码? 我有几天没有参加我的项目,他们从 Google 写信给我,两天后我的项目就结束了。我已经查找过信息,但我不知道如何创建规则才能使它们正确并且我的项目也能正常工作
我编辑我的问题以添加详细信息
在我的应用程序中,我只希望注册用户能够发表评论。
Firebase 向我显示的警报如下:
“其安全规则定义为 public,因此任何人都可以从其数据库中窃取、修改或删除数据。”
数据库是空的,所以还没有记录。
你能帮帮我吗?如果我没有正确编写规则,Firebase 将关闭我的项目,规则不应该是 public.
我阅读了 documentation that Firebase offers,但我并不真正了解如何创建我的规则。
对于经过身份验证的用户,他们会显示类似这样的内容:
// Allow read/write access on all documents to any user signed in to the application
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if request.auth != null;
}
}
}
另一方面,他们展示了这些规则:
**// Allow read/write access to all users under any conditions
// Warning: **NEVER** use this rule set in production; it allows
// anyone to overwrite your entire database.
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if true;
}
}
}**
我不知道我到底应该使用哪一个,以及我应该如何编写它们,以便在我的 React Native 应用程序中用户可以留下反馈。
你能帮帮我吗?
我展示我的数据库规则的代码
//REALTIME DATABASE
{
"rules": {
".read": true,
".write": true
}
}
//CLOUD FIRESTORE
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
// This rule allows anyone with your database reference to view, edit,
// and delete all data in your Firestore database. It is useful for getting
// started, but it is configured to expire after 30 days because it
// leaves your app open to attackers. At that time, all client
// requests to your Firestore database will be denied.
//
// Make sure to write security rules for your app before that time, or else
// all client requests to your Firestore database will be denied until you Update
// your rules
match /{document=**} {
allow read, write: if request.time < timestamp.date(2020, 9, 2);
}
}
}
理想情况下,您需要只允许经过身份验证的用户访问资源。来自你上面的代码
{
"rules": {
".read": true,
".write": true
}
}
以上内容将允许任何人读取和写入数据库,甚至是未经身份验证的用户。
如您所见,对于 firestore,规则规定只有在日期未过 (2020,9,2) 的情况下,它才允许对云 firestore 进行完全权限读写
访问 link To learn more about firebase database rules
并访问
to learn about firestore rules
您可以为您的用户使用 firebase 身份验证,如果他们通过身份验证,他们就可以访问数据库。
根据经验,保护数据安全的主要方式有两种:
- 在文档中设置一个字段,例如“userID”,并且仅当 auth.uid 值与该字段匹配时才允许 CRUD。
- 使用云 Firestore 的 collection-document-collection 特性并编写一个规则,允许用户对他们自己的所有集合进行 CRUD。例如
match /users/{userID}{
allow read: if request.auth.uid ==userID;
allow write: if request.auth.uid == userID;
match /userDocs/{docID}{
allow read: if request.auth.uid == userID;
allow write: if request.auth.uid == userID;
}
}
您可以使用以下规则,只有经过身份验证的用户才能写入和读取数据库。
对于 Cloud Firestore:
// Allow read/write access on all documents to any user signed in to the application
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if request.auth != null;
}
}
}
对于实时数据库:
// Only authenticated users can access/write data
{
“rules”: {
“.read”: “auth != null”,
“.write”: “auth != null”
}
}