【发布时间】:2017-08-01 21:49:03
【问题描述】:
Cloud Function 在更新或删除时收到 Firebase 权限被拒绝错误。
使用文件中的凭据初始化服务帐户,以便使用默认帐户当前不可用的auth.createCustomToken
admin = require('firebase-admin');
functions = require('firebase-functions');
credential = admin.credential.cert(require('./credentials.json'));
admin.initializeApp({credential: credential, databaseURL: functions.config().firebase.databaseURL});
阻止更新的安全规则:
"downloads": {
"$key": {
".write": "data.val() == null"
}
}
用户将带有push 的数据插入/downloads,然后Cloud Function 尝试更新并随后删除。即使管理员帐户应该绕过包括验证在内的所有安全规则,这两个操作都会失败。
FIREBASE WARNING: update at /downloads/-Kexz33ljYjKblF_ZgUo failed: permission_denied
FIREBASE WARNING: set at /downloads/-Kexz33ljYjKblF_ZgUo failed: permission_denied
如果我将规则更改为这样,第一个错误(更新)就会消失:
"downloads": {
"$key": {
".write": "newData.child('uid').val() == auth.uid"
}
}
更新
解决方法是将event.data.ref重新定义为
ref = admin.database().ref(event.data.ref.path.toString())
【问题讨论】:
-
您能否编辑您的问题以包含触发
permission_denied的代码?
标签: javascript firebase firebase-security firebase-admin google-cloud-functions