12
match /UserProfile {
    match /{uId}{
    allow get: if isUserLoggedIn() && !isUserBlocked(uId);
  }

when i try to get data from UserProfile/{uId} using the above security rules it throws the following error in the firestore and in code it says insufficient permissions:

Error running simulation — Error: simulator.rules line [199], column [28]. Function not found error: Name: [get].

now the definition of above two function are here

function isUserLoggedIn(){
    return request.auth != null;
}

function isUserBlocked(uId){
    return (('blocked' in get(/databases/$(database)/documents/UserSettings/$(uId)).data) && (request.auth.uid in get(/databases/$(database)/documents/UserSettings/$(uId)).data.blocked));
}

the first function runs very well but the second one doesn't

it throws that error

and as of my knowledge the function is alright

please help i have wasted a whole lot of time on this piddly problem

what i have tried

  1. i read in one of the threads that it is a temporary problem but it is not like that. its been more than 48 hours now
  2. somewhere it was also mentioned that this is a bug only in simulator but the code will run smoothly and even this is not the case. in code the error is insufficient permissions as expected by me
  3. i have read the docs properly so my code is alright have tested the get method in other rules and there it is working fine

thats it please help

Community
  • 1
  • 1
Harkal
  • 1,770
  • 12
  • 28
  • Have you filed a bug for this? I'm seeing the same thing in my environment, not just in the simulator. Rules that have worked for months, and now suddenly don't. – HondaGuy May 06 '19 at 18:40
  • @HondaGuy firebase devs already know about this. look at the comments of the accepted answer where Doug Stevenson himslef[he works for firebase] says devs knows about it – Harkal May 06 '19 at 18:50

1 Answers1

7

Update: The errors are a bug in the rules simulator, see Doug's comment below.

I tried out your rules and they worked as expected in the simulator.

Rules:

match /UserProfile {
  function isUserLoggedIn(){
    return request.auth != null;
  }

  function isUserBlocked(uId){
    return (('blocked' in get(/databases/$(database)/documents/UserSettings/$(uId)).data) && (request.auth.uid in get(/databases/$(database)/documents/UserSettings/$(uId)).data.blocked));
  }

  match /{uId}{
    allow get: if isUserLoggedIn() && !isUserBlocked(uId);
  }
}

Test query in simulator:

get /UserProfile/foo
Authenticated: Yes
Firebase UID: bar

The request succeeds or fails based on the UserSettings/foo document in the database:

Denies request:

/UserSettings/foo    
{
 content: "my content"
 blocked: { bar: true }
}

Allows request:

/UserSettings/foo    
{
 content: "my content"
 blocked: { otheruser: true }
}

I think that errors can pop up when the data doesn't exist or isn't in the expected format.

For example, if I delete the /UserSettings/foo document I also receive:

Error: simulator.rules line [58], column [28]. Function not found error: Name: [get].

I also get this error if the blocked field is anything other than a map (because in is a function for maps):

Error: simulator.rules line [58], column [95]. Function not found error: Name: [in].

You can probably clean up these errors by using exists and checking the type of blocked but either way, your rules should still deny the request as expected.

Juan Lara
  • 6,454
  • 1
  • 22
  • 31
  • 2
    I would call this a bug in the simulator, as a non-existent document shouldn't yield an error like that. But it should fail the rule though. – Doug Stevenson May 03 '19 at 00:22
  • 10
    Confirmed with the team - it is a bug in the simulator. – Doug Stevenson May 03 '19 at 00:32
  • big thanks to both well this answer is fine actually that's the case yesterday only i discovered that this error appears on fields which do not exist and the simulator should throw an error like 'field does not exist' but it say 'method not found' and this thing wasted so much time – Harkal May 03 '19 at 05:56
  • @DougStevenson juan [unable to tag him] lol please read the above comment – Harkal May 03 '19 at 05:59
  • I tried to work around this error (make it work in simulator) this way: `function isAdmin() { return isAuth() && exists(/database/$(database)/documents/users/$(request.auth.uid)) && get(/databases/$(database)/documents/users/$(request.auth.uid)).data.admin == true; }` but adding the `exists()` call makes the simulator give "unknown error". – GaryO May 13 '19 at 21:18
  • Seems like this bug is still not fixed after more than two years – Edwin Liu Jul 30 '21 at 15:27
  • It is indeed a bug in the simulator (that is there up until today). If the data you try to get does not exist, rather than saying the rule is rejected it throws an error. Very lame and time consuming – Danielo515 Aug 15 '22 at 12:24