【问题标题】:Firebase Security Rules Access newData Node NameFirebase 安全规则访问 newData 节点名称
【发布时间】:2019-05-29 06:44:36
【问题描述】:

我找不到任何方法来访问在 Firebase 规则中写入的数据的名称,这对我来说非常重要。 Firebase 规则是否支持这样的事情?

"tests":{
    "$test":{

      ".read": "auth.uid != null",
     ".write": "auth.uid  != null
         "$user_id":{".validate": "newData.isBoolean() && auth.uid == NewData.NAME(?)"}

    }
  }

将要写入的示例数据:

{
  "YHer1txgZyReM8dEkSCTDg4l2ro1": true
}

示例数据库结构:

"tests":{
    "test1":{

       "YHer1txgZyReM8dEkSCTDg4l2ro1": true,
       "A4sfgYhred1festfsefsSRGFsgr3": true,
       "7JIsfgYhred1festfsefsSRGFsg8": true,

    }
 "test2":{

      "YHer1txgZyReM8dEkSCTDg4l2ro1": true,
      "A4sfgYhred1festfsefsSRGFsgr3": true,
      "7JIsfgYhred1festfsefsSRGFsg8": true,

    }
  }

【问题讨论】:

  • 您能否编辑您的问题以包含您尝试使用的安全规则示例?
  • @FrankvanPuffelen 我写了一个快速示例。其背后的想法是我不希望用户在此公共 $test 节点下创建与用户 ID 不同的任何其他节点。
  • $test代替newData.NAME(?)
  • @FrankvanPuffelen 但它是针对它下面的节点的。此外,任何经过身份验证的人都必须可以创建 $test 节点。可能我应该使用 validate 。为了更好地理解,我添加了一个示例数据库结构。你能检查一下 Puffelen 先生吗?

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


【解决方案1】:

$user_id代替newData.NAME(?)

所以:

 "tests":{
    "$test":{

      ".read": "auth.uid != null",
     ".write": "auth.uid  != null
         "$user_id":{".validate": "newData.isBoolean() && auth.uid == $user_id"}"

    }
  }

另请参阅 securing user data 上的 Firebase 文档,其中包含一个非常接近您需要的示例。

【讨论】:

  • 答案的编辑版本允许具有不同用户 ID 的用户删除数据,因为 validate 不检查删除操作。有什么办法可以预防吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-15
  • 2022-01-19
  • 2019-08-14
  • 2020-06-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多