【问题标题】:How to configure the rules of a Firebase project to be safe?如何配置 Firebase 项目的规则以确保安全?
【发布时间】:2020-12-19 14:39:53
【问题描述】:

您好,我有 Firebase 项目,当我创建数据库时,我会创建一些测试规则。 现在,它们过期了,他们关闭了我的项目。 这是我第一次使用 Firebase 项目,我没有经验。我将向您展示我是如何为 Cloud Firestore 和实时数据库定义规则的。 该项目是一个应用程序,用户可以在其中注册和离开他们的 cmets。 我应该如何为我的数据库设置安全规则? 我应该如何编写我的规则代码? 我有几天没有参加我的项目,他们从谷歌给我写信,两天后我的项目就关闭了。我已经查找了信息,但我不知道如何创建规则以便它们正确并且我的项目也可以工作

我编辑我的问题以添加详细信息

在我的应用程序中,我只希望注册用户能够编写 cmets。

Firebase 向我显示的警报如下:

“其安全规则被定义为公开的,因此任何人都可以窃取、修改或删除其数据库中的数据。”

数据库是空的,所以还没有记录。

你能帮帮我吗?如果我写的规则不正确,Firebase 将关闭我的项目,规则不应该公开。

我阅读了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);
    }
  }
}

【问题讨论】:

  • 如果您不提供有关您的数据库的结构(Firestore 还是 Realtime Databaqse BTW?)以及您想要实现哪种访问权限(“应用程序在哪些用户可以注册和离开他们的 cmets”太模糊了)?
  • 谢谢@RenaudTarnec,我会编辑这个问题。我只希望在应用程序中注册的用户写入数据库。数据库仍然是空的
  • 请查看下面提供的解决方案,并让我们知道它们是否适合您,以便进一步了解。
  • 继续写感谢@NibrassH 的支持,目前这对我不起作用
  • 我在下面发布了一个答案,请查看它并让我知道它是否适合您。谢谢。

标签: firebase firebase-realtime-database google-cloud-firestore firebase-security rules


【解决方案1】:

理想情况下,您需要只允许经过身份验证的用户访问资源。从你上面的代码

    {
  "rules": {
    ".read": true,
    ".write": true
  }
}

以上将允许任何人读取和写入数据库,甚至是未经身份验证的用户。

对于 Firestore,您可以看到规则规定,只有在日期未过去 (2020,9,2) 时,它才应允许对云 Firestore 进行完全特权读取和写入

访问链接To learn more about firebase database rules

参观

to learn about firestore rules

您可以为您的用户使用 firebase 身份验证,然后如果他们通过身份验证,他们就可以访问数据库。

【讨论】:

    【解决方案2】:

    根据经验,保护数据的主要方法有两种:

    1. 在文档中设置一个字段,例如“userID”,并且仅当 auth.uid 值与该字段匹配时才允许 CRUD。
    2. 使用 cloud firestore 的集合-文档-集合特性并编写一个规则,允许用户对他们自己的所有集合进行 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;
        }
    }
    

    【讨论】:

    • 感谢您对@CRUD DS 的支持。 ,我应该按照你给我写的规则写吗?
    • @MiguelEspeso 我不能就此提出建议,因为这取决于您的项目要求。请使用这两种方法并进行实验。你可以的!
    【解决方案3】:

    您可以使用以下规则,只有经过身份验证的用户才能写入和读取数据库。

    对于 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”
    }
    }
    

    【讨论】:

    • 谢谢,我已将它们更改为您放置的那些,但您会发现它们与我的相比仅有所不同(^)。如果它有效,我会接受你的回答很好@NibrassH
    • 我有一个问题,这些是 Cloud Firestore 规则还是实时数据库规则?谢谢@NibrasN
    • 不,这些适用于 Cloud Firestore,我已经更新了答案,还添加了实时数据库。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2018-08-15
    • 1970-01-01
    • 1970-01-01
    • 2021-01-12
    • 2019-02-05
    • 2016-10-31
    • 2016-06-27
    • 1970-01-01
    相关资源
    最近更新 更多