// docs / baas security / firebase rules scanner
Firebase 規則掃描器:找出開放的 Firestore、Realtime Database 與 Storage 規則
Firebase 應用程式在安全性上以一致的方式失敗:allow read, write: if true; 規則是從測試模式快速入門留下、在進入生產前從未被替換的遺物。AI 編碼工具會從文件範例逐字產生這些規則,並很少提示開發者去強化它們。本文展示 Firebase 規則掃描器如何從專案外部偵測 Firestore、Realtime Database 與 Cloud Storage 上的開放規則 — 以及如何修復它發現的問題。
掃描器如何找到開放的 Firebase 規則
Firebase services are reachable at predictable public addresses, so a scanner with no credentials can check each one the way an anonymous visitor would. FixVibe's Firebase check covers all three services independently:
- Firestore. FixVibe finds the project from the deployed app's own Firebase configuration and reports collections that anonymous visitors can read.
- Realtime Database. FixVibe reports a database whose data anonymous visitors can read — often the whole tree at once.
- Cloud Storage. FixVibe reports buckets that anonymous visitors can list. Listable storage is a finding even when individual downloads are denied, because it lets attackers find guessable filenames.
測試模式陷阱的真實面貌
Firebase 的快速入門文件包含網路上最常被複製的規則區塊之一:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if true;
}
}
}Firebase 過去會為這些規則加上自動 30 天到期。情況已經改變:現今,除非開發者替換,否則規則會永久存續。AI 編碼工具 — 在多年含測試模式區塊的文件上訓練 — 經常逐字發出它,並告訴開發者「這就是你的安全規則」。它不是。
出現在生產中但同樣寬鬆的其他變體:
// future-date variant — equivalent to "if true" allow read, write: if request.time < timestamp.date(2099, 1, 1); // authenticated-user variant — any signed-in user reads and writes anything allow read: if true; allow write: if request.auth != null; // any-auth variant — any signed-in user owns every document allow read, write: if request.auth != null;
- 未來時間戳變體:一個允許一切直到遙遠未來日期的規則。實際上永不過期 (見上方醒目提示區塊)。
allow read: if true; allow write: if request.auth != null;— 公開讀取,任何已驗證使用者可寫入。allow read, write: if request.auth != null;— 任何登入的使用者都能讀或寫任何文件,包括其他使用者的資料。
掃描器發現開放規則時該怎麼做
開放的 Firebase 規則是執行時的緊急事件。三項服務上的修復形狀相同:將每條規則限定到 request.auth.uid,並比對明確的擁有者欄位。每項服務都有自己的規則語法:
Firestore
match /users/{userId} { allow read, write: if request.auth != null && request.auth.uid == userId; }。路徑區段繫結 {userId} 會成為使用者唯一能接觸的文件。
match /users/{userId} {
allow read, write: if request.auth != null
&& request.auth.uid == userId;
}即時資料庫
{ "rules": { "users": { "$uid": { ".read": "$uid === auth.uid", ".write": "$uid === auth.uid" } } } }。$uid 萬用字元擷取路徑區段以供比較。
{
"rules": {
"users": {
"$uid": {
".read": "$uid === auth.uid",
".write": "$uid === auth.uid"
}
}
}
}雲端儲存
service firebase.storage { match /b/{bucket}/o { match /users/{userId}/{allPaths=**} { allow read, write: if request.auth.uid == userId; } } }。慣例:將檔案存放於 users/[uid]/[filename],讓路徑強制所有權。
service firebase.storage {
match /b/{bucket}/o {
match /users/{userId}/{allPaths=**} {
allow read, write: if request.auth.uid == userId;
}
}
}Deploy rules via the Firebase CLI: firebase deploy --only firestore:rules, firebase deploy --only database, firebase deploy --only storage. Verify the new rules are in production by re-running the FixVibe scan — the open-rules finding should clear.
firebase deploy --only firestore:rules
firebase deploy --only database
firebase deploy --only storage與 Firebase 內建工具的比較
Firebase 主控臺向你顯示目前的規則,但不會根據執行時行為對其稽核。Firebase Rules 模擬器讓你以合成請求測試規則邏輯 — 有用但屬於本機。這兩個工具都不會告訴你生產規則實際對網際網路上的匿名攻擊者傳回什麼。像 FixVibe (或手動設定的 Burp Suite) 這樣的外部掃描器,是唯一從攻擊者相同角度進行探測的工具。Google 自家的 App Check 可緩解濫用,但無法取代正確限定範圍的規則。
常見問題
掃描器會讀取或修改我的 Firestore 資料嗎?
被動掃描對每項服務最多發出一次匿名讀取,以確認規則是否允許。掃描器記錄回應形狀與資料的存在 — 它不會分頁、不會列舉文件,也不會寫入。寫入探測受驗證的網域所有權所限制,絕不會針對未驗證的目標執行。
如果我的 Firebase 專案使用 App Check 會怎樣?
App Check 會用 403 拒絕未驗證的請求。沒有 App Check 權杖的掃描器在每個探測上都會看到 403 — 這是正確的結果。App Check 不是規則正確性的替代品 (被竊的 App Check 權杖加上開放的規則仍會洩漏資料),但它確實能擋下機會性的外部掃描。
掃描器能偵測部分規則設定錯誤 (讀開、寫關) 嗎?
Yes. Open read access and open write access are reported as separate findings, because data exfiltration and data manipulation are separate risks.
這對於部署在自訂網域下的 Firebase 應用程式有效嗎?
是的。掃描器從已部署的套件中擷取 Firebase 專案 ID,而非從網域。自訂網域、app.web.app 子網域,以及自架的 Firebase 應用程式,只要 JavaScript 套件可被取得,就能以相同方式運作。
後續步驟
Run a free FixVibe scan against your production URL — the Firebase rules check runs on every plan and flags open rules across Firestore, Realtime Database, and Cloud Storage. For a deeper explainer on the allow read, write: if true pattern specifically, see Firebase allow read, write: if true explained. For the umbrella view across Supabase, Firebase, Clerk, and Auth0, read BaaS misconfiguration scanner.
