Firebase 3.0 安全规则 - 不允许推送匿名子项

Firebase 3.0 Security Rules - Don't allow to push anonymous children

我正在构建一个允许用户标记其工作时间的应用程序。在我的应用程序中有两种类型的用户。其中一个是只有读取权限的 'employee',另一个是具有读写权限的 'admin'。员工可以阅读自己的工作时间,管理员可以为每个 user/employee 创建新用户或添加工作时间。

管理员填写表格并推送数据。在 firebase 上,我检查了用户是否经过身份验证,并且还为用户对象的每个 属性 运行了验证。但我问自己,如果有人复制我的 firebase url(在源代码中可见)并试图将一些匿名对象或属性推送到我的数据库怎么办?

例如,我的注册表单需要以下字段:

{ 
   "users": {
      "-KInd4V0V9k5n6yhAETd": {
         "uid": "-KInd4V0V9k5n6yhAETd",
         "username": "Haris",
         "password": "HelloWolrd123",
         "age":      "21"
         }
    }
}

在 firebas 方面,我已经使用 newData 方法检查了子项是否从表单传递:

".validate": "newData.hasChildren(['uid','username','password,'age'])"

我已经 运行 对每个 属性 进行了验证,检查数据是否有效,例如 username.isString() 等

那么,如果管理员使用这样的开发人员工具将用户对象推送到我的数据库中怎么办 (已添加电子邮件 属性)

 {
    "-KInd4V0VEEEEEEEE": {
         "uid": "-KInd4V0VEEEEEEEE",
         "username": "Haris",
         "password": "HelloWolrd123",
         "age":      "21",

         // anonymous Object pushed from n admin developer-console
          "email": "anonymous@object.com"
         }
}

这会起作用,因为所有必填字段均有效,并且在 firebase 上通过了验证。但是我怎么能 拒绝 管理员 不能添加和传递 'email' 属性 到我的 firebase 数据库?因为我在数据库上的用户对象结构没有电子邮件 属性。我不想让用户可以将他自己的匿名 objects/data 推送到我的数据库。我需要验证每个 属性 还是有一种方法可以验证对象本身?比如 newData('users').isValid()?并且如果追加 属性 那么 isValid returns false 因为对象与数据库结构中的对象不一样?

我不完全确定,但我认为您正在寻找拒绝 所有其他 children 的规则。如果这确实是您要找的,那么:

{
  "rules": {
    "users": {
      "$uid": {
        // Valid users have these properties
        ".validate": "newData.hasChildren(['uid','username','password,'age'])",
        // The uid property must be a string and refer to an existing noder under /users
        "uid": {
          ".validate": "newData.isString() && root.child('users').child(newData.val()).exists()"
        },
        // the user name must be a string
        "username: {
          ".validate": "newData.isString()"
        },
        "password: {
          // I really hope you're not storing a password
        },
        "age: {
          // TODO: you're storing age as a string now, might want to fix that
          ".validate": "newData.isNumber()"
        },
        // All other properties are rejected
        "$other": {
          ".validate": false
        }
      }
    }
  }
}