I have the following database entry: "companies: 8":
我有以下数据库规则,不允许将“companys: 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 将“companies: 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})
我究竟做错了什么?
您正在使用 Firebase Admin SDK,看起来您是使用服务帐户的凭据进行初始化 https://firebase.google.com/docs/database/admin/start#authenticate-with-admin-privileges。在这种情况下,没有应用任何安全规则,我认为甚至没有应用验证规则。
如果出于某种原因您必须使用 Admin SDK 并希望执行验证规则,以有限的权限进行身份验证 https://firebase.google.com/docs/database/admin/start#authenticate-with-limited-privileges:
作为最佳实践,服务应该只能访问资源
它需要。为了对资源进行更细粒度的控制
Firebase 应用程序实例可以访问、使用您的
代表您的服务的安全规则。然后设置合适的
授予您的服务访问其所需资源的规则。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)