4

My app is working just fine but when I use the Firebase emulators, I have a problem with the Storage rules not being obeyed. I have downloaded the storage.rules file and it is in the same directory as the firebase-json file. The Emulator suite launches just fine and I can see that the Storage emulator is working. However, when I try to upload an image (as I do in the live app) I get an error.

Error while uploading file: Error Domain=FIRStorageErrorDomain Code=-13021 "User does not have permission to access gs://my-stuff-7796d.appspot.com/Profiles/0ye7psTQA4xR6DfjZRXjjtCWKyPw.jpg." UserInfo={object=Profiles/0ye7psTQA4xR6DfjZRXjjtCWKyPw.jpg, ResponseBody={"error":{"code":403,"message":"Permission denied. No WRITE permission."}}, bucket=my-stuff-7796d.appspot.com, data={length = 74, bytes = 0x7b226572 726f7222 3a7b2263 6f646522 ... 73696f6e 2e227d7d }, data_content_type=application/json; charset=utf-8, NSLocalizedDescription=User does not have permission to access gs://my-stuff-7796d.appspot.com/Profiles/0ye7psTQA4xR6DfjZRXjjtCWKyPw.jpg., ResponseErrorDomain=com.google.HTTPStatus, ResponseErrorCode=403}

The storage.rules are:

service firebase.storage {
  match /b/{bucket}/o {
    match /{allPaths=**} {
      allow read, write: if request.auth != null;
    }
  }
}

Again, running against the live Firebase works just fine and the rules are obeyed. Here is my firebase.json file

  "firestore": {
    "rules": "firestore.rules",
    "indexes": "firestore.indexes.json"
  },
  "storage": {
    "rules": "storage.rules"
  },
  "emulators": {
    "auth": {
      "port": 9099
    },
    "firestore": {
      "port": 8080
    },
    "storage": {
      "port": 9199
    },
    "ui": {
      "enabled": true
    }
  }
}

When I launch my app, this is the code I initialize after call FirebaseApp.configure

Auth.auth().useEmulator(withHost:"localhost", port:9099)
Storage.storage().useEmulator(withHost:"localhost", port:9199)
let settings = Firestore.firestore().settings
settings.host = "localhost:8080"
settings.isPersistenceEnabled = false
settings.isSSLEnabled = false
Firestore.firestore().settings = settings

What am I missing, or is this a bug?

Stewart Lynch
  • 875
  • 9
  • 26
  • Further to his issue, If I manually add an image to the Storage bucket in the emulator console, I can read from my app. The app will just not upload the image, though it does in the live environment. If, after having successfully manually uploaded and read the image from the app, I try to upload a new one from the app, the upload fails, but the previous one that I had manually uploaded gets removed. – Stewart Lynch Oct 19 '21 at 16:17
  • One more update. I just updated to Firebase-tools 9.20.0 and now when I try to upload an image, the simulators just crash. :( – Stewart Lynch Oct 19 '21 at 17:46
  • At its present state the Firebase Storage emulator seems to be somewhat unreliable. Prone to crashes or behaving in an unpredictable manner. – Chris Schreiner Oct 21 '21 at 15:00
  • I have filed a bug report and it has been verified so waiting for an update – Stewart Lynch Oct 27 '21 at 23:32
  • @StewartLynch Could you link to the bug report you filed please? – joshpetit Dec 16 '21 at 11:29
  • oops. Sorry @joshpetit I missed that comment. This is what I found in my inbox regarding that bug report Case 10151612: Storage WRITE rules not being recognized in Storage.rules in Emulator Suite – Stewart Lynch Jan 18 '22 at 01:22

1 Answers1

0

I also had this issue. It seems to have been resolved for me in version 11.8.0. As a temporary workaround, I resorted to allowing all reads/writes so that I wasn't having to use my production environment and pay for usage. Not an ideal solution, but it unblocked me.

But for those who might be having similar issues, try updating to the latest firebase-tools:

npm install -g firebase-tools

Be sure to address any issues with:

npm audit fix

Or make the following change to the storage.rules file:

service firebase.storage {
  match /b/{bucket}/o {
    match /{allPaths=**} {
      allow read, write;
    }
  }
}