建構 Cloud Firestore 安全性規則

Cloud Firestore 安全性規則可讓您控管 資料庫內的集合彈性規則語法可讓您建立 從所有寫入到整個資料庫,到作業皆符合任何條件的規則 針對特定文件。

本指南將說明安全性規則的基本語法和結構。合併 搭配使用這項語法和安全性規則條件 完成規則集

宣告服務和資料庫

Cloud Firestore 安全性規則的開頭一律為下列宣告:

service cloud.firestore {
  match /databases/{database}/documents {
    // ...
  }
}

service cloud.firestore 宣告會將規則的範圍限定為 Cloud Firestore,防止 Cloud Firestore 安全性規則與 Cloud Storage 或其他產品均有規則

match /databases/{database}/documents 宣告會指定規則 符合專案中的任何 Cloud Firestore 資料庫目前每項專案 只有一個名為 (default) 的資料庫。

基本讀取/寫入規則

基本規則包含指定文件路徑的 match 陳述式,以及 讀取指定資料時允許詳細描述的 allow 運算式:

service cloud.firestore {
  match /databases/{database}/documents {

    // Match any document in the 'cities' collection
    match /cities/{city} {
      allow read: if <condition>;
      allow write: if <condition>;
    }
  }
}

所有比對陳述式都應指向文件,而非集合。配對 陳述式可以指向特定文件 (例如 match /cities/SF),或使用萬用字元 指向指定路徑中的任何文件,如 match /cities/{city} 中所示。

在上述範例中,比對陳述式使用 {city} 萬用字元語法。 也就是說,這項規則適用於 cities 集合中的任何文件,例如 /cities/SF/cities/NYC。當比對陳述式中的 allow 運算式出現 city 變數會解析為城市文件名稱。 例如 SFNYC

精細運算

在某些情況下,將 readwrite 細分為更常見的 ID 執行精細作業舉例來說,您的應用程式可能想強制執行 建立文件時以外的條件或者您可能需要 允許單一文件讀取,但拒絕大型查詢。

read 規則可拆分為 getlist,而 write 規則可以 分為 createupdatedelete

service cloud.firestore {
  match /databases/{database}/documents {
    // A read rule can be divided into get and list rules
    match /cities/{city} {
      // Applies to single document read requests
      allow get: if <condition>;

      // Applies to queries and collection read requests
      allow list: if <condition>;
    }

    // A write rule can be divided into create, update, and delete rules
    match /cities/{city} {
      // Applies to writes to nonexistent documents
      allow create: if <condition>;

      // Applies to writes to existing documents
      allow update: if <condition>;

      // Applies to delete operations
      allow delete: if <condition>;
    }
  }
}

階層式資料

Cloud Firestore 中的資料會按照文件集合整理 文件可能會透過子集合擴充階層。請務必 瞭解安全性規則與階層資料的互動方式。

假設 cities 集合中的每份文件都包含 landmarks 個子集合。安全性規則只會套用到相符的路徑,因此 cities 集合中定義的存取權控管設定不適用於 landmarks 個子集合。請改為編寫明確規則來控管存取權 子集合:

service cloud.firestore {
  match /databases/{database}/documents {
    match /cities/{city} {
      allow read, write: if <condition>;

        // Explicitly define rules for the 'landmarks' subcollection
        match /landmarks/{landmark} {
          allow read, write: if <condition>;
        }
    }
  }
}

建立 match 陳述式的巢狀結構時,內部 match 陳述式的路徑一律為 相對於外部 match 陳述式的路徑。下列規則集 因此相同:

service cloud.firestore {
  match /databases/{database}/documents {
    match /cities/{city} {
      match /landmarks/{landmark} {
        allow read, write: if <condition>;
      }
    }
  }
}
service cloud.firestore {
  match /databases/{database}/documents {
    match /cities/{city}/landmarks/{landmark} {
      allow read, write: if <condition>;
    }
  }
}

遞迴萬用字元

如要將規則套用至任意深層階層,請使用 遞迴萬用字元語法 {name=**}。例如:

