【问题标题】:Firebase storage rules: authenticate the allowed userFirebase 存储规则:对允许的用户进行身份验证
【发布时间】:2021-09-11 07:37:51
【问题描述】:

我有两个用户之间的通信渠道,我使用 Firebase 存储在这些用户之间传输文件。 我不希望第三个用户访问频道的文件。

我正在尝试使用“authfile”和其中的元数据来识别授权用户。 该身份验证文件是使用 admin sdk 上传的。

只有特定用户才能下载和上传文件到存储中 authfiles 元数据包含其 uid 的特定路径。仍然不允许用户触摸 authfile。

authfile 中的元数据如下所示:

metadata: {
  AvnYaUFdaJh1j0FMIiKWoIC2MDw1: true, // firebase userId 1
  YhFyYRbutlf1PFI4NLKTN4qWRhQ2: true, // firebase userId 2
}

my storage

我尝试过的规则:

rules_version = '2';
service firebase.storage {
match /b/{bucket}/o {

  // Channel Media    
  match /media/{channelId}/{allPaths=**} {
    
    function isAllowedUser(uid) {
      return /authfile.resource.metadata.users[uid] == true;
    }

    allow delete: if isAllowedUser(request.auth.uid)
      && request.resource.contentType != 'authfile'

    allow read: if isAllowedUser(request.auth.uid)
      && request.resource.contentType != 'authfile'
    
    allow write: if isAllowedUser(request.auth.uid)
      && request.resource.contentType != 'authfile'
      && request.resource.size < 16 * 1024 * 1024;
      
    }
  }
}

我是第一次编写 Firebase 存储规则,希望得到有关如何实施的建议。

【问题讨论】:

    标签: firebase firebase-storage firebase-security


    【解决方案1】:

    我认为您在这里不需要“用户”。

    function isAllowedUser(uid) {
        return /authfile.resource.metadata.users[uid] == true;
    }
    

    这应该是有效的 resource.metadata[uid] == true;

    此外,如果您需要检查其他文件是否存在或其元数据,请考虑使用 answer 中提到的云功能。

    【讨论】:

    • 感谢您的回复。我正试图让这个应用程序以便宜的价格运行,因此更少的函数请求会更好。也许我只是在用户发送的文件的元数据中添加了一些频道秘密,我在创建频道时与对话各方共享。我认为这可能足够安全。但是,发送到存储的文件将进行 e2e 加密。
    猜你喜欢
    • 2018-10-25
    • 2019-11-18
    • 2020-06-11
    • 1970-01-01
    • 2019-03-31
    • 2020-12-16
    • 2021-11-29
    • 2020-03-06
    • 1970-01-01
    相关资源
    最近更新 更多