【问题标题】:firebase security rule to allow admin users to read/write all other users creates a security breach?允许管理员用户读/写所有其他用户的firebase安全规则会造成安全漏洞?
【发布时间】:2020-05-27 11:11:34
【问题描述】:

我正在使用云 Firestore 数据库,我的根数据库上有“用户”集合,其中包含所有用户文档(命名为 uid),其中包含我收集的数据。 我想让用户只能读/写他们自己的集合,所以我开始我的安全规则

service cloud.firestore {
  match /databases/{database}/documents {
    // Allow only authenticated content owners access
    match /users/{userId}/{documents=**} {
      allow read, write: if request.auth.uid == userId
    }
  }
}

然后我意识到用户没有创建自己的主文档的权限(它将其创建为“幽灵”文档)。我还想给它添加数据,例如显示名称和电子邮件,所以我添加了

// Allow users to read/write their own main user object
match /users/{userId} {
  allow read, write: if request.auth.uid == userId
}

一切正常,但现在我想允许管理员用户读取/写入所有用户的集合和文档。 firebase's documentations suggests 向用户的文档添加一个键值对,例如 (admin:true),所以像这样编辑我的安全规则应该可以解决问题:

match /users/{userId}/{documents=**} {
  allow read, write: if request.auth.uid == userId
  allow read, write: if get(/users/$(request.auth.uid)).data.admin == true; //-security breach?
}

但这不会造成安全漏洞,用户可以在技术上使用(admin:true) 更新他自己的文档并获得管理员权限吗? 如果是这样,我猜我的方法应该是创建一个单独的 admins 集合并在那里添加我的管理员,然后在

的行中做一些事情
allow read, write: if get(/admins/$(request.auth.uid)) != null

?或者有什么更漂亮的我可以使用?

【问题讨论】:

    标签: firebase google-cloud-firestore firebase-authentication firebase-security


    【解决方案1】:

    澄清一下,如果您要使用 get(),您需要输入完整路径。所以在你的情况下,它将是:

    allow read: if get(/databases/$(database)/documents/admins/$(request.auth.uid)).data.admin == true
    

    您可以阅读更多关于它的信息here

    【讨论】:

      【解决方案2】:

      这条规则确实允许任何人将自己标记为管理员,从而​​违背了您的规则的全部目的:

      allow read, write: if get(/users/$(request.auth.uid)).data.admin == true;
      

      你要做的是:

      • give the admin users a custom admin: true claim,然后您可以通过以下方式检查您的规则:allow read, write: if auth.token.admin === true;
      • 在您的安全规则中为您的管理员保留一份明确的 UID 列表,然后在每个规则中进行检查。所以:allow read, write: if isAdmin();,然后是function isAdmin(request) { return request.auth.uid == "KqEizsqUQ6XMkUgLUpNxFnzLm4F3" }
      • 在 Firestore 中的另一个(通常不可写入的)集合中保留管理员 UID 列表,并检查是否存在。这与您所描述的差不多,如果您想快速轻松地添加/删除管理员,这是最常用的方法。

      【讨论】:

      • 其实还有更好的选择:allow write: if !("admin" in request.resource.data);
      猜你喜欢
      • 1970-01-01
      • 2015-03-25
      • 2020-07-11
      • 2016-02-26
      • 2020-11-22
      • 2019-09-12
      • 2023-03-13
      • 2021-06-10
      • 1970-01-01
      相关资源
      最近更新 更多