Austin Fay
Austin Fay

Reputation: 540

Realtime database emulator ignores database.rules.json

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

Upvotes: 7

Views: 1768

Answers (1)

Morten Nyhaug
Morten Nyhaug

Reputation: 283

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.

Upvotes: 4

Related Questions