简体   繁体   English

Firebase 实时数据库的这些默认安全规则是什么意思?

[英]What do these default security rules for the Firebase Realtime Database mean?

I've created a new project on Firebase, and created a Realtime Database in there.我在 Firebase 上创建了一个新项目,并在其中创建了一个实时数据库。 When asked about the security rules for my database, I selected to Start in test mode .当询问我的数据库的安全规则时,我选择了Start in test mode

Now the security rules of my database in the Firebase console show up as:现在我的数据库在Firebase控制台的安全规则显示为:

{
  "rules": {
    ".read": "now < 1622790000000",  // 2021-6-4
    ".write": "now < 1622790000000",  // 2021-6-4
  }
}

What do these rules mean?这些规则是什么意思? And how can I change them to be more secure?我怎样才能改变他们更安全?


It's been a month since I created my Firebase Realtime Database, and I now got a message:我创建 Firebase 实时数据库已经一个月了,现在我收到一条消息:

Your project's Realtime Database '' will start denying client requests unless you update your security rules除非您更新安全规则,否则您项目的实时数据库“”将开始拒绝客户端请求

These default test mode rules are a simple catch-all that allows everyone in the world to read from and write to your database until a given date.这些默认的测试模式规则是一个简单的包罗万象的规则,它允许世界上的每个人在给定日期之前读取和写入您的数据库。

Let's break the rules down to see exactly how they work:让我们分解规则,看看它们是如何工作的:

  1. The ".read" and ".write" nodes immediately under "rules" determine who can read/write the data in the entire database. "rules"下的".read"".write"节点决定了谁可以读/写整个数据库中的数据。

  2. The now variable is automatically set by Firebase to be the current time on the server. now变量由 Firebase 自动设置为服务器上的当前时间。 This value is in milliseconds since the epoch , which is the recommended value to also store timestamps in Firebase.该值以 epoch 以来的毫秒数为单位,这是在 Firebase 中也存储时间戳的推荐值。

  3. The 1622790000000 value in the rules is the timestamp of some point in the future.规则中的1622790000000值是未来某个时间点的时间戳。 Let's see what this value is in a more readable date format:让我们以更易读的日期格式看看这个值是什么:

     console.log(new Date(1622790000000))

    "2021-06-04T07:00:00.000Z" “2021-06-04T07:00:00.000Z”

So anyone can read of write all data in our database until June 4th, 2021. After that date nobody can access the data anymore with the client-side SDKs.因此,任何人都可以在 2021 年 6 月 4 日之前读取或写入我们数据库中的所有数据。在那之后,没有人可以再使用客户端 SDK 访问数据。 The Firebase Admin SDKs bypass these rules altogether, so they are not affected. Firebase Admin SDK完全绕过这些规则,因此不受影响。


Can I extend the time period?我可以延长时间吗?

You may have gotten a message like this from Firebase:你可能从 Firebase 收到这样的消息:

You chose to start developing in Test Mode, which leaves your Realtime Database instance completely open to the Internet.您选择在测试模式下开始开发,这会使您的实时数据库实例完全对 Internet 开放。 Because this choice makes your app vulnerable to attackers, your database security rules were configured to stop allowing requests after the first 30 days.由于此选择使您的应用容易受到攻击者的攻击,因此您的数据库安全规则配置为在前 30 天后停止允许请求。 In 5 day(s), all client requests to your Realtime Database instance will be denied. 5 天后,所有客户端对您的实时数据库实例的请求都将被拒绝。

This message means that access to your data is about to expire, due to timestamp that is in your security rules.此消息意味着由于您的安全规则中的时间戳,对您的数据的访问即将到期。

It's actually pretty easy to extend the test mode to another deadline.将测试模式延长到另一个截止日期实际上很容易。 All you need to do is change that 1622790000000 value.您需要做的就是更改1622790000000值。 For example, for extend it to July 4th, I can set the value to 1625382000000 .例如,要将其延长到 7 月 4 日,我可以将值设置为1625382000000

To determine the value to use, I run this tiny JavaScript snippet:为了确定要使用的值,我运行了这个微小的 JavaScript 片段:

 console.log(new Date("2021-07-04T07:00:00.000Z").getTime())

Here's another tool to calculate these values.这是计算这些值的另一个工具

By using 1625382000000 we've extended test mode for a month and everyone can read/write the entire database until July 4, 2021.通过使用1625382000000 ,我们将测试模式延长了一个月,每个人都可以读取/写入整个数据库,直到 2021 年 7 月 4 日。


