首页 文章

Firebase:您的实时数据库*****具有不安全的规则

提问于
浏览
-3

我每周都会从Firebase收到这封烦人的电子邮件:

[Firebase]您的实时数据库*****具有不安全的规则

我想沉默这封电子邮件 .

这样的答案引用了用户:Firebase email saying my realtime database has insecure rules

我的应用没有任何用户,也没有使用firebase /身份验证 .

如何以这种电子邮件消失的方式编写我的规则?

Firebase Web控制台上的我的数据库规则现在看起来像这样:

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

我的firebase.json:

{
  "database": {
    "rules": "database.rules.json"
  },
  "hosting": {
    "public": "build",
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ],
    "rewrites": [
      {
        "source": "**",
        "destination": "/index.html"
      }
    ]
  }
}

我的fire.js:

import firebase from 'firebase/app';
import 'firebase/database';
import 'firebase/storage';

const config = {
  apiKey: <API_KEY>,
  authDomain: <AUTH_DOMAIN>,
  databaseURL: <DATABASE_URL>,
  projectId: <PROJECT_ID>,
  storageBucket: <STORAGE_BUCKET>,
  messagingSenderId: <YADA_YADA>,
};
const fire = firebase.initializeApp(config);
export default fire;

2 回答

  • 0

    您的规则现在的方式,世界上任何人都可以读取和写入数据库的内容 . 这对您来说是一个安全问题,也可能是计费问题,因为有人可能会填满您的数据库,并且您需要为此付费 .

    如果您的应用没有任何用户且未使用身份验证,则听起来就像是您从您控制的后端服务中使用它 . 如果您只是使用admin SDK来读取和写入数据库,则可以将其锁定为:

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

    admin SDK具有对数据库的完全访问权限并忽略所有规则,因此这不会影响使用它的任何代码 .

  • 2

    我找到了一个解决方法 .

    使用一些电子邮件和密码在Firebase Web身份验证控制台中创建用户 .

    现在在你 fire.js 添加这个:

    firebase.auth().signInWithEmailAndPassword(<USERNAME>, <PASSWORD>).catch((error) => {
      console.log(error.code);
      console.log(error.message);
    });
    
    firebase.auth().onAuthStateChanged((user) => {
      if (user) {
        // User is signed in.
         console.log("IF YOU DON'T GET HERE SOMETHING IS WRONG");
      } else {
        // User is signed out.
         console.log("IF YOU GET HERE SOMETHING IS WRONG");
      }
    });
    

    在您的Firebase数据库规则控制台中粘贴以下内容:

    {
      "rules": {
        "<RESOURCE1>": {
            ".read": "auth != null",
            ".write": "auth != null"
        },
        "<RESOURCE2>": {
            ".read": "auth != null",
            ".write": "auth != null"
        },
        "<RESOURCE3>": {
            ".read": "auth != null",
            ".write": "auth != null"
        },
        "<RESOURCE4>": {
            ".read": "auth != null",
            ".write": "auth != null"
        }
      }
    }
    

    现在电子邮件警告将消失 .

相关问题