【问题标题】:Firebase storage rules for writing at specific path not considered未考虑在特定路径写入的 Firebase 存储规则
【发布时间】:2020-05-17 11:11:12
【问题描述】:

我有一个非常简单的 Firebase 存储结构:

> check-in-station-content
> reports

这是两个文件夹,我正在尝试对 check-in-station-content 设置限制,以仅接受视频/mp4 文件,不大于 30MB 并且只能由以下人员上传认证的各方。所以,我的规则是这样的:

rules_version = '2';
service firebase.storage {
  match /b/{bucket}/o {
    match /{allPaths=**} {
      allow read: if request.auth != null;
    }

    match /check-in-station-content {
      // Allow write files, subject to the constraints:
      // 1) File is less than 30MB
      // 2) Content type is video/mp4
      match /{videoId} {
        allow write: if request.auth != null && request.resource.size < 30 * 1024 * 1024 && request.resource.contentType.matches('video/mp4')
      }
    }
  }
}

然后,当我转到存储的 UI 并单击 上传 按钮时,没有什么能阻止我上传任何大小的文件

【问题讨论】:

    标签: firebase firebase-storage firebase-security


    【解决方案1】:

    当您使用 Firebase 控制台时,您实际上绕过了所有安全规则,因为您是作为项目所有者(或具有类似访问权限的其他角色,例如编辑者)与 Cloud Firestore 进行交互的。仅当您使用其中一个客户端 SDK 或其他客户端 API 时,这些规则才适用。

    顺便说一句,请注意您的规则匹配 check-in-station-background-content 路径,而不匹配 check-in-station-content 路径。

    【讨论】:

    • 是的,你说得对,我输错了匹配路径。我刚刚编辑了它。
    • @user2128702 Renaud 的回答的另一部分也是正确的:当您在 Firebase 控制台中上传文件(或执行任何其他操作)时,它会绕过安全规则。要在控制台中测试安全规则,您需要使用规则编辑器左下方显示的规则游乐场。
    • @FrankvanPuffelen 感谢您提到规则游乐场的精确/澄清,弗兰克。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-26
    • 2020-07-28
    • 2017-12-01
    • 2017-08-11
    • 2019-05-07
    • 2014-05-03
    • 2019-10-30
    相关资源
    最近更新 更多