5

I have below data stored in my firebase:

firebaseRoot
    admins
        simplelogin:1: 
    users
        simplelogin:1
            email: abc@xyz.com
            picture: csd
            provider: password
            uid: simplelogin:1
        simplelogin:2
            email: abc1@xyz.com
            picture: zsd
            provider: password
            uid: simplelogin:1

and following security rules:

{
  "rules": {
    "admins": {
      ".read": "root.child('admins').child(auth.uid).val() === true",
      ".write": "root.child('admins').child(auth.uid).val() === true"
    },
    "users": {
      "$user":{
        ".read": "$user === auth.id || root.child('admins').child(auth.uid).val() === true",
        ".write": "$user === auth.id"
      }
    }
  }
}

My authorization requirements are as below.

  1. admins can be read and added only by the existing admin only. This works.
  2. All users can be read by the admin but should not be able to write user data.
  3. a user can read and update his own user data.

Currently with above rules, I am not able read users data both for admins and logged in users. I get below error message. Please provide your help. Thanks.

var rootRef = new Firebase('https://xxxxx.firebaseio.com/');
var users = rootRef.child('users');
users.on('value', function(snap) {
console.log(snap.key(), snap.val());
}, function(error) {
console.log(error);
});

Error:

Error: permission_denied: Client doesn't have permission to access the desired data.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Ajay
  • 105
  • 1
  • 9

2 Answers2

5

There are two pitfalls when it comes to Firebase security rules:

  1. rules cascade

    This means that once you give somebody (read or write) access on a certain level in the JSON structure, you cannot take that right away anymore on a lower level

  2. rules are not filters

    This means that you can only read a node if you have read access to all data in that node. If you only have read access to part of the data, a read operation for the complete data will fail.

In your security rules, you only give permission to read (some of) the children under users. So trying to read the entire users.on('value' will fail.

You can solve this by giving the administrator .read access to the users node.

    "users": {
      ".read": "root.child('admins').child(auth.uid).val() === true",
      "$user":{
        ".read": "$user === auth.id",
        ".write": "$user === auth.id"
      }
    }
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Thank you Frank. I had almost given up upon firebase security. I had read The security rules documentation over and over but I couldn't get it completely, your answer however makes me realize how simple and elegant it is. I just cannot thank enough! – Ajay Jun 10 '15 at 04:27
  • 1
    You're welcome. We're aware that both are non-intuitive, but that especially #2 sometimes makes functionality harder to implement than we'd like it to be. But for now, all we can do is explain it. Keep hacking, and keep asking! – Frank van Puffelen Jun 10 '15 at 04:32
  • @FrankvanPuffelen I find first example not clear here: https://firebase.google.com/docs/database/security/securing-data we can't access to the list of messages (/messages → not allowed), we can only access to one message (/message/message0 → allowed if timestamp < 10min for example ) – Emeric Sep 22 '18 at 16:51
  • Agreed @Curse. Please [file a bug report](https://firebase.google.com/support/contact/bugs-features/) explaining why this example is not clear. – Frank van Puffelen Sep 22 '18 at 22:17
1

Here is my working example:

ajsecuretest
    roles
        simplelogin:1
            role: 'admin'
        simplelogin:2
            role: 'editor'
    users
        simplelogin:1
             email: 'abc@xyz.com'
             picture: 'a.jpg'
             provider: 'password'
             uid: 'simplelogin:1'
        simplelogin:2
             email: 'xyz@abc.com'
             picture: 'b.jpg'
             provider: 'password'
             uid: 'simplelogin:2'

Rules:

{
  "rules": {
    "roles":{
      ".read": "root.child('roles').child(auth.uid).child('role').val() === 'admin'",
      ".write": "root.child('roles').child(auth.uid).child('role').val() === 'admin'",
      "$id" : {
        ".read" : "$id === auth.uid"
      }
    },
    "users": {
      ".read" :  "root.child('roles').child(auth.uid).child('role').val() === 'admin'",
      "$user":{        
        ".read": "$user === auth.uid",
        ".write": "$user === auth.uid"
      }
    }
  }
}
Ajay
  • 105
  • 1
  • 9