Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 413
Harkal Avatar asked May 02 '19 15:05

Harkal


People also ask

How to change Security rules in Firestore?

To set up and deploy your first set of rules, open the Rules tab in the Cloud Firestore section of the Firebase console. Write your rules in the online editor, then click Publish.

How to check Firestore rules?

Open the Firebase console and select your project. Then, from the product navigation, do one of the following: Select Realtime Database, Cloud Firestore, or Storage, as appropriate, then click Rules to navigate to the Rules editor.

How to protect Firestore?

Use App Check to help ensure that only your app can access your Firestore data. For server client libraries, use Identity and Access Management (IAM) to manage access to your database. Learn how to secure your data for the Java, Python, Node. js, and Go client libraries with IAM.


1 Answers

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.

like image 185
Juan Lara Avatar answered Sep 19 '22 13:09

Juan Lara