【问题标题】:Firebase security per user每个用户的 Firebase 安全性
【发布时间】:2015-06-17 15:33:15
【问题描述】:

我正在一个网站上工作,使用 firebase
安全性是:

{
  "rules": {
    "users": {
      ".read": true,
      ".write": true
    }
  }
}

所以每个人都可以添加他们的信息,但没有人可以访问主要部分。
但是当有人现在在控制台中输入这个时:

ref = new Firebase("https://xxx.firebaseio.com/users");
ref.createUser({
        email: email,
        password: password
}, function(error, userData) {});
ref.authWithPassword({
        email: email,
        password: password
    }, function(error, authData) {));
ref.remove();

所有用户数据都将被删除。 所有用户都有自己的 uid(例如 simplelogin:58)和 storageID(例如 -Js18LFoT0SmFi2Iq4GP
我可以用这些做点什么吗?我真的不希望任何人能够删除我的所有用户数据,但我需要让用户编辑他们自己的信息,并在他们愿意时删除他们的帐户。

这是我的一些代码:

function register() {
    var ref = new Firebase("https://fiery-heat-xxx.firebaseio.com/");
    ref.createUser({
        email: email,
        password: password
    }, function(error, userData) {
        if (error) {
            alert("Error creating user: " + error)
        } else {
            console.log("Successfully created user account with uid:", userData.uid);
            var uid = userData.uid


            var usersRef = new Firebase("https://fiery-heat-xxx.firebaseio.com/users/" + uid)
            var newUser = usersRef.set({
                faveShow1: "",
                faveShow2: "",
                faveShow3: "",
                faveShow4: "",
                faveShow5: "",
                faveShow6: "",
                faveShow7: "",
                faveShow8: "",
                faveShow9: "",
                faveShow10: "",
                uid: uid
            });
            //var key = newUser.key();
            //console.log(key)
            login();
        }
    });
}

function login() {
  clear();
    var ref = new Firebase("https://fiery-heat-xxx.firebaseio.com/");
    ref.authWithPassword({
        email: email,
        password: password
    }, function(error, authData) {
        if (error) {
            alert("Login Failed!" + error);
        } else {
            console.log("Authenticated successfully with payload:", authData);

            thisAuthData = authData.uid;
            var usersRef = new Firebase("https://fiery-heat-xxx.firebaseio.com/users/" + thisAuthData);

            usersRef.on("value", function(snapshot) {
                for (var i = 0; i < 1; i++) {
console.log(snapshot.val())
                    if (true) {
                        globalAuthData = snapshot.val();
                        //globalKey = amount;
                        var SS = snapshot.val()
                        show1 = SS.faveShow1;
                        show2 = SS.faveShow2;
                        show3 = SS.faveShow3;
                        show4 = SS.faveShow4;
                        show5 = SS.faveShow5;
                        show6 = SS.faveShow6;
                        show7 = SS.faveShow7;
                        show8 = SS.faveShow8;
                        show9 = SS.faveShow9;
                        show10 = SS.faveShow10;
                        //...//




                    }
                }

            }, function(errorObject) {
                alert("The read failed: " + errorObject.code);
            });




        }
    });
}

function removeUser() {
  clear();
    var ref = new Firebase("https://fiery-heat-xxx.firebaseio.com/");
    var refSer = new Firebase("https://fiery-heat-xxx.firebaseio.com/users/" + thisAuthData)


    ref.removeUser({
        email: email,
        password: password
    }, function(error) {
        if (error === null) {
            alert("User removed successfully");
            refSer.remove();
            logoff();
        } else {
            console.log("Error removing user:", error);
        }
    });
}

function edit() {
  clear();
    var fredNameRef = new Firebase('https://fiery-heat-xxx.firebaseio.com/users/' + thisAuthData);
    var onComplete = function(error) {
        if (error) {
            console.log('Synchronization failed');
        } else {
            console.log('Synchronization succeeded');
            console.log(thisAuthData);
            console.log(globalAuthData);
            login();
        }
    };
    if (document.getElementById("form1").value != "") {
        var show1 = document.getElementById("form1").value;
    }
    var show2 = document.getElementById("form2").value;
    var show3 = document.getElementById("form3").value;
    var show4 = document.getElementById("form4").value;
    var show5 = document.getElementById("form5").value;
    var show6 = document.getElementById("form6").value;
    var show7 = document.getElementById("form7").value;
    var show8 = document.getElementById("form8").value;
    var show9 = document.getElementById("form9").value;
    var show10 = document.getElementById("form10").value;


    fredNameRef.update({
        faveShow1: show1,
        faveShow2: show2,
        faveShow3: show3,
        faveShow4: show4,
        faveShow5: show5,
        faveShow6: show6,
        faveShow7: show7,
        faveShow8: show8,
        faveShow9: show9,
        faveShow10: show10,
    }, onComplete);

}

function logoff() {
  clear()
    var ref = new Firebase('https://fiery-heat-xxx.firebaseio.com/')
    ref.unauth();
    //...//
    }
}

