【问题标题】:Firebase rules allowing write when they shouldn't beFirebase 规则允许在不应写入时写入
【发布时间】:2018-01-05 05:28:26
【问题描述】:

我有以下数据库条目:“公司:8”:

我有以下数据库规则,不允许将“公司:8”模拟写入数据库。

{
  "rules": {
      ".read": "auth != null",
      ".write": "auth != null",
        "companies": {  
              ".validate": "(data.exists() && (newData.val() === data.val() + 1)) || (!data.exists() && newData.val() == 0)" 
        }
  }
}

但是,当我尝试使用 Firebase Python SDK 将“公司:20”写入数据库时​​,这些规则也不允许这样做:

In [1]: import firebase_admin

In [2]: from firebase_admin import credentials, db

In [3]: cred = credentials.Certificate('serviceAccountCredentials_dev_async.json
   ...: ')

In [4]: firebase_admin.initialize_app(cred, {'databaseURL': 'https://async-testi
   ...: ng.firebaseio.com/'})
Out[4]: <firebase_admin.App at 0x7fc50c00c080>

In [5]: ref = db.reference()

In [6]: ref.update({'companies': 20})

我做错了什么?

【问题讨论】:

    标签: python firebase firebase-realtime-database firebase-security firebase-admin


    【解决方案1】:

    您正在使用 Firebase Admin SDK,并且您看起来像是 initializing with the credentials for a service account。在这种情况下,没有应用安全规则,我认为甚至没有应用验证规则。

    如果出于某种原因您必须使用 Admin SDK 并希望执行验证规则,authenticate with limited priveleges:

    作为最佳实践,服务应该只能访问资源 它需要。为了对资源进行更细粒度的控制 Firebase 应用实例可以访问,使用您的唯一标识符 代表您的服务的安全规则。然后设置合适的 授予您的服务访问所需资源的规则。

    【讨论】:

    • 鲍勃抓得好!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-26
    • 2020-06-09
    • 1970-01-01
    • 2020-08-19
    • 2020-09-26
    相关资源
    最近更新 更多