Swift + Firebase安全无法正常工作 [英] Swift + Firebase Security not working
问题描述
我在Firebase控制台中创建了两个用户,他们都有不同的用户名和电子邮件地址。
我希望他们能够在线存储他们的分数数据库。这是结构:
AppName
- GameStats
- DBW9WQEs2sQn9CuPTE9t7Q1qWSz2
- 得分:0986
- Li75C2BYW7bQnKqMmrqLAZ67HUy4
- 得分:44131
访问此值并保留同步我正在使用这个:
$ b $ pre $ let $ baseRef = FIRDatabase.database()。参考(withPath:GameStats /+ user。 (快照在
打印(snapshot.value))
let scoreRef = scoreRef.child(Score)
scoreRef.observe(.value,
})
我想测试两个用户是否可以访问其他用户的信息。我改变了行,包括其他 user.uid
像这样:
let baseRef = FIRDatabase.database()。reference(withPath:GameStats / Li75C2BYW7bQnKqMmrqLAZ67HUy4)
$ p
//登录用户:DBW9WQEs2sQn9CuPTE9t7Q1qWSz2
$ p
$ b $ p $可选(44131)
如果我更改数据库中的值,它会自动更新为我放置的值。 / p>
这是错误的用户,由于某种原因可以访问它。
这些是我的规则:
{
rules:{
.read:auth!= null,
.write:auth!= null,
GameStats:{
$ user_id:{
.write:auth!= null& amp ;& auth.uid === $ user_id&& auth.provider ==='password',
.read:auth!= null&& auth.uid === $ user_id&& auth.provi der ==='password'
}
}
}
}
为什么应用程序允许一个用户读取另一个用户数据,以及如何限制访问权限,以便用户只能访问其userid下的数据?
正如@M_G建议的那样,我从父项和 .read
中取出了 .write
。所以我的规则是:
{
rules:{
//.read :$ auth!= null,
//.write:auth!= null,
GameStats:{
$ user_id:{
.write:auth!= null&& auth.uid === $ user_id&& auth.provider ==='password',
.read:auth!= null && auth.uid === $ user_id&& auth.provider ==='password'
}
}
}
}
我现在得到这个输出:
[FirebaseDatabase] setValue:或removeValue:at / GameStats / DBW9WQEs2sQn9CuPTE9t7Q1qWSz2失败:permission_denied - 这也适用于正确的用户。如果错误的用户也得到这个错误。 Firebase文档不正确(现在)。在Firebase控制台中,打开规则模拟器。在这个时候没有密码选项,我认为它是一个bug。
如果你没有使用多重身份验证或多个身份验证在你的项目中不重要,根据你的规则使用提供者。否则你可以测试这个规则进行密码认证:
。write:auth!= null&& auth。 uid === $ user_id&& auth.token.firebase.identities.email!== null
I have two users created in my Firebase console, both have a different username and email address.
I want them to be able to store their score online in the database. This is the structure:
AppName
- GameStats
- DBW9WQEs2sQn9CuPTE9t7Q1qWSz2
- Score : 0986
- Li75C2BYW7bQnKqMmrqLAZ67HUy4
- Score : 44131
To access this value and keep it synced I am using this:
let baseRef = FIRDatabase.database().reference(withPath: "GameStats/" + user.uid + "")
let scoreRef = scoreRef.child("Score")
scoreRef.observe(.value, with: { snapshot in
print(snapshot.value)
})
I wanted to test whether the two users could access other information from another user. I changed the line to include the other user.uid
like so:
let baseRef = FIRDatabase.database().reference(withPath: "GameStats/Li75C2BYW7bQnKqMmrqLAZ67HUy4")
// Logged in User: DBW9WQEs2sQn9CuPTE9t7Q1qWSz2
and for some reason it outputs this:
Optional(44131)
If I change the value in the database, it automatically updates the value to the one I put.
This is the wrong user and for some reason it is able to access it.
These are my rules:
{
"rules": {
".read": "auth != null",
".write": "auth != null",
"GameStats": {
"$user_id": {
".write": "auth != null && auth.uid === $user_id && auth.provider === 'password'",
".read": "auth != null && auth.uid === $user_id && auth.provider === 'password'"
}
}
}
}
Why is the app allowing one user to read another users data and how do I restrict access so that the user can only access the data under their userid?
As @M_G suggested, I took out the .write
from the parent and the .read
. So my rules are now:
{
"rules": {
// ".read": "auth != null",
// ".write": "auth != null",
"GameStats": {
"$user_id": {
".write": "auth != null && auth.uid === $user_id && auth.provider === 'password'",
".read": "auth != null && auth.uid === $user_id && auth.provider === 'password'"
}
}
}
}
I now get this output:
[FirebaseDatabase] setValue: or removeValue: at /GameStats/DBW9WQEs2sQn9CuPTE9t7Q1qWSz2 failed: permission_denied - This is for the correct user too. I get this error if wrong user also.
解决方案 Firebase document is wrong (for now). In Firebase console, open rule simulator. There is no 'password' option at this time, I think its a bug.
If you have not use multiple authentication or multiple authentication does not matter in your project, dont use provider at your rules. Otherwise you can test this rule for password authentication:
".write": "auth != null && auth.uid === $user_id && auth.token.firebase.identities.email !== null"
这篇关于Swift + Firebase安全无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!