我开始用firebase为我的移动应用程序制定安全规则。我现在有低于安全的规定
{
"rules": {
".read": "auth != null",
".write": "auth != null || newData.child('appSecret').val() === '123'",
"queue": {
"tasks" : {
".indexOn": "_state"
}
}
}
}在写规则中,我检查auth数据是否为null,或者正在插入的数据包含预先确定的应用程序机密。规则的第二部分是向firebase-queue添加任务,以便从服务器获得一个自定义令牌来验证移动应用程序用户。一旦我为应用程序添加了其他规则,我将把这个规则移到/queue/tasks。
使用上面的规则,我在向队列中添加任务时遇到了困难。我在模拟器中尝试了一个写,得到了下面的输出。
Attempt to write {"appSecret":"123","task":"GET_AUTH_TOKEN"} to /queue/tasks/23232 with auth=null
/:.write: "auth != null || newData.child('appSecret').val() === '123'"
=> false
/queue
/queue/tasks
/queue/tasks/23232:<no rules>
No .write rule allowed the operation.
Write was denied.当插入的数据将appSecret设置为123时,为什么拒绝写入操作?
弗兰克回答后的更新,
安全性不仅是插入的检查数据,还包括插入的位置。因此,在我的上述规则中,我试图说,当您在/中插入数据时,请确保用户经过身份验证,或者插入的数据包含一个值设置为123的子appSecret,而我试图在location /queue/tasks/23232中插入数据。所以,本质上,我在/插入的内容是
{
'queue': {
'tasks': {
'23232': {
"appSecret":"123",
"task":"GET_AUTH_TOKEN"
}
}
}
}因此,插入的新数据不包含值为appSecret的子123。我把规则更新为
{
"rules": {
".read": "auth != null",
"queue": {
"tasks" : {
"$taskId" : {
".write": "newData.child('appSecret').val() == '123'",
},
".indexOn": "_state"
}
}
}
}写操作是允许的。当然,现在还需要为数据库中的其他位置添加write规则,因为根据上述规则,在任何其他位置都不允许使用write。
发布于 2016-06-08 04:09:07
您正在尝试将{"appSecret":"123","task":"GET_AUTH_TOKEN"}写到路径/queue/tasks/23232。因此,您最终会将123写到/queue/tasks/23232/appSecret。
/的写入规则规定,用户必须进行身份验证,或者新的数据必须包含具有值123的/appSecret。由于情况并非如此,所以该写被拒绝。
https://stackoverflow.com/questions/37693029
复制相似问题