[go: nahoru, domu]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

database.rules.json is not applied to the default RTDB instance in the emulator. #3124

Closed
aameen951 opened this issue Feb 10, 2021 · 4 comments · Fixed by #3146
Closed

database.rules.json is not applied to the default RTDB instance in the emulator. #3124

aameen951 opened this issue Feb 10, 2021 · 4 comments · Fixed by #3146
Assignees

Comments

@aameen951
Copy link

Hi there,

This #2979 pull request fixed one part of the problem mentioned in #2965 where the default instance in hosting is different from the default instance in firebase-admin and both of them now reference the same instance which is project-id-default-rtdb.

However, the rules in database.rule.json are still being applied to project-id instead of project-id-default-rtdb, and I don't know how to change that manually.

[REQUIRED] Environment info

firebase-tools: 9.3.0
Platform: Windows

[REQUIRED] Test case

functions/index.js

const admin = require("firebase-admin");

// After #2979, The following line will uses `http://localhost:9000?ns=project-id-default-rtdb`
admin.initializeApp();

admin.database().ref('test').set(1);

database.rules.json

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

[REQUIRED] Steps to reproduce

  1. Run the command: curl localhost:9000/.settings/rules.json?ns=project-id-default-rtdb -H "Authorization: Bearer owner"
  2. The output is:
{
    "rules": {
        ".read": true,
        ".write": true
    }
}
  1. Run the command: curl localhost:9000/.settings/rules.json?ns=project-id -H "Authorization: Bearer owner"
  2. The output is:
{
  "rules": {
    "test":{
      ".read": true
    },
    ".read": false,
    ".write": false
  }
}

[REQUIRED] Expected behavior

Since project-id-default-rtdb is now the default instance, the database.rules.json should also be applied to project-id-default-rtdb instance instead of project-id instance.

[REQUIRED] Actual behavior

Now, database.rules.json is applied to project-id.

@StevenHallLtd
Copy link

This has been driving me crazy. Do you know of a workaround @aameen951? I tried using the instance flag on the CLI, and also setting a target in firebase.json, but to no avail. I'm not sure I'm setting it right as the docs aren't that explicit.

@samtstern samtstern self-assigned this Feb 18, 2021
@samtstern
Copy link
Contributor

Thanks @StevenHallLtd and @aameen951 for reporting, let me take a look at this.

@aameen951
Copy link
Author
aameen951 commented Feb 18, 2021

@StevenHallLtd At first, I didn't know it was happening. But when I start to use queries with filters it became an annoying problem.
I worked around it by programmatically setting the rules on the database from firebase functions.
In your functions/src/index.js do the following:

import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';

import { promises as fs } from 'fs';
import * as path from 'path';

// This is the database instance
const db = admin.database();

// check if the firebase function is running inside the emulator.
const IS_INSIDE_EMULATOR = process.env.FUNCTIONS_EMULATOR === 'true';

if(IS_INSIDE_EMULATOR)
{
  // read the rules file.
  fs.readFile(path.join(__dirname, "../../../database.rules.json"), 'utf8').then((source) => {
    // manually set the rules on the database.
    db.setRules(source);
  });
}

The one downside to this workaround so far is that if you change the rules file it won't update automatically, you have to go to any file that belongs to functions and save it to make functions reload and cause the above code to run again.

@samtstern
Copy link
Contributor

Fixed in #3146 and will be included in the next release.

This was referenced Mar 9, 2021
This was referenced Mar 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants