首页 > 解决方案 > Serious problem Firebase read rule for blocked user

问题描述

I am facing with a serious problem with Firebase security rule. I want to set read to false to blocked user on search. Here is my database structure.

users:{
      uid1:{
           name:"Joe",
           created:1226287
          },
      uid2:{
          name:"John",
          created:1273725
          },
      uid3:{...},
      uid4:{...},
      ...
   },

 blocked:{
       uid1:{
            uid2: true,
            uid4: true
       }
   }

Consider above structure, assume that user2 search a user under users node by name typing "Joe" and click enter. I set my firebase rule like this.

 users:{
     $uid:{
         ".read":"!root.child('blocked').child($uid).hasChild(auth.uid)"
         }
     }

This rule works fine if user2 query user/user1 path. But when user2 searched, how can I prevent him from getting blocked user data.

标签: firebasefirebase-realtime-databasefirebase-security

解决方案


没有直接的方法可以获取没有阻止该特定用户的用户列表。但是,如果用户根据用户名或其他一些基于用户的标准进行搜索,那么最好的工具是 Firebase Functions。

实现功能的步骤(考虑基于名称搜索)(从客户端调用 firebase 函数并传递搜索名称):

  1. 获取所有具有相同名称的用户详细信息。
  2. 获取所有阻止请求用户的用户(或同名用户)。
  3. 弹出所有阻止请求用户的用户。
  4. 将用户列表发送回请求的用户。

为什么 Firebase Function 是这种情况下的最佳选择?因为这些功能是在安全的环境中执行的,并且可以绕过安全规则。

在此处阅读有关功能的更多信息。


推荐阅读