【问题标题】:iOS -Is Write Denied Error In Firebase Security Rules Or In The Rules Simulator?iOS - Firebase 安全规则或规则模拟器中的写入被拒绝错误?
【发布时间】:2018-01-20 05:00:55
【问题描述】:

我的数据库结构是:

root
  |
  @-users
  |   |
  |   @-uid
  |      |
  |      @-registration
  |      |     |
  |      |     |-completed: true
  |      |
  |      @-sneakersPath//registered users post/write sneaker for sale to this path
  |            |
  |            |
  |            @-autoID//i.e. xyz123
  |                |-sneakercondition: "used"
  |                |-sneakername: "nike"
  |
  |
  @searchSnkPath//registered and anonymous users can search/read the sneakers for sale here
      |
      @-autoID//xyz123
          |-sneakercondition: "used"
          |-sneakername: "nike"

我正在开发一个只有登录用户才能使用的 iOS 应用,并且我使用 Firebase 作为我的后端。我正在使用 2 种 Firebase 登录方法:电子邮件/密码和匿名。我有一个登录场景,其中包含一个电子邮件/密码字段和一个以匿名用户身份输入的按钮。

有两种情况:

  1. 用户使用Email/Password 方法创建帐户。他们必须填写电子邮件和密码字段。一旦他们这样做了,这个用户就会得到一个永久的 uid 并创建一个 registration path。在该注册路径中,我添加了一个键/值对 completed:true。从那时起,用户按下登录按钮进入应用程序(他们使用其永久 uid 登录)。该用户可以将(write) 出售的运动鞋发布到sneakersPath。他们还可以在(read) 搜索他们或其他用户发布的待售运动鞋。

  2. 匿名用户使用该应用程序而不使用Anonymous 方法创建帐户,该方法是一个匿名按钮。他们按下匿名输入按钮,他们可以进入应用程序。一旦他们按下该按钮,他们就会获得一个对他们的访问唯一的临时 uid(他们使用临时 uid 登录)。由于该用户是匿名用户,因此该用户唯一能做的就是搜索(read) 第一个场景中的其他用户发布的待售内容。一旦他们注销,如果他们以匿名方式重新登录,他们会得到一个不同的临时 uid。

在这两种情况下,我都会对client side 进行检查,以验证用户是作为注册用户登录还是匿名用户登录。基于这些检查,我可以阻止匿名用户匿名发布运动鞋。一切正常。

尽管我在 iOS 端运行检查,但我希望在我的 Firebase 规则中进行一些额外的强制执行,以确保匿名用户不能发布任何内容,并且我想特别对 kvp completed:true 运行检查:

root.child('users').child($uid).child('registration').child('completed').val() === true"

现在我的安全规则是:

{
  "rules": {
    "users": {
       "$uid": {
            ".read": "$uid === auth.uid && auth != null"
            ".write": "$uid === auth.uid && auth != null && root.child('users').child($uid).child('registration').child('completed').val() === true"   
   }   
  }
 }
}

当我进入 Firebase 规则模拟器并尝试编写时,我得到:

在下图中,我已将 Authenticated 切换到“on”,而我得到的唯一选择是 Anonymous、Google、Facebook、Twitter 和 Custom。由于我使用的是电子邮件/密码并且未列出,因此我选择了自定义。我也没有选中管理框,因为我不知道它的用途。

我是在我的 Json 安全规则中做错了什么,还是我错误地使用了规则模拟器?

【问题讨论】:

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


    【解决方案1】:

    问题在于,在 Firebase 数据库中,我必须添加与 Firebase 在模拟器中提供给我的完全相同的 foo uid 数据。

    root
      |
      @-users
      |   |
      |   @-uid//this is another user's uid
      |   |  |
      |   |  @-registration
      |   |  |     |
      |   |  |     |-completed: true
      |   |  |
      |   |  @-sneakersPath
      |   |        |
      |   |        |
      |   |        @-autoID
      |   |            |-sneakercondition: "used"
      |   |            |-sneakername: "nike"
      |   |
      |   |
      |   @59c96197-7cf9-44d3-bfbc-8d4f3324461//this is the foo uid the simulator provided. It must be added in the db so it can read it
      |   |  |
      |   |  @-registration
      |   |  |     |
      |   |  |     |-completed: true
      |   |  |
      |   |  @-sneakersPath
      |   |        |
      |   |        |
      |   |        @-autoID//abc789
      |   |            |-sneakercondition: "new"
      |   |            |-sneakername: "reebok"
      |
      @searchSnkPath
          |
          @-autoID//xyz123
          |    |-sneakercondition: "used"
          |    |-sneakername: "nike"
          |
          |
          |
          @-autoID//abc789
              |-sneakercondition: "new"
              |-sneakername: "reebok"
    

    这是模拟器提供的 foo 路径/uid 数据:

    注意 foo uid 也在令牌负载中:

    如果您要使用数据库中的真实数据在模拟器中进行测试,请确保删除模拟器提供的 foo 路径/uid 并将其替换为数据库中的任何路径/uid,否则您将不断收到模拟写入拒绝响应。

    在令牌有效负载中,您可以保留 "provider" : "anonymous" 以与数据库中的真实数据一起使用,但请务必将 foo uid 替换为您使用的任何一个

    【讨论】:

      猜你喜欢
      • 2016-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-21
      • 1970-01-01
      • 1970-01-01
      • 2020-03-22
      • 2020-08-16
      相关资源
      最近更新 更多