Firestore security rules - wildcarding Collection Names?

Viewed 415

I have a set of Collections whose names all start with ABC and I want to write a single rule that applies to all of them regardless of what follows ABC. Something like:

match /ABC*/{anyid} {
  allow read, write;
}

Is this possible? In the Rules Console there are no syntax errors highlighted, but the Simulator won't allow me to access the table with:

GET /ABC123/456

Any ideas?

1 Answers

As far as I know it is not currently possible to match on a partial collection (or document) name. It sounds like an interesting feature request though, so I recommend filing a feature request.

In the meantime, the only thing I can think of is matching all collections, and then testing the path through resource['__name__']:

match /53829635/{document} {
  match /{col}/{doc} {
    allow read: if resource['__name__'][5].matches('ABC.*')
  }
}

The resource['__name__'] expression returns a Path, which can be indexes as an array to get the path segments. It has a form /databases/(default)/documents/collection/document, so the subcollection is at index 5. Since that is just a string, we can use matches on it. In this case I allow reading from any subcollection whose name starts with ABC.


Update: it turns out that you can also simply access the col wildcard, instead of looking up from the path. So this would work the same:

allow read: if col.matches('ABC.*')
Related