[英]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.