和我的安全规则:

{
  "rules": {
    "users": {
      "$user_id": {
         ".write": "$user_id === auth.uid"
      },
     ".read": true
    }
  }
}

但我现在无法注册或更新...

【问题讨论】:

    标签: javascript html security firebase firebase-security


    【解决方案1】:

    为确保用户的信息只能由该用户编辑,您需要使用auth.uid

    https://www.firebase.com/docs/web/guide/understanding-security.html

    最重要的内置变量是 auth。这个变量是 在您的用户进行身份验证后填充。它包含有关他们的数据 和 auth.uid,一个唯一的字母数字标识符,适用于 提供者。 auth 变量是许多规则的基础。

    {
      "rules": {
        "users": {
          "$user_id": {
            ".write": "$user_id === auth.uid"
          }
        }
      }
    }
    

    为了更清楚一点,auth.uid 指的是当前登录的用户,$user_id 指的是数据库中的位置。 $ 指向 $location 规则变量:

    https://www.firebase.com/docs/security/api/rule/path.html

    {   "rules": {
        "users": {
          "$user": {
            ".read": "auth.uid === $user",
            ".write": "auth.uid === $user"
          }
        }  
      } 
    }
    

    当用户对 Firebase 应用进行身份验证时,会发生三件事:

    • 在客户端的回调中返回有关用户的信息 设备。这允许您自定义应用程序的用户体验 那个特定的用户。

    • 返回的用户信息包含一个uid(一个 唯一 ID),保证在所有提供商中都是不同的, 并且永远不会针对特定的经过身份验证的用户进行更改。 uid 是一个 包含您正在验证的提供者名称的字符串 with,后跟一个冒号和提供者返回的唯一 ID。

    • 应用的 Security and Firebase 中 auth 变量的值 规则变得明确。此变量为 null 表示未经身份验证 用户,但对于经过身份验证的用户,它是一个包含 用户的唯一 (auth.uid) 和可能的有关用户的其他数据。 这使您可以安全地控制每个用户的数据访问。

    【讨论】:

    • 好的,但是如果我使用 push,我会得到另一种类型的 ID:一种格式为 -Js18LFoT0SmFi2Iq4GP 的 ID,其中我还存储了用户的 UID 和有关用户的其他信息。我该如何为此制定规则?所以用户只能在那个“文件夹”中编辑与他的 UID 一起的东西???
    • 或者有没有更简单的方法来存储多行数据,他们可以在用户的​​帐户中编辑这些数据?
    • 当您使用push() 时,您将添加到列表中。您看到的 id 是 Firebase 对新列表成员的引用。如果数据具有定义的路径(如用户名),您想使用 set()
    • 您是否在仪表板中使用模拟器?这可以为您提供有关 permission_denied 的更多详细信息
    • 你想这样保存usersRef.set({ "Jon Snow": { faveShows: { faveShow1: "Breaking Bad", faveShow2: "The Walking Dead", faveShow3: "Game of Thrones" } } });
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-17
    • 1970-01-01
    • 1970-01-01
    • 2018-02-25
    • 1970-01-01
    • 2015-12-29
    相关资源
    最近更新 更多