【发布时间】:2017-07-07 17:40:58
【问题描述】:
通过firebase登录后,我从firebase获得了uid。使用该 uid,我进入我的数据库并获取有关用户的更多信息
-user
-9a0rzPh3g5bqclxsarzx6pvd03 <- this is the user id
-name: John Doe
-companyId: Microsoft
-uid: 9a0rzPh3g5bqclxsarzx6pvd03
-email: john.doe@mail.com
-managerEmail: JamesBond@mail.com
现在我的应用程序将用户的详细信息保存在名为 currentUser 的变量中。
由于我的应用程序是基于“表单”的应用程序,用户将表单提交给经理以供批准,当用户填写表单时,我会根据“companyId”将其保存到我的 firebase 节点 然后,经理 (James Bond) 将能够查询pendingForms 节点以查找发给他的任何内容
-forms
-Microsoft
-pendingForms
-KdAh5CCsbxvc1EVcbau <- this is the time stamped ID generated by firebase
-submissionDate: 72490175
-submistionNote: Please take a look
-managerEmail: JamesBond@mail.com
-userEmail: JohnDoe@mail.com
由于我的应用是基于 Angular 2 和 Typescript 构建的,我相信我的代码很容易被修改。 我担心的是,一旦下载了用户信息,用户就可以进入代码并将“currentUser”变量的 companyId 更改为其他人的。 因此,如果他可以访问另一家公司的 companyId 和经理电子邮件,他将能够通过操作客户端代码将表单提交给该经理。
我一直在阅读与 Security Rules API 和 User based security rule 相关的几个 firebase 文档。我可以看到解决此问题的唯一方法是自定义身份验证令牌,例如
".write": "auth.companyId === root.child('user/' + $companyId).child(auth.uid).child('managerEmail).val()"
但是,我不认为这对我来说是最好的方法,因为我不熟悉为此创建额外的服务器。 我在想是否有另一种方法,例如将以下逻辑添加到安全规则中
- 当 firebase 收到新日期时,firebase 会查看“managerEmail”字段并获取该值。 -> newData.child('managerEmail')
- Firebase 然后使用 auth.uid 值进入 user/uid/managerEmail 看看这个值是否与获取的 managerEmail 匹配。
- 如果它们相同,则表示客户端未修改数据。
- 如果它们不匹配,则意味着客户端以某种方式修改了用户变量
【问题讨论】:
标签: firebase firebase-realtime-database firebase-authentication firebase-security