Firebase Auth规则允许注册用户和公共用户访问-Angular-Ionic

时间:2020-08-07 19:11:25

标签: angular firebase google-cloud-firestore firebase-authentication firebase-security

我有两个使用相同Firestore数据库的应用程序。一个是后端Web应用程序(使用Angular),另一个是使用Ionic的前端应用程序。

我的后端应用程序将仅允许注册用户(和登录用户)访问数据库。我已经设置了Firebase Auth规则来限制基于uid的访问。这很完美。这是一个规则示例:

// Allow access to document if user signed in = uid on document
        match /fundraisers/{fundraiser} {
        allow write, read: if  if isOwner(resource.data.uid);
// Reusable function to determine document ownership
    function isOwner(userId) {
        return request.auth.uid == userId
    }

但是现在,我希望使用前端应用程序(在Ionic中)的任何人都能够阅读和更新筹款人文档。使用前端应用程序的用户无需登录或注册即可使用该应用程序。是否可以在Ionic应用程序中设置应用程序设置或某些变量,然后将其传递给Firebase Auth规则,以允许读取和更新对文档的请求?

预先感谢...

2 个答案:

答案 0 :(得分:1)

不可能简单地将一些参数从客户端传递到规则。这根本不是很安全,因为您不能相信客户端代码正确无误。

也不能仅将访问权限限制为您应用程序的用户。任何人都可以尝试使用Firestore REST API从Internet上的任何地方访问您的数据库。

如果您想要未经身份验证的访问,则您的规则只需要允许它。由于未经身份验证的用户将没有request.auth.uid == userId的任何值,因此您将无法要求request.auth之类的东西。

答案 1 :(得分:0)

这是非常不安全的,但是您可以做的是定义允许写入Firebase数据库的数据形状:

{
    "rules": {
      ".read": true,
      "$docId": {
        ".write": true,
        ".validate": "newData.hasChildren(['c','i','t','u']) || newData.hasChild('l')",
        "c": { ".validate": "newData.isString() && newData.val().length < 50000" },
        "i": { ".validate": "newData.isNumber()" },
        "l": { 
          "c": { ".validate": "newData.isNumber()" },
          "$other": { ".validate": "newData.isBoolean()" }
        },
        "t": { ".validate": "newData.isNumber()" },
        "u": { ".validate": "newData.isString() && newData.val().length < 50" },
        "$other": { ".validate": false }
      }
    }
  }

还有一个选项可以支持“匿名”登录https://firebase.google.com/docs/auth/web/anonymous-auth

至少可以帮助跟踪匿名用户的唯一ID,并有可能做一些基于Firebase函数的检查。

但是我同意这是非常不安全的。

相关问题