修正不安全的規則

本指南將說明 Cloud Firestore Security Rules 設定中的常見安全漏洞,協助您檢查及強化規則,並在部署前測試變更。

如需 AI 輔助,請將下方提示詞複製到 AI 程式設計代理 (例如 Android Studio 內建 Gemini、Antigravity、Claude Code、Codex 和 Cursor)。您可以自訂提示,並與代理反覆調整。如需詳細安裝資訊,請參閱 Firebase 代理技能。

Help me use Firebase in my app.
1. Make sure that **Firebase agent skills** are installed and updated. For Claude Code, Codex, or Cursor agents, help me install the Firebase plugin. For other agents, run `npx skills add firebase/agent-skills`. Always try to use **Firebase agent skills** for Firebase-related tasks.
2. Help me write, validate, and deploy Firebase Security Rules to protect my app's Firestore data.

如果收到快訊,指出 Cloud Firestore 資料庫未妥善保護,您可以修改並測試 Cloud Firestore Security Rules,解決安全漏洞。

如要查看現有的安全性規則,請前往 Firebase 控制台的「Rules」(規則) 分頁。

瞭解你的Cloud Firestore Security Rules

Cloud Firestore Security Rules 可保護資料,避免惡意使用者存取。在 Firebase 控制台中建立的任何 Cloud Firestore 執行個體,預設規則都會拒絕所有使用者存取。如要開發應用程式及存取資料庫,您必須修改這些規則,並考慮在開發環境中向所有使用者授予全面存取權。不過,將應用程式部署至正式環境前,請務必妥善設定規則並保護資料。

開發應用程式及測試規則的不同設定時,請使用 Cloud Firestore 模擬器在本機開發環境中執行應用程式。

不安全規則的常見情境

您可能已預設設定 Cloud Firestore Security Rules,或在最初使用 Cloud Firestore 開發應用程式時設定,請務必在部署應用程式前檢查並更新。請避免下列常見錯誤,確保使用者資料安全無虞。

公開存取

設定 Cloud Firestore 時,您可能已將規則設為在開發期間允許開放存取。您可能認為自己是唯一使用應用程式的人,但只要部署應用程式,就能在網際網路上使用。如果您未驗證使用者及設定安全性規則,那麼只要有人猜出您的專案 ID,就能竊取、修改或刪除資料。

不建議:所有使用者皆可讀取及寫入。
// Allow read/write access to all users under any conditions
// Warning: **NEVER** use this rule set in production; it allows
// anyone to overwrite your entire database.

service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if true;
    }
  }
}
解決方案:限制讀寫權限的規則。

請根據資料階層建立合適的規則。其中一項常見的做法是建立以使用者為基礎的安全防護機制,並搭配使用 Firebase Authentication。進一步瞭解如何使用規則驗證使用者。

僅限內容擁有者

service cloud.firestore {
  match /databases/{database}/documents {
    // Allow only authenticated content owners access
    match /some_collection/{document} {
      // Allow reads and deletion if the current user owns the existing document
      allow read, delete: if request.auth.uid == resource.data.author_uid;
      // Allow creation if the current user owns the new document
      allow create: if request.auth.uid == request.resource.data.author_uid;
      // Allow updates by the owner, and prevent change of ownership
      allow update: if request.auth.uid == request.resource.data.author_uid
                    && request.auth.uid == resource.data.author_uid;

    }
  }
}
  

混合公開和私人存取權

service cloud.firestore {
  match /databases/{database}/documents {
    // Allow public read access, but only content owners can write
    match /some_collection/{document} {
      // Allow public reads
      allow read: if true
      // Allow creation if the current user owns the new document
      allow create: if request.auth.uid == request.resource.data.author_uid;
      // Allow updates by the owner, and prevent change of ownership
      allow update: if request.auth.uid == request.resource.data.author_uid
                    && request.auth.uid == resource.data.author_uid;
      // Allow deletion if the current user owns the existing document
      allow delete: if request.auth.uid == resource.data.author_uid;
    }
  }
}
  

任何通過驗證的使用者都能存取

有時,Cloud Firestore Security Rules 會檢查使用者是否已登入,但不會根據該驗證進一步限制存取權。如果其中一項規則包含 auth != null,請確認您是否要讓所有登入的使用者存取資料。

