javascript - 一些 Firebase 安全规则适用于 Cloud Functions 中的管理员

标签 javascript firebase firebase-security firebase-admin google-cloud-functions

Cloud Functions 在更新或删除时收到 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())

最佳答案

event.data 内对象返回到您的 Cloud Function 的 onWrite() 回调,有两种类型的数据库引用:一种与触发写入的用户具有相同的权限(event.data.ref),另一种是具有管理员权限,授予完全读写访问权限(event.data.adminRef)。我无法判断,因为您没有提供显示您的 Cloud Function 的代码示例,但我敢打赌您使用的是 event.data.ref。切换到使用 event.data.adminRef 应该可以解决您的问题。

关于javascript - 一些 Firebase 安全规则适用于 Cloud Functions 中的管理员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42737203/

相关文章:

仅当应用程序处于前台时才播放 Android 推送通知声音,但当应用程序处于后台时不播放声音

android - 上传图像,Firebase 显示 "User does not have permission to access this object",即使存储设置是公开的

google-cloud-firestore - 比较 Firestore 规则中的文档时间戳

javascript - 全面的 RegExp 以删除 JavaScript 注释

javascript - 不调用 jQuery 动态加载图像

java - 按用户名在 FirebaseDatabase 中搜索用户

android - 移动设备上的 Firestore : rules are not enforced on cached data

javascript - 如何获取点击的子节点的索引

javascript - $.post Jquery发送数组

android - Firebase Android Studio (2.2.3) 问题