【发布时间】:2018-05-19 01:53:35
【问题描述】:
我确定我遗漏了一些关于 Firebase 存储规则的内容,但我已经完成了以下操作:
第 1 步
首先我设置了以下 Firebase 存储规则:
service firebase.storage {
match /b/{bucket}/o {
match /items/{dev_key}/{perm_id}/{file_name} {
allow write: if request.auth.uid == dev_id;
allow read: if request.auth.token.permId == perm_id;
}
}
}
我希望只有使用与相关位置匹配的自定义声明 permId 登录的用户才能下载文件 allow read: if request.auth.token.permId == perm_id;。
所以,我然后在用户上设置custom claim in Cloud Functions,如下所示:
第 2 步
admin.auth().setCustomUserClaims(uid, {permId: '1'}).then(() => {
// send off some triggers to let user know the download is coming
admin.database().ref(`collection/${uid}/${itemId}`).update({
downloadReady: true
});
});
然后我将用户注销并重新登录......这设置了自定义声明。 我检查了它们在 Cloud Functions 中的设置如下:
第 3 步
admin.auth().verifyIdToken(idToken).then((claims) => {
console.log("--------------claims -------------");
console.log(JSON.stringify(claims));
});
我在声明字符串中看到了...permID: "1"
然后我在客户端请求了一个 downloadURL(希望这里是我出错的地方)...我希望这不是公共下载 url,而是 Firebase 存储安全规则将检查的下载 url:
第 4 步
var pathReference = storage.ref('items/<some-key>/1/Item-1');
pathReference.getDownloadURL()
.then((url)=>{
console.log("url: ", url);
})
我从这个电话收到的网址给了我这个链接 https://firebasestorage.googleapis.com/v0/b/emiru84-games.appspot.com/o/games%2FcfaoVuEdJqOWDi9oeaLLphXl0E82%2F1%2FGame-1?alt=media&token=45653143-924a-4a7e-b51d-00774d8986a0 (我用来测试的小图)
到目前为止一切顺利,拥有正确声明的用户能够查看此图像
然后我重复了第 2 步,再次注销/登录,但这次的 permId 为“0”。我希望之前生成的 url 不再起作用,因为我的用户不再有正确的自定义声明......并且存储桶位置仍在同一位置(bucket/dev_key/1/filename),但它仍然有效。
如果我重复第 4 步,我会得到一个新的 url,然后给出相应的 403 错误响应。然而,旧的 url 仍然有效(我想只要添加了 token 参数)。这是预期的吗?如果是这样,我不确定如果下载 url 仍然是公开的,我是否理解存储安全规则会产生什么影响?
任何帮助清除我迷糊的大脑将不胜感激。
【问题讨论】:
标签: firebase firebase-storage firebase-security google-cloud-functions