无法获取Firebase存储安全规则以拒绝访问文件

时间:2017-12-05 11:53:29

标签: firebase firebase-storage firebase-security google-cloud-functions

我确定我错过了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(希望这里我出错了)...我希望这不是公共下载网址,而是Firebase存储安全规则将检查的下载网址:

第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不再有效,因为我的用户不再拥有正确的自定义声明...并且存储桶位置仍在同一位置(存储桶/ dev_key / 1 / filename)但它仍然有效。

如果我重复步骤4,我会得到一个新的url,然后给出相应的403错误响应。然而旧的URL仍然有效(我想只要加上令牌参数)。这是预期的,如果是这样,我不确定我是否理解如果下载网址是公共的,存储安全规则会如何产生影响?

任何有助于清除我迷雾大脑的帮助都会受到赞赏。

1 个答案:

答案 0 :(得分:3)

Cloud Storage for Firebase中的下载URL始终可公开读取。它不受安全规则的影响。

如果您不想允许公开访问文件,可以撤消其下载URL。