service cloud.firestore {
  match /databases/{database}/documents {
    // Matches any document in the cities collection as well as any document
    // in a subcollection.
    match /cities/{document=**} {
      allow read, write: if <condition>;
    }
  }
}

使用遞迴萬用字元語法時,萬用字元變數會包含 整個相符路徑片段 (即使文件位於深層巢狀結構中) 子集合。例如,上述規則 位於 /cities/SF/landmarks/coit_tower 的文件,以及 document 變數為 SF/landmarks/coit_tower

不過請注意,遞迴萬用字元的行為取決於規則 版本。

版本 1

根據預設,安全性規則會使用第 1 版。在第 1 版中,遞迴萬用字元 比對一或多個路徑項目它們與空白路徑不相符 match /cities/{city}/{document=**} 與子集合中的文件相符,但 不在 cities 集合中,但與 match /cities/{document=**} 相符 cities 集合和子集合中的文件。

遞迴萬用字元必須放在比對陳述式的結尾。

版本 2

在第 2 版的安全性規則中,遞迴萬用字元符合零或多個路徑 項目。match/cities/{city}/{document=**} 與任一份文件相符 子集合和 cities 集合中的文件。

您必須在頂端新增 rules_version = '2';,才能採用第 2 版 安全性規則:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    // Matches any document in the cities collection as well as any document
    // in a subcollection.
    match /cities/{city}/{document=**} {
      allow read, write: if <condition>;
    }
  }
}

每個比對陳述式最多只能有一個遞迴萬用字元,但在版本中則 2,您可以將這個萬用字元放在比對陳述式中的任何位置。例如:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    // Matches any document in the songs collection group
    match /{path=**}/songs/{song} {
      allow read, write: if <condition>;
    }
  }
}

如果您使用集合群組查詢,請務必使用 請參閱「保護集合群組查詢」一節。

重疊的比對聲明

文件可能會比對多個 match 陳述式。在 如果有多個 allow 運算式符合同一個要求,則允許存取 如果「任一」條件為 true

service cloud.firestore {
  match /databases/{database}/documents {
    // Matches any document in the 'cities' collection.
    match /cities/{city} {
      allow read, write: if false;
    }

    // Matches any document in the 'cities' collection or subcollections.
    match /cities/{document=**} {
      allow read, write: if true;
    }
  }
}

在上述範例中,對於 cities 集合的所有讀取和寫入作業都將 但第二項規則一律為 true,但第 1 項 則一律為 false

安全性規則限制

處理安全性規則時,請注意以下限制:

限制 說明
每項要求的 exists()get()getAfter() 呼叫數量上限
  • 單一文件要求和查詢要求的上限為 10 項。
  • 多文件讀取作業、交易和批次寫入作業的上限為 20 項。上述限制 (10 項呼叫) 同樣適用於各項作業。

    舉例來說,假設您建立的批次寫入要求含有 3 項寫入作業,而您的安全性規則會使用 2 項文件存取呼叫來驗證各項寫入作業。此時,各項寫入作業會使用 2 項存取呼叫 (上限為 10 項),批次寫入要求則會使用 6 項存取呼叫 (上限為 20 項)。

超過任一項限制都會引發權限遭拒的錯誤。

系統可能會快取部分的文件存取呼叫,已快取的呼叫不會計入限制中。

巢狀 match 陳述式深度上限 10
一組巢狀 match 陳述式中允許的路徑長度上限 (以路徑區段為單位) 100
一組巢狀 match 陳述式中允許的路徑擷取變數數量上限 20
函式呼叫深度上限 20
函式引數數量上限 7
每個函式的 let 變數繫結上限 10
遞迴或循環函式呼叫的數量上限 0 (不允許)
每個要求中經評估的運算式數量上限 1,000 個
規則集的大小上限 規則集必須遵守以下兩項大小限制:
  • 規則集文字來源的大小限制為 256 KB 透過 Firebase 控制台或 CLI 發布 firebase deploy
  • 針對結果產生的已編譯規則集大小,限制為 250 KB Firebase 處理來源並在 後端。

後續步驟