Firebase数据库-逆向索引中的安全考虑

5
在 Firebase 指南中,建议之一是维护一个反向索引以跟踪用户操作。以下是我所指的代码片段:
// 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 字段的访问权限 - 这似乎是一种安全风险。

任何关于如何在尽可能保持访问限制的同时管理此类操作的想法?也许是另一个将用户已知标识符(例如电子邮件)映射到组列表的数据库对象?


尝试使用 Firebase 的 多路径更新 - zook2005
1个回答

1

解决方案1 - 客户端

一种解决方案是拥有一个单独的用户邀请对象,这样ghopper可以将alovelace添加到私人组,并在alovelace的邀请中显示,而不是自动将其添加到组中。然后,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进行处理。当ghopperalovelace添加为成员时,可能的事件顺序之一是:

  • Check that ghopper belongs to the group and can add another user (client-side)
  • Send a request to your server with payload that includes group name/id, user sending the request and email of the user being added

  • Server then looks up alovelace's user id using the provided email and update the user record.

    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内处理安全问题”的问题,这有点违背了提问的初衷。 - Itai Hanski
不确定我是否理解了您的评论。您问:“在最大限度地保持访问受限的情况下如何管理它?”,我回答说...在FB中管理它,但是在您的服务器上...毕竟,管理员API是Firebase的一部分。我没有建议其他随意无关的应用程序或API作为解决方案。 - WittyID
当然你没有。但是我想限制范围在Firebase内,为了评估Firebase。也许我的问题表述不够清楚。 - Itai Hanski
我现在更好地理解了您的意图,因此我相应地编辑了我的答案。 - WittyID

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接