【问题标题】:Some Firebase security rules apply for admin in Cloud Functions一些 Firebase 安全规则适用于 Cloud Functions 中的管理员
【发布时间】: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


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2023-03-24
    • 1970-01-01
    • 2021-05-14
    • 1970-01-01
    • 1970-01-01
    • 2019-04-16
    • 2018-11-27
    • 2021-10-06
    • 1970-01-01
    相关资源
    最近更新 更多