【问题标题】:Firebase storage metadata not included in security rulesFirebase 存储元数据未包含在安全规则中
【发布时间】:2020-01-28 03:58:56
【问题描述】:

我正在尝试实施 Firebase 存储规则,以便只有帖子作者可以创建、删除和更新它。问题是在模拟器中它工作得很好,但在实际情况下却没有。对我来说,我使用的元数据似乎没有出现在规则中或未包含在 put 请求中。

我按照文档中的说明做所有事情:

这些是我的规则:

service firebase.storage {
  match /b/{bucket}/o {
  //Posts
    match /{allImages=**} {
       allow read: if authenticated();
       allow create: if authenticated() && metadata().userId == request.auth.uid;
       allow update, delete: if authenticated() && metadata().userId == request.auth.uid;
    }
    }
  function metadata() { return request.resource.metadata; }
  function authenticated() { return request.auth.uid != null; }
}

这是我用来上传文件的功能:

uploadFile({ commit, rootState }, payload) {
    const storageRef = firebase
      .storage()
      .ref(`posts/${payload.id}/${payload.index}/${payload.index}`)
    commit('app/setError', null, { root: true })
    commit('app/setLoading', true, { root: true })
    return new Promise((resolve, reject) => {
      storageRef
        .put(payload.file, {
          userId: rootState.authentication.user.id
        }) //HERE IS THE METADATA AS THE SECOND PARAMETER (userId)
        .then(snapshot => {
          snapshot.ref.getDownloadURL().then(downloadURL => {
            resolve(downloadURL)
            commit('app/setLoading', false, { root: true })
          })
        })
        .catch(error => {
          reject(error)
          commit('app/setError', error, { root: true })
          commit('app/setLoading', false, { root: true })
        })
    })
  },

编辑 1:这是我在模拟器中测试的对象元数据:

{"metadata":{"userId":"9PuxRiKI17Y8hbwW9aVISpdrpZa2"},"name":"b/myprojectid-develop.appspot.com/o/posts/{postId}","bucket":"myprojectid-develop.appspot.com"}

这是我在真实案例中作为元数据发布的缩小对象:

{"userId":"9PuxRiKI17Y8hbwW9aVISpdrpZa2"}

我做错了什么?任何帮助表示赞赏!

【问题讨论】:

  • 您是否确定您为 userId 传递的值正是您所期望的?我们在这里看不到实际值是什么——它隐藏在对象属性后面。
  • @DougStevenson Jep 我 100% 确信这些值是我所期望的。我还想提一下,我在真实案例中使用与模拟器测试相同的 UID 进行了测试。为什么我必须说我确信我在对象中的值是在实时数据库上我也使用安全规则并且这些规则运行良好。如果需要,我可以使用我发布的带有其值的最小对象来更新问题。
  • 看起来应该可以。如果您有特定的重现步骤,请向 Firebase 支持提交错误报告,看看他们是否可以重现。 support.google.com/firebase/contact/support
  • @DougStevenson 谢谢,我确实在您提供的链接下提交了错误报告。

标签: javascript firebase firebase-storage firebase-security


【解决方案1】:

我确实联系了 firebase 支持,他们提供的解决方案是将您的元数据对象包装在 customMetadata 中,如下所示:

storageRef
.put(payload.file, {
   customMetadata: { //This is the required key
      userId: <your user id data>
   }
})

注意!这似乎记录在here

【讨论】:

  • 看起来它记录在这里:firebase.google.com/docs/reference/js/…
  • 哇,我完全错过了,它在文档中非常深入。我什至确实读过这个页面。
  • 自己错过了!很高兴你得到了答案。
猜你喜欢
  • 2021-01-03
  • 1970-01-01
  • 2016-01-17
  • 2016-09-27
  • 2018-12-19
  • 2020-07-03
  • 1970-01-01
  • 2020-03-24
相关资源
最近更新 更多