firebase - 在写入之前使用 Firebase 安全规则验证数据

标签 firebase firebase-realtime-database firebase-authentication firebase-security

通过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()"

但是,我认为这对我来说不是最好的方法,因为我不熟悉为此创建额外的服务器。 我在想是否有另一种方法,例如在安全规则中添加以下逻辑

  1. 当 Firebase 收到新日期时,Firebase 会查看“managerEmail”字段并获取该值。 -> newData.child('managerEmail')
  2. 然后,Firebase 使用 auth.uid 值进入 user/uid/managerEmail 以查看该值是否与获取的 managerEmail 匹配。
  3. 如果相同,则说明数据在客户端没有被修改。
  4. 如果它们不匹配,则意味着客户端以某种方式修改了用户变量

最佳答案

用户确实可以在客户端更改uid。

但 Firebase 的安全规则会在 Firebase 服务器上自动执行。他们无法伪造安全规则中 auth.uid 的值,除非他们知道以该用户身份登录的凭据。

换句话说:我可以轻松确定您的 Stack Overflow 用户 ID 是 7528750。但除非我也知道您的凭据(例如电子邮件+密码),否则我无法像您一样登录 Stack Overflow 并开始发帖.

关于firebase - 在写入之前使用 Firebase 安全规则验证数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42310755/

相关文章:

javascript - 使用从 Firebase 获取 Javascript 返回的数据

javascript - 如何分离 firebase 实时监听器?

firebase - 在BigQuery中支持西里尔文数据

javascript - Firebase 即使使用 if/else 语句 JS 也会进行多次调用

ios - Firebase Phone Auth reCAPTCHA 验证在某些设备上不起作用

android - Flutter 在尝试 Firebase 身份验证时无法捕获 PlatformException

firebase - 当用户确认使用 sendEmailVerification 发送的电子邮件时收到通知

angular - 为什么 .map 运算符在没有 .pipe 的情况下无法工作(Angular + Angularfire2/auth)?

javascript - firebase 交易减少值

angular - 如何在 AngularFire/TypeScript 中访问 FirebaseError 的 "code"属性?