确保Firebase数据安全

问题描述:

我正在尝试首次设置Firebase规则。我想要做的是限制登录用户的访问权限,以便他们只能访问他们的数据。他们自己的数据是usersusercontacts。任何人都可以写信给contactmessages路线,任何人都无法阅读。确保Firebase数据安全

这套规则是否符合我的期望呢?我在模拟器中试过,得到了一个simulated write denied错误。我可能在模拟器上做错了什么。

{ 
    "rules": { 
    "users": { 
     "$user_id": { 
     // grants read/write access to the owner of this user account 
     // whose uid must exactly match the key ($user_id) 
     ".write": "$user_id === auth.uid", 
     ".read": "$user_id === auth.uid" 
     } 
    }, 
    "usercontacts":{ 
     "$user_id": { 
       ".write": "$user_id === auth.uid", 
       ".read": "$user_id === auth.uid" 
     } 
    }, 
    "contactmessages": { 
     ".read" : false, 
     ".write" : "$user_id === auth.uid" 
    } 
    } 
} 
+0

您可以编辑您的问题包含您尝试失败的写入操作?因此,您写入的完整路径,活动uid(如果有),您正在写入的数据以及现有数据(与写入规则相关)。 –

的这里的问题是你最后一次写入规则:

"contactmessages": { 
    ".read" : false, 
    ".write" : "$user_id === auth.uid" 
} 

您正在使用$ user_ID的,但没有任何$ user_ID的引用到。相反,如果你希望其他人能够写在这里,你应该使用这样的规则:

".write" : true 

或本条的规定,如果你想每一个身份验证的用户才能够写:

".write" : "auth != null" 

试试这个

{ 
    "rules": { 
    "users": { 
     "$userId":{ 
      ".read": "(auth != null) && ($userId === auth.uid)", 
      ".write": "(auth != null) && ($userId === auth.uid)" 
     } 
    }, 
    "usercontacts": { 
     "$userId": { 
       ".read": "(auth != null) && ($userId === auth.uid)", 
       ".write": "(auth != null) && ($userId === auth.uid)" 
     } 
     }, 
     "contactmessages": { 
     "$userId": { 
       ".read": "(auth != null) && ($userId === auth.uid)", 
       ".write": "(auth != null) && ($userId === auth.uid)" 
     } 
     } 
    } 
}