firebasegoogle-cloud-firestorefirebase-security

Firestore security rules error: [code=permission-denied]: Null value error. for 'list'


I'm writing my security rules for my Firebase chat app, but I'm getting the following error while using firebase emulators:

Uncaught Error in snapshot listener: FirebaseError: [code=permission-denied]: Null value error. for 'list'

When I run the same query using the Firestore rules playground, I get a different error:

Error: Invalid argument provided to call. Function: [get], Argument: ["||invalid_argument||"]

Here is my code:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {

    match /chats/{chatId} {

      allow read: if isChatParticipant(database, chatId);
    }
  }
}

function isChatParticipant(database, chatId) {
  let chat = get(/databases/$(database)/documents/chats/$(chatId));
  
  return chat != null && 
    (
      (chat.data.keys().has('customerUid') && isUser(chat.data.customerUid)) ||
      (chat.data.keys().has('sellerUid') && isUser(chat.data.sellerUid))
    );
}

The error points to the isChatParticipant(), where I check if the chat document is not null (as suggested from Doug Stevenson's answer), and that the person requesting access to the document is a participant in the chat.

How can I fix my firestore rules so that a request to read a chat document does not throw an error?


Solution

  • For the second error, Invalid argument provided to call. Function: [get], it's because I was not passing database to my function, and so the call to get was using an undefined variable.

    After fixing this, queries using the firebase rules playground succesfully ran without error.

    However, the error FirebaseError: [code=permission-denied]: Null value error. for 'list' still occurred when I ran the query through my app using the firebase emulators. It seems like this is an issue with the emulators itself, since there is no error when I run the query on Firestore rules playground.