电子邮件:[Firebase] 对 Cloud Firestore 数据库的客户端访问权限将在 X 天后到期

Dou*_*son 19 firebase firebase-security google-cloud-firestore

我收到一封电子邮件,表明我正在“测试模式”下进行开发,但它使我的数据库完全对互联网开放。我最初接受的默认规则如下所示:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {

    // This rule allows anyone on the internet 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
    // your app will lose access to your Firestore database
    match /{document=**} {
      allow read, write: if request.time < timestamp.date(2019, 12, 14);
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

需要做什么才能满足这封电子邮件的要求?

Dou*_*son 20

此处显示的安全规则与之前更为宽松的默认规则不同。这条规则的想法:

match /{document=**} {
  allow read, write: if request.time < timestamp.date(2019, 12, 14);
}
Run Code Online (Sandbox Code Playgroud)

是指您可以在指定日期之前不受限制地访问 Firestore 数据库,以便在一个月内自由地试验它。然而,从长远来看,允许不受限制的访问显然是一个巨大的安全漏洞。

推荐的做法是首先完全删除此规则,因为它允许任何人在您的数据库中读取和写入任何内容。然后,设计一些适当的规则,只允许访问最终用户应该能够访问的集合和文档。Stack Overflow 对此的完整讨论是题外话(因为我们不知道您的应用程序的要求),但这里有一些不错的地方可以开始学习安全规则:

您应该做的是调用数据库中每个集合和子集合的访问约束。理想情况下,您应该锁定对所有集合的未经身份验证的写访问,除非绝对需要。在最好的情况下,您使用 Firebase 身份验证来帮助控制仅在经过身份验证的用户需要时对文档的访问。

或者,如果您已经完成了数据库的工作(暂时),您可以通过专门使用以下规则来完全阻止从 Web 和移动客户端访问数据库:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    allow read, write: if false;
  }
}
Run Code Online (Sandbox Code Playgroud)

根据此规则,仍将允许使用 Firebase Admin SDK 或其他 Cloud SDK 从后端代码进行访问。


ziz*_*utg 13

或者如果你和我一样,谁还在测试模式?只更新日期

match /{document=**} {  
   // from previous date 2019, 12, 14 to new date 2020, 01, 4
   allow read, write: if request.time < timestamp.date(2020, 01, 4); 
}
Run Code Online (Sandbox Code Playgroud)


Pra*_*rni 6

每当你在 firebase 上启动一个新项目(或)设置一个 firestore 数据库时,firebase 默认为你的数据库添加一组规则,看起来像这样。

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {

    // This rule allows anyone on the internet 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
    // your app will lose access to your Firestore database
    match /{document=**} {
      allow read, write: if request.time < timestamp.date(XXXX, XX, XX);
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

“timestamp.date”的日期为您启动项目后的 1 个月。或多或少像 30 天免费试用。绕过此日期后,数据库将拒绝所有客户端请求。因此,电子邮件基本上是提醒您更改安全规则。一种简单的方法是仅允许经过身份验证的用户进行读/写请求。

// 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;
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

请注意,这是定义规则的一种方法,不需要完全如图所示,您可以根据您的要求进一步进行修改。有关更多信息,您可以查看此文档


小智 5

创建 firestore 数据库后,您将获得 30 天的访问权限。你的规则看起来像这样。查看日期,允许在一定时间内读/写,

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if
          request.time < timestamp.date(2021, 8, 17);
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

日期部分在这里很重要。如果您想在测试模式下使用更长时间,您可以增加此日期。

request.time < timestamp.date(2021, 10, 30);
Run Code Online (Sandbox Code Playgroud)

或者,最好在开发应用程序时设置对任何经过身份验证的用户的访问权限,例如

service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if request.auth != null;
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

总是更具体是有好处的,尤其是在生产中部署时。在这种情况下,你的规则可以是,

 match /some_collection/{userId}/{documents=**} {
      allow read, write: if request.auth != null && request.auth.uid == userId
    }
Run Code Online (Sandbox Code Playgroud)

您可以在官方文档中阅读更多详细信息 - https://firebase.google.com/docs/rules/basics