Search code examples
firebasegoogle-cloud-firestorefirebase-security

Function not found error: Name: [get]. in firestore security rules simulation


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


Solution

  • 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.