【问题标题】:How can I prevent other sites/apps from accessing my Firebase?如何阻止其他网站/应用访问我的 Firebase?
【发布时间】:2015-02-12 21:50:05
【问题描述】:

我正在构建一个简单的示例应用程序,它基本上只允许网站上的所有用户互相聊天,就像聊天室一样。 Firebase 让我匿名验证用户身份,这正是我想要的,因为我只希望我的应用程序上的用户使用它。以下代码根据 Firebase 文档提供身份验证:

var ref = new Firebase("https://<your-firebase>.firebaseio.com");
ref.authAnonymously(function(error, authData) {
  if (error) {
    console.log("Login Failed!", error);
  } else {
    console.log("Authenticated successfully with payload:", authData);
  }
});

这很酷。有用。伟大的。但这是我的问题。

有什么办法可以防止有人简单地从我的源代码中复制我的 javascript 代码并针对我的 Firebase 运行他们自己的应用程序?由于身份验证方法位于我的应用程序的客户端,因此可以简单地复制粘贴并开始在我的 Firebase 上读写并修改所有内容。

我在仪表板中设置了这样的规则

{
  "rules": {
     ".read": "auth !== null",
     ".write": "auth !== null"
  }
}

我真的不知道这到底有多安全。我不能使用秘密令牌,因为它都是客户端。我错过了什么?

【问题讨论】:

标签: javascript firebase firebase-security


【解决方案1】:

只需创建检查用户是否具有特定属性的安全规则。您可以在仪表板中为您自己的用户表示(我们称之为“isAdmin”)设置此属性一次,然后如果该属性存在且为 true,则所有规则都返回 true。

快速示例:

{
  "rules": {
    // Allow everyone to read. This rule cannot be refined in deeper levels.
    // Once permission is granted at a certain level, it cannot be revoked
    // in a deeper level. However, the other way around works, as we see
    // next.
    ".read": true,

    // Always allow writes by users who have the isAdmin attribute. If this
    // evaluates to false, you can still have subrules in deeper levels that
    // may grant permission.
    ".write": "root.child('users/' + auth.uid + '/isAdmin').val() === true",

    "users": {
      "$uid": {
        // Users can write to their own entries, only. Except for the admin,
        // as his root rule already evaluated to true. The cascade is also
        // the reason why we need to check that the incoming isAdmin attribute is 
        // actually false. We don't want to give the user a blank cheque just
        // because he is himself ...
        ".write": "$uid === auth.uid && newData.child('isAdmin').val() === false",

        "isAdmin": {
          // No one can write this attribute, except users who already have
          // the attribute (see above). The cascade makes sure of that.
          ".write": false
        }
      }
    }
  }
}

免责声明:我没有对此进行广泛测试。

欲了解更多信息,请参阅https://www.firebase.com/docs/security/guide/securing-data.html#section-other-paths

【讨论】:

    猜你喜欢
    • 2023-03-12
    • 2016-11-27
    • 1970-01-01
    • 1970-01-01
    • 2017-11-03
    • 2015-10-07
    • 1970-01-01
    • 1970-01-01
    • 2013-06-10
    相关资源
    最近更新 更多