通过firebase登录后,我从firebase获取了uid。使用该 uid,我进入数据库并获取有关用户的更多信息
-user
-9a0rzPh3g5bqclxsarzx6pvd03 <- this is the user id
-name: John Doe
-companyId: Microsoft
-uid: 9a0rzPh3g5bqclxsarzx6pvd03
-email: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1b71747375357f747e5b767a727735787476" rel="noreferrer noopener nofollow">[email protected]</a>
-managerEmail: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="256f44484056674a4b416548444c490b464a48" rel="noreferrer noopener nofollow">[email protected]</a>
现在我的应用程序将用户的详细信息保存在名为 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: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7a301b171f093815141e3a171b131654191517" rel="noreferrer noopener nofollow">[email protected]</a>
-userEmail: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ffb5909791bb909abf929e9693d19c9092" rel="noreferrer noopener nofollow">[email protected]</a>
由于我的应用程序是基于 Angular 2 和 Typescript 构建的,因此我相信我的代码很容易被修改。 我担心的是,一旦下载了用户信息,用户就可以进入代码并将“currentUser”变量的companyId更改为其他人的。 因此,如果他有权访问另一家公司的 companyId 和经理电子邮件,他只需通过操作客户端代码就可以将表单提交给该经理。
我一直在阅读与 Security Rules API 相关的几个 Firebase 文档和 User based security rule 。我能看到解决此问题的唯一方法是自定义身份验证 token ,例如
".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 匹配。
- 如果相同,则说明数据在客户端没有被修改。
- 如果它们不匹配,则意味着客户端以某种方式修改了用户变量
最佳答案
用户确实可以在客户端更改uid。
但 Firebase 的安全规则会在 Firebase 服务器上自动执行。他们无法伪造安全规则中 auth.uid 的值,除非他们知道以该用户身份登录的凭据。
换句话说:我可以轻松确定您的 Stack Overflow 用户 ID 是 7528750。但除非我也知道您的凭据(例如电子邮件+密码),否则我无法像您一样登录 Stack Overflow 并开始发帖.
关于firebase - 在写入之前使用 Firebase 安全规则验证数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42310755/