FixVibe

// docs / baas security / firebase rules scanner

Firebase 規則掃描器:找出開放的 Firestore、Realtime Database 與 Storage 規則

Firebase 應用程式在安全性上以一致的方式失敗:<code>allow read, write: if true;</code> 規則是從測試模式快速入門留下、在進入生產前從未被替換的遺物。AI 編碼工具會從文件範例逐字產生這些規則,並很少提示開發者去強化它們。本文展示 Firebase 規則掃描器如何從專案外部偵測 Firestore、Realtime Database 與 Cloud Storage 上的開放規則 — 以及如何修復它發現的問題。

掃描器如何找到開放的 Firebase 規則

Firebase 服務暴露眾所周知、可預測的 URL 形狀。沒有憑證的掃描器可以探測每一個服務並觀察匿名讀取是否成功。FixVibe 的 baas.firebase-rules 檢查在每項 Firebase 服務上各自執行三個獨立探測:

  • <strong>Firestore.</strong> The scanner extracts the project ID from the deployed app's bundle (it's in <code>firebase.initializeApp({ projectId: ... })</code>), then issues <code>GET https://firestore.googleapis.com/v1/projects/[project-id]/databases/(default)/documents/[collection]:listDocuments</code> against common collection names. A <code>200 OK</code> with documents in the response means <code>allow read</code> is permissive.
  • Realtime Database。掃描器探測 https://[project-id]-default-rtdb.firebaseio.com/.json。如果根節點可匿名讀取,回應就是整個資料庫樹的 JSON。較保守的測試查詢 .json?shallow=true,它只傳回頂層鍵 — 無論哪種情況都是一項發現。
  • Cloud Storage。掃描器查詢 https://firebasestorage.googleapis.com/v0/b/[project-id].appspot.com/o。如果回應在無驗證的情況下列出檔名,則該儲存桶可匿名列出。即使個別檔案下載被拒絕,可列出的儲存仍是一項發現 — 攻擊者會列舉儲存桶以找出可猜測的檔名。

測試模式陷阱的真實面貌

Firebase 的快速入門文件包含網路上最常被複製的規則區塊之一:

firebase
rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if true;
    }
  }
}

Firebase 過去會為這些規則加上自動 30 天到期。情況已經改變:現今,除非開發者替換,否則規則會永久存續。AI 編碼工具 — 在多年含測試模式區塊的文件上訓練 — 經常逐字發出它,並告訴開發者「這就是你的安全規則」。它不是。

出現在生產中但同樣寬鬆的其他變體:

firebase
// 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} 會成為使用者唯一能接觸的文件。

firebase
match /users/{userId} {
  allow read, write: if request.auth != null
                     && request.auth.uid == userId;
}

Realtime Database

<code>{ "rules": { "users": { "$uid": { ".read": "$uid === auth.uid", ".write": "$uid === auth.uid" } } } }</code>. The <code>$uid</code> wildcard captures the path segment for comparison.

json
{
  "rules": {
    "users": {
      "$uid": {
        ".read":  "$uid === auth.uid",
        ".write": "$uid === auth.uid"
      }
    }
  }
}

Cloud Storage

service firebase.storage { match /b/{bucket}/o { match /users/{userId}/{allPaths=**} { allow read, write: if request.auth.uid == userId; } } }。慣例:將檔案存放於 users/[uid]/[filename],讓路徑強制所有權。

firebase
service firebase.storage {
  match /b/{bucket}/o {
    match /users/{userId}/{allPaths=**} {
      allow read, write: if request.auth.uid == userId;
    }
  }
}

透過 Firebase CLI 部署規則:firebase deploy --only firestore:rulesfirebase deploy --only databasefirebase deploy --only storage。透過重新執行 FixVibe 掃描來驗證新規則已在生產中生效 — baas.firebase-rules 發現應該會被清除。

bash
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 權杖加上開放的規則仍會洩漏資料),但它確實能擋下機會性的外部掃描。

掃描器能偵測部分規則設定錯誤 (讀開、寫關) 嗎?

可以 — 每條規則 (allow readallow write) 都被獨立探測。以 200 OK 成功的唯讀探測會回報一項開放讀取發現,即使寫入被拒絕。這兩項發現是不同的:資料外洩與資料操縱是不同的風險。

這對於部署在自訂網域下的 Firebase 應用程式有效嗎?

是的。掃描器從已部署的套件中擷取 Firebase 專案 ID,而非從網域。自訂網域、app.web.app 子網域,以及自架的 Firebase 應用程式,只要 JavaScript 套件可被取得,就能以相同方式運作。

後續步驟

對你的生產 URL 執行免費的 FixVibe 掃描 — baas.firebase-rules 檢查在每個方案上都提供,並會標記 Firestore、Realtime Database 與 Cloud Storage 中的開放規則。關於 allow read, write: if true 樣式的深入說明,請參閱 Firebase allow read, write: if true 解析。關於跨 Supabase、Firebase、Clerk 與 Auth0 的總覽,請閱讀 BaaS 設定錯誤掃描器

// 掃描你的 baas 介面

在別人之前找到開放的資料表。

輸入一個生產 URL。FixVibe 會列舉你的應用程式通訊的 BaaS 提供者、識別其公開端點,並回報未經驗證的用戶端可以讀取或寫入什麼。免費、無需安裝、不需信用卡。

  • 免費方案 — 每月 3 次掃描,註冊免信用卡。
  • 被動 BaaS 指紋識別 — 不需要網域所有權驗證。
  • Supabase、Firebase、Clerk、Auth0、Appwrite 等。
  • 每項發現都附 AI 修復提示 — 可貼回 Cursor / Claude Code。
Firebase 規則掃描器:找出開放的 Firestore、Realtime Database 與 Storage 規則 — Docs · FixVibe