firebasegoogle-cloud-firestorefirebase-security

How to access a non-default Firestore Database in Firebase Storage Rules?


When writing rules for Firebase Storage, I get a warning that the parameter for firestore.get must always start with /databases/(default)/documents, like this:

firestore.get(/databases/(default)/documents)

Additionally, according to the warning in the Firebase documentation, multiple databases must be enabled to access documents from the default Cloud Firestore database.

Currently, I have created both a default Firestore database and a custom database. Previously, I was only using the custom database.

When I use firestore.get(/databases/(default)/documents), the security rules work fine. However, since all my existing data is stored in the custom database, I need to access it instead.

How should I write the firestore.get rule to properly access my custom Firestore database instead of the default one?

Here is the complete security rule I am using:

service firebase.storage {
  match /b/{bucket}/o {
    function findCourse(course) {
      return course in firestore.get(/databases/(default)/documents/accounts/$(request.auth.uid)).data.authedCourse;
    }
    match /video/{course}/{allPaths=**} {
      allow read: if request.auth != null && findCourse(course);
    }
  }
}

The rule allows read access only if the authedCourse array contains the course value.

  1. firestore.get(/databases/custom/documents) This results in an error during simulation, stating that I must use /databases/$(database)/documents/.

  2. firestore.get(/databases/$(custom)/documents) This results in the following error:

Function not found error: Name: [firestore.get].; Error: Invalid argument provided to call. Function: [firestore.get], Argument: ["||invalid_argument||"]

  1. firestore.get(/databases/{custom}/documents) This fails to save and produces the following error:
Line 7: Missing 'match' keyword before path.;
Line 7: mismatched input 'creapple' expecting '}';
Line 7: Unexpected '-'.;  
Line 7: Unexpected '}'.;
Line 7: Missing 'match' keyword before path.;
Line 7: Forward slash '/' found where identifier or binding expected.;  
Line 7: mismatched input '$' expecting {'{', '/', PATH_SEGMENT};
Line 9: Unexpected 'match'.

Solution

  • From the documentation you can only access the default firestore database in storage rules.

    Warning: Storage Rules can only access documents from the default Cloud Firestore database when multiple databases are active.