How can I better protect the data?如何更好地保护数据?

At some point you should come up with a better way to protect your (user's) data than just opening it until a specific date.在某些时候,您应该想出一种更好的方法来保护您(用户的)数据,而不是仅仅在特定日期之前打开它。 I typically do this right when I start a project, but it's also fine if you start it a bit later.当我开始一个项目时,我通常会这样做,但如果你稍后再开始它也可以。

The important thing is that you should treat the server-side security rules the same as the client-side source code of your app .重要的是您应该将服务器端安全规则与您的应用程序的客户端源代码一样对待

I develop my code and rules in tandem.我同时开发我的代码和规则。 So:所以:

  1. I start with a fully closed off database, since there is no code yet that needs access to any data.我从一个完全封闭的数据库开始,因为还没有代码需要访问任何数据。

  2. I add some data manually to the database, and write code to read it.我手动将一些数据添加到数据库中,并编写代码来读取它。 At this point, I write security rules that only allow read-access to that specific data.此时,我编写了只允许读取特定数据的安全规则。 So it may be ".read": true , but it'll be much deeper in my JSON structure.所以它可能是".read": true ,但在我的 JSON 结构中会更深。 Even such simple rules will already block many bad actors.即使是这样简单的规则也已经阻止了许多不良行为者。

  3. The first time I want the app to write to the database is also when I add authentication.我第一次希望应用程序写入数据库也是在我添加身份验证时。 Typically I start with anonymous auth, since it does not require me to enter any credentials.通常我从匿名身份验证开始,因为它不需要我输入任何凭据。

    I then include the hard-coded UID in my security rules, to ensure only I can write data.然后我将硬编码的 UID 包含在我的安全规则中,以确保只有我可以写入数据。 You'll often still find this top-level ".write": "auth.uid === 'hardcodedUidOfPufsAnonymousUser'" in my rules much later, after I added proper data ownership.在我添加了适当的数据所有权之后,你经常会在我的规则中找到这个顶级的".write": "auth.uid === 'hardcodedUidOfPufsAnonymousUser'"

    When using Firestore I sometimes evolve that as explained here: User conflict when using same Auth method for Admin and Normal users |使用 Firestore 时,我有时会按照此处的说明进行改进: 对管理员和普通用户使用相同的身份验证方法时的用户冲突 | Firebase Auth Firebase 认证

  4. At any point when I add (typically lists of) data, I think through who "owns" this data, and who can read it.在我添加(通常是列表)数据的任何时候,我都会考虑谁“拥有”这些数据,以及谁可以读取它。 I then expand my rules to allow exactly that access, and nothing more.然后我扩展我的规则以允许该访问,仅此而已。

This need to update my security rules as I write code slows down the pace at which I code, but I'll gladly do it anyway.这需要在我编写代码时更新我的安全规则,这会减慢我编写代码的速度,但我还是很乐意这样做。 Keeping the data in my database secure at every step, allows me to give people access to the app/database with confidence.在每一步都确保数据库中的数据安全,让我可以让人们充满信心地访问应用程序/数据库。 I recommend you do the same.我建议你也这样做。

For more information, I recommend reading:有关更多信息,我建议阅读:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Firebase 实时数据库规则 - Firebase realtime database rules Firebase 实时数据库:Google 警告写入规则 - Firebase Realtime Database: Google warning for write rules Firebase 实时数据库规则被拒绝权限 - Firebase Realtime Database Rules Denied Permission Firebase 存储安全规则是否支持版本 2? - Do Firebase Storage security rules support version 2? 我的 Firebase 实时数据库也没有在控制台上加载任何数据以获取规则 - My realtime database of Firebase is not loading any data on the console also for rules 做 Firebase 实时数据库使用缓存 - Do Firebase Realtime Database uses cache 如何为 Firebase 实时数据库读取证券增加额外的安全性? - How to add extra security to Firebase Realtime Database read securities? Firebase Auth ID 令牌验证的必要性,即使有数据库的安全规则? - Necessity of Firebase Auth ID token validation, even if there are security rules of database? Childs 的 firebase 规则在多大程度上被数据库中为“parents”设置的规则覆盖? - To what extent are the firebase rules of Childs overridden by rules set for 'parents' in the database? 如何在 firebase 实时数据库的更新上正确地做一些事情? - How to properly do something onUpdate of firebase realtime database?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM