【问题标题】:Firebase Database - security consideration in an inverse indexFirebase 数据库 - 反向索引中的安全考虑
【发布时间】:2016-10-04 23:59:27
【问题描述】:

在 Firebase 指南中,建议之一是维护反向索引以跟踪用户操作。这是我所指的sn-p:

// An index to track Ada's memberships
{
  "users": {
    "alovelace": {
      "name": "Ada Lovelace",
      // Index Ada's groups in her profile
      "groups": {
         // the value here doesn't matter, just that the key exists
         "techpioneers": true,
         "womentechmakers": true
      }
    },
    ...
  },
  "groups": {
    "techpioneers": {
     "name": "Historical Tech Pioneers",
     "members": {
        "alovelace": true,
        "ghopper": true,
        "eclarke": true
      }
    },
    ...
  }
}

每个用户都在反向索引中跟踪他/她的组 - 在这种情况下,这意味着键保存实际值,而值无关紧要。


更新

我不确定如何在技术上更新索引,但经过一番研究后我得到了它:setValue 可以接受所有变量,而不仅仅是键值对。这意味着更新索引非常简单:只需获取对 groups/$group_id/members/$member_id 的引用并将其值设置为 true

现在我的问题不同了:

假设所有组都是私有的。这意味着用户只能通过邀请加入群组 - 当前群组成员必须将另一个用户添加到成员列表中。因此,如果我是 ghopper 并且我想将 alovelace 添加为成员,我需要更新她的索引,该索引是她的用户对象的一部分 - 这意味着我必须知道她的用户 ID 以某种方式对她的groups 字段具有写访问权限 - 这似乎存在安全风险。

对于如何在尽可能限制访问的同时管理此问题有什么想法吗?可能是另一个将用户已知标识符(如电子邮件)映射到组列表的数据库对象?

【问题讨论】:

标签: json indexing firebase firebase-realtime-database firebase-security


【解决方案1】:

解决方案 1 - 客户端

一种解决方案是有一个单独的用户邀请对象,以便 ghopper 可以将 alovelace 添加到私人组并显示 alovelace's 邀请而不是自动将她添加到群组中。 alovelace 需要批准添加并更新她的群组成员资格。这样,只有用户保留对其用户记录的访问权限。这与在 facebook 上添加朋友或在linkedin 上请求连接非常相似。

为了说明,架构可能看起来像这样

// An index to track Ada's memberships
{
  "users": {
    "alovelace": {
      "name": "Ada Lovelace",
      // Index Ada's groups in her profile
      "groups": {
         // the value here doesn't matter, just that the key exists
         // Only Ada can write here
         "techpioneers": true,
         "womentechmakers": true
      }
    },
    ...
  },
  "invitations": {
    "alovelace": {
      "name": "Ada Lovelace",
      "groups": {
         // the value here doesn't matter, just that the key exists
         // Anyone can write here
         "ghoppersfanclub": true, // Ada might accept this and move it to groups
         "explicitcontentgroup": true, // Ada might reject this and delete this entry
      }
    },
    ...
  },
  "groups": {
    "techpioneers": {
     "name": "Historical Tech Pioneers",
     "members": {
        "alovelace": true,
        "ghopper": true,
        "eclarke": true
      }
    },
    ...
  }
}

解决方案 2 - 服务器端

尽管 Firebase 旨在支持在没有服务器代码的情况下构建应用,但在某些情况下您应该将服务器纳入其中。在我看来,安全性和可信操作的执行,例如一个用户更改另一个用户的记录(如果我们不使用像上面的“邀请”这样的单独对象)应该由您的可信服务器使用管理 API 处理。当 ghopper 添加 alovelace 作为成员时,一个可能的事件顺序是:

  • 检查 ghopper 是否属于该组并且可以添加其他用户(客户端)
  • 向您的服务器发送一个请求,其中包含组名/ID、发送请求的用户和被添加用户的电子邮件

  • 服务器然后使用提供的电子邮件查找 alovelace 的用户 id 并更新用户记录。

    admin.auth().getUserByEmail(alovelace_email)
      .then(function(userRecord) {
        // Add group to alovelace's groups.
        // Trigger a client-side notification using child_changed
        // Allow alovelace to approve or decline addition to group
      })
      .catch(function(error) {
        console.log("Error fetching user data:", error);
      });
    

上述示例使用电子邮件作为公共/可共享的唯一标识符,但也有类似的 getUserByPhoneNumber(phoneNumber) 方法。

【讨论】:

  • 感谢您的回复,但您基本上是在用“使用其他东西”来回答“如何处理 FB 内部的安全问题”,这违背了问题的目的。
  • 不确定我是否理解您的评论。您问“关于如何在尽可能限制访问的同时管理此问题的任何想法?”我回答了....在FB中管理它,但在您的服务器上...管理API毕竟是firebase的一部分。我没有建议其他一些随机不相关的应用程序或 API 作为解决方案。
  • 不,你当然没有。但我的意思是将范围限制在 Firebase,为了评估 FB。也许我的问题不够清楚。
  • 我想我现在更好地理解了你的意图,我相应地编辑了我的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-14
  • 1970-01-01
  • 2018-05-02
  • 2018-02-02
  • 2010-09-30
  • 1970-01-01
相关资源
最近更新 更多