Firebase安全规则检查要验证的新数据的值
我开始使用Firebase安全规则检查要验证的新数据的值,firebase,firebase-realtime-database,firebase-security,Firebase,Firebase Realtime Database,Firebase Security,我开始使用firebase为我的移动应用程序编写安全规则。我现在有以下安全规则 { "rules": { ".read": "auth != null", ".write": "auth != null || newData.child('appSecret').val() === '123'", "queue": { "tasks" : { ".indexOn": "_state"
firebase
为我的移动应用程序编写安全规则。我现在有以下安全规则
{
"rules": {
".read": "auth != null",
".write": "auth != null || newData.child('appSecret').val() === '123'",
"queue": {
"tasks" : {
".indexOn": "_state"
}
}
}
}
在写入规则中,我检查auth
数据是否不为空,或者插入的数据是否包含预先确定的应用程序机密。规则的第二部分是将任务添加到firebase队列
,以从服务器获取自定义令牌来验证移动应用程序用户。一旦我为应用程序添加了其他规则,我将把此规则移动到/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.
因此,插入的新数据不包含值为123
的子appSecret
。我将规则更新为
{
"rules": {
".read": "auth != null",
"queue": {
"tasks" : {
"$taskId" : {
".write": "newData.child('appSecret').val() == '123'",
},
".indexOn": "_state"
}
}
}
}
并且写入操作是允许的。当然,现在您还需要为数据库中的其他位置添加
write
规则,因为使用上述规则,write
将不允许在任何其他位置进行。您正在尝试将{“appSecret”:“123”,“task”:“GET_AUTH_TOKEN”}
写入路径/queue/tasks/23232
。因此,您最终将123
写入/queue/tasks/23232/appSecret
/
的写入规则规定,用户必须是身份验证用户,或者新数据必须包含值为123
的/appSecret
。由于情况并非如此,写入被拒绝。您正试图将{“appSecret”:“123”,“task”:“GET_AUTH_TOKEN”}
写入路径/queue/tasks/23232
。因此,您最终将123
写入/queue/tasks/23232/appSecret
/
的写入规则规定,用户必须是身份验证用户,或者新数据必须包含值为123
的/appSecret
。由于情况并非如此,因此拒绝写入。谢谢!我认为该规则只会检查正在插入的新数据,而不查看插入的位置,但一旦您解释了它,它就有意义了。我可以更新规则并使其生效。newData
表示该位置的新数据,因此它包括写入的数据和该位置中未修改的任何现有数据。谢谢!我认为该规则只会检查正在插入的新数据,而不查看插入的位置,但一旦您解释了它,它就有意义了。我能够更新规则并使其生效。newData
表示该位置的新数据,因此它既包括写入的数据,也包括该位置中未修改的任何现有数据。
{
"rules": {
".read": "auth != null",
"queue": {
"tasks" : {
"$taskId" : {
".write": "newData.child('appSecret').val() == '123'",
},
".indexOn": "_state"
}
}
}
}