7

I have a Firebase project that uses the realtime database, and I'm trying to set up the local emulator for testing. Unfortunately, it seems that the Firebase-cli is ingoring my database.rules.json file. This happens even after creating a test project to solve this specific problem.

Let me give you some info about my setup.

I created a Firebase project with nothing enabled. I created an empty directory on my local machine and ran firebase init database. Doing that created a few files:

.firebaserc

{
  "projects": {
    "default": "emulators-test-244be"
  }
}

firebase.json

{
  "database": {
    "rules": "database.rules.json"
  }
}

database.rules.json (I modified the rules here to lock down the database)

{
  "rules": {
    ".read": false,
    ".write": false
  }
}

When I run the emulator using firebase emulators:start --only database, the console tells me that the database successfully initializes. However, when I visit http://localhost:9000/.inspect/coverage?ns=emulators-test-244be to view the currently loaded security rules, it gives the following output:

{
    "rules": {
        ".read": true,
        ".write": true
    }
}

Obviously, this directly contradicts the rules I have set in the database.rules.json file.

One thing I did notice, is that if I modify the database.rules.json file while the emulator is running, I get the following output from the console:

i database: Change detected, updating rules for undefined...

Apparently, doing this causes the emulator to create a new database called undefined. When I go to http://localhost:9000/.inspect/coverage?ns=undefined, the output correctly reflects what I have set in my database.rules.json file.

Why is this happening? Am I incorrectly setting up my local Firebase project? Or is there a bug in the Firebase CLI? I've scoured the Firebase docs, Stack Overflow, and Google. I haven't found anything.

I have firebase tools version 8.4.3.

UPDATE

After searching through the issues on the firebase-tools GitHub repo, it seems that this is a bug.

Link here: https://github.com/firebase/firebase-tools/issues/2371

Austin Fay
  • 500
  • 3
  • 16
  • 1
    I had a similar issue today, my rules weren't being loaded. I noticed in my realtime database emulator console there were four different databases online: `myprojectid`, `emulatorui`, `fake-server`, `myprojectid-default-rtdb`. I don't know why, but the emulator was loading my project rules just for the last one. The solution was to change `databaseURL` in my firebase config to `http://localhost:9000?ns=vttrpg-db43b-default-rtdb`. I still don't know where is the configuration for the other three instances. – Leonardo Raele Apr 17 '22 at 03:03

1 Answers1

4

This workaround worked for me:

.firebaserc

{
  "projects": {
    "default": "emulators-test-244be"
  },
  "targets": {
    "emulators-test-244be": {
      "database": {
        "default": [
          "emulators-test-244be"
        ]
      }
    }
  }
}

firebase.json

{
  "database": [
    {
      "target": "default",
      "rules": "database.rules.json"
    }
  ],
  "emulators": {
    "database": {
      "port": 9000
    },
    "ui": {
      "enabled": true,
      "port": 4000
    }
  }
}

When I start the emulator with firebase emulators:start and visit: http://localhost:9000/.inspect/coverage?ns=emulators-test-244be I get the rules specified in my database.rules.json file.

Hope it also works for others with this problem.

Morten Nyhaug
  • 263
  • 2
  • 13