javascript - The Firebase security rules not works -
i have problem firebase rules. have root folder called 'users', containing many usersids again contains data, this:
users { userid1 { info_user { a: { a1: whatever a2: whatever a3: { a3.1: whatever a3.2: whatever a3.3: whatever a3.n: whatever } } n: {} ... } }, userid2 {}, ...n }
everything work fine when running following code if there no security rules, such:
{rules {".read" : true, ".write" : true}} function getuserinfo () { var dbref = firebase.database(); var myarray = []; dbref.ref('users').on('child_added', function(snapshot) { snapshot.foreach(function(childsnapshot) { myarray = (childsnapshot.val().a3); // }); }); }
my challenge change security rules without change database structure.
so, tried this:
{ "rules" : { ".read" : false, ".write" : false, "users": { "$userid": { "info_user": { "a":{ ".read": "auth != null", ".write": "(root.child('r').child('w').haschild(auth.uid))", "a1":{".validate": "newdata.isstring()"}, "a2":{".validate": "newdata.isstring()"}, "a3":{".validate": "newdata.isstring()"} }, "n": { ".read": "auth != null", ".write": "$user_id === auth.uid" } } } } } }
the expected result read node a3 when user authenticated.
how this?
firebase database read permission enforced when first attach listener. when attach listener dbref.ref('users')
, must have read permission /users
. in security rules not case.
to make rules work, move read permission `users:
{ "rules" : { "users": { ".read" : "auth != null", ...
note permission cascades down. once grant user read permission on /users
, have access data under /users
. cannot take permission away specific node under there.
this leads 1 of pitfalls of securing firebase database: rules cannot used filter data. more on this, see firebase documentation on "rules not filters", answer it , many more of questions developers struggling concept.
Comments
Post a Comment