1
votes

I'm trying to follow the examples laid out in the Firebase Security Rules for Cloud Storage Reference:

https://firebase.google.com/docs/reference/security/storage/

I copy & paste the example for split() into my Storage Rules and it won't compile/let me save:

Splits a string according to a provided regular expression and returns a list of strings. Uses Google RE2 syntax.

// Allow files named "file.*" to be uploaded
match /{fileName} {
  allow write: if fileName.split('.*\..*')[0] == 'file'
}

The error I get: Unexpected '.'.

For the life of me, I can't get the example code provided to run, nor can I make changes and get Regex to work as expected.

Anyone have any suggestions for Allow files named "file.*" to be uploaded?

2

2 Answers

3
votes

There seems to be a \ missing in the rules. Try this:

// Allow files named "file.*" to be uploaded
match /{fileName} {
  allow write: if fileName.split('.*\\..*')[0] == 'file'
}

In general when working with regular expressions, the backslashes are the first thing to check. Different systems use backslashes for escaping and sometimes (such as here) you'll need to double escape them.

0
votes

The documentation is wrong. Use this instead ('\.'):

// Allow files named "file.*" to be uploaded
match /{fileName} {
  allow write: if fileName.split('\\.')[0] == 'file'
}