不建議:任何登入的使用者都可讀取及寫入整個資料庫。
service cloud.firestore {
  match /databases/{database}/documents {
    match /some_collection/{document} {
      allow read, write: if request.auth != null;
    }
  }
}
解決方案:使用安全性條件限縮存取權。

檢查驗證時,您也可以使用其中一個驗證屬性,進一步限制特定使用者對特定資料集的存取權。進一步瞭解如何新增安全性條件和角色型存取權。

角色存取權

service cloud.firestore {
  match /databases/{database}/documents {
    // Assign roles to all users and refine access based on user roles
    match /some_collection/{document} {
     allow read: if request.auth != null && get(/databases/$(database)/documents/users/$(request.auth.uid)).data.role == "Reader"
     allow write: if request.auth != null && get(/databases/$(database)/documents/users/$(request.auth.uid)).data.role == "Writer"

     // Note: Checking for roles in your database using `get` (as in the code
     // above) or `exists` carry standard charges for read operations.
    }
  }
}

以屬性為準的存取權

// Give each user in your database a particular attribute
// and set it to true/false
// Then, use that attribute to grant access to subsets of data
// For example, an "admin" attribute set
// to "true" grants write access to data

service cloud.firestore {
  match /databases/{database}/documents {
    match /collection/{document} {
      allow write: if get(/databases/$(database)/documents/users/$(request.auth.uid)).data.admin == true;
      allow read: true;
    }
  }
}
  

混合公開和私人存取權

service cloud.firestore {
  match /databases/{database}/documents {
    // Allow public read access, but only content owners can write
    match /some_collection/{document} {
      allow read: if true
      allow write: if request.auth.uid == request.resource.data.author_uid
    }
  }
}
  

未驗證電子郵件地址的存取權

有時,Cloud Firestore Security Rules 會檢查使用者的電子郵件是否屬於特定網域。雖然這通常是良好的做法,但電子郵件不一定會在登入時驗證,直到使用者收到驗證電子郵件並執行額外步驟為止。請務必驗證電子郵件地址確實屬於該使用者。

不建議:任何使用者都能以任意電子郵件地址登入。
service cloud.firestore {
  match /databases/{database}/documents {
    // Allow access based on email domain
    match /some_collection/{document} {
     allow read: if request.auth != null
                 && request.auth.email.endsWith('@example.com')
    }
  }
}
解決方案:僅限已驗證的電子郵件地址存取。

驗證電子郵件

service cloud.firestore {
  match /databases/{database}/documents {
    // Allow access based on email domain
    match /some_collection/{document} {
     allow read: if request.auth != null
                 && request.auth.email_verified
                 && request.auth.email.endsWith('@example.com')
    }
  }
}

封閉式存取

開發應用程式時,另一種常見做法是鎖定資料。這通常表示您已關閉所有使用者的讀取和寫入權限,如下所示:

// Deny read/write access to all users under any conditions
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if false;
    }
  }
}

Firebase Admin SDK 和 Cloud Functions 仍可存取資料庫。如果您打算將 Cloud Firestore 做為僅限伺服器的後端,並搭配 Firebase Admin SDK 使用,請採用這些規則。雖然這項設定很安全,但您應測試應用程式的用戶端是否能正確擷取資料。

如要進一步瞭解 Cloud Firestore Security Rules 及其運作方式,請參閱「開始使用 Cloud Firestore Security Rules」。

請查看「Cloud Firestore Security Rules」

如要檢查應用程式行為並驗證 Cloud Firestore Security Rules 設定,請使用 Cloud Firestore 模擬器。在部署任何變更之前,請使用 Cloud Firestore 模擬器在本機環境中執行及自動化單元測試。

如要在 Firebase 控制台中快速測試更新後的 Cloud Firestore Security Rules,請使用規則 Playground 工具。

  1. 如要開啟規則模擬工具,請按一下「Rules」(規則) 分頁中的「Rules playground」(規則模擬工具)。
  2. 在「規則測試區」設定中,選取測試選項,包括:
    • 測試讀取或寫入
    • 資料庫中的特定位置 (以路徑表示)
    • 驗證類型:未經驗證、已驗證的匿名使用者,或特定使用者 ID
    • 規則明確參照的文件特定資料 (例如,如果規則要求必須存在特定欄位,才允許寫入)
  3. 按一下「執行」,然後在規則視窗上方的橫幅中查看結果。