[英]Problem with Firebase Realtime Database rules
我对 Linux 中的 Firebase 实时数据库规则有疑问。我正在开发一个使用其 API 的项目,但我无法让它们工作。 在阅读了几乎所有可用的文档后,我在一个新项目中再次尝试,结果相同。 我在Ubuntu 20.04.4 LTS ,工具的版本更新了:
我在 Firebase 控制台中创建了项目,并使用firebase init
(实时数据库和模拟器)和npm init
进行了初始化,然后安装了 Firebase 库用于测试 ( @firebase/testing
) 和mocha 。
我用 firebase 模拟器启动了firebase emulators:start
了。
让我给你看代码。
{
"database": {
"rules": "database.rules.json"
},
"emulators": {
"database": {
"port": 9000
},
"ui": {
"enabled": true
}
}
}
{
"rules": {
".read": false,
".write": false,
"rooms": {
".read": false,
".write": false
}
}
}
{
"name": "frdrulesproblem",
"version": "1.0.0",
"description": "Firebase Realtime Database rules problem",
"main": "test.js",
"scripts": {
"test": "mocha --exit"
},
"author": "Author",
"license": "ISC",
"devDependencies": {
"mocha": "^9.2.1"
},
"dependencies": {
"@firebase/testing": "^0.20.11"
}
}
const assert = require('assert');
const firebase = require('@firebase/testing');
const MY_PROJECT_ID = "realtime-database-rules-problem";
const myId = "user_abc";
const theirId = "user_xyz";
const myAuth = {uid: myId, email: "abc@gmail.com"};
function getAuthedDatabase(auth) {
return firebase
.initializeTestApp({databaseName: MY_PROJECT_ID, auth: auth})
.database();
}
describe("", () => {
it("Shouldn't be able to read elements in the collection", async () => {
const userAbc = getAuthedDatabase(null);
await firebase.assertFails(userAbc.ref("rooms").once("value"));
});
it("Can read elements in the collection despite having denied read access", async () => {
const userAbc = getAuthedDatabase(null);
await firebase.assertSucceeds(userAbc.ref("rooms").once("value"));
});
});
测试结果:
> frdrulesproblem@1.0.0 test /home/author/Dev/FRDRulesProblem
> mocha --exit
1) Shouldn't be able to read elements in the collection
✔ Can read elements in the collection despite having denied read access
1 passing (65ms)
1 failing
1)
Shouldn't be able to read elements in the collection:
Error: Expected request to fail, but it succeeded.
at /home/author/Dev/FRDRulesProblem/node_modules/@firebase/testing/dist/index.cjs.js:257:31
at process._tickCallback (internal/process/next_tick.js:68:7)
npm ERR! Test failed. See above for more details.
当我打开指向 Firebase 模拟器 ( http://localhost:9000/.inspect/coverage
) 的本地链接时,它显示了错误的规则声明:
{
"rules": {
".read": true,
".write": true
}
}
我在 inte.net 中看到了类似的问题,但它是旧的并且与 Windows 相关。我是 Firebase 的新手并且正在测试但我认为代码是正确的。
请尝试这种方式可能是帮助你没有安全
这些规则给任何人,甚至是不是你应用程序用户的人,读写
访问您的数据库。 在开发过程中,您可以使用公共规则代替默认规则来将您的文件设置为公开可读可写。 这对于原型设计很有用,因为您无需设置身份验证即可开始。 这种访问级别意味着任何人都可以读取或写入您的数据库。 在启动您的应用程序之前,您应该配置更安全的规则。
//没有安全
{
“rules”: {
“.read”: true,
“.write”: true
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.