CloudStorageのFirebaseセキュリティルールを使用すると、CloudStorageバケットに保存されているオブジェクトへのアクセスを制御できます。柔軟なルール構文を使用すると、Cloud Storageバケットへのすべての書き込みから特定のファイルへの操作まで、あらゆる操作を制御するルールを作成できます。
このガイドでは、完全なルールセットを作成するためのCloudStorageセキュリティルールの基本的な構文と構造について説明します。
サービスとデータベースの宣言
クラウドストレージのFirebaseセキュリティルールは、常に次の宣言で始まります。
service firebase.storage {
// ...
}
service firebase.storage
宣言は、ルールをCloud Storageにスコープし、CloudStorageセキュリティルールとCloudFirestoreなどの他の製品のルールとの競合を防ぎます。
基本的な読み取り/書き込みルール
基本的なルールは、Cloud Storageバケットを識別するmatch
ステートメント、ファイル名を指定するmatchステートメント、および指定されたデータの読み取りが許可される場合の詳細を示すallow
式で構成されます。 allow
式は、関連するアクセス方法(読み取り、書き込みなど)、およびアクセスが許可または拒否される条件を指定します。
デフォルトのルールセットでは、最初のmatch
ステートメントは{bucket}
ワイルドカード式を使用して、ルールがプロジェクト内のすべてのバケットに適用されることを示します。ワイルドカード一致のアイデアについては、次のセクションで詳しく説明します。
service firebase.storage {
// The {bucket} wildcard indicates we match files in all Cloud Storage buckets
match /b/{bucket}/o {
// Match filename
match /filename {
allow read: if <condition>;
allow write: if <condition>;
}
}
}
すべてのmatchステートメントはファイルを指します。 matchステートメントは、 match /images/profilePhoto.png
のように、特定のファイルを指すことができます。
ワイルドカードを一致させる
単一のファイルを指すことに加えて、Rulesはワイルドカードを使用して、 match /images/{imageId}
のように、名前にスラッシュを含む特定の文字列プレフィックスが含まれる任意のファイルを指すことができます。
上記の例では、matchステートメントは{imageId}
ワイルドカード構文を使用しています。これは、/ images /images/profilePhoto.png
や/images/croppedProfilePhoto.png
など、名前の先頭に/images/
が付いているすべてのファイルにルールが適用されることを意味します。 matchステートメントのallow
式が評価されると、 imageId
変数はprofilePhoto.png
やcroppedProfilePhoto.png
などの画像ファイル名に解決されます。
match
内からワイルドカード変数を参照して、ファイル名またはパス認証を提供できます。
// Another way to restrict the name of a file
match /images/{imageId} {
allow read: if imageId == "profilePhoto.png";
}
階層データ
前に述べたように、CloudStorageバケット内には階層構造はありません。ただし、ファイル名の規則(多くの場合、ファイル名にスラッシュが含まれる規則)を使用することで、ネストされた一連のディレクトリとサブディレクトリのように見える構造を模倣できます。 Firebaseセキュリティルールがこれらのファイル名とどのように相互作用するかを理解することが重要です。
すべて/images/
語幹で始まる名前のファイルのセットの状況を考えてみましょう。 Firebaseセキュリティルールは一致したファイル名にのみ適用されるため、 /images/
ステムで定義されたアクセス制御は/mp3s/
ステムには適用されません。代わりに、さまざまなファイル名パターンに一致する明示的なルールを記述します。
service firebase.storage {
match /b/{bucket}/o {
match /images/{imageId} {
allow read, write: if <condition>;
}
// Explicitly define rules for the 'mp3s' pattern
match /mp3s/{mp3Id} {
allow read, write: if <condition>;
}
}
}
match
ステートメントをネストする場合、内側のmatch
ステートメントのパスは常に外側のmatch
ステートメントのパスに追加されます。したがって、次の2つのルールセットは同等です。
service firebase.storage {
match /b/{bucket}/o {
match /images {
// Exact match for "images/profilePhoto.png"
match /profilePhoto.png {
allow write: if <condition>;
}
}
}
}
service firebase.storage {
match /b/{bucket}/o {
// Exact match for "images/profilePhoto.png"
match /images/profilePhoto.png {
allow write: if <condition>;
}
}
}
再帰一致ワイルドカード
ファイル名の末尾の文字列と一致して返すワイルドカードに加えて、 {path=**}
のようにワイルドカード名に=**
を追加することで、より複雑な一致を実現する複数セグメントのワイルドカードを宣言できます。
// Partial match for files that start with "images"
match /images {
// Exact match for "images/**"
// e.g. images/users/user:12345/profilePhoto.png is matched
// images/profilePhoto.png is also matched!
match /{allImages=**} {
// This rule matches one or more path segments (**)
// allImages is a path that contains all segments matched
allow read: if <other_condition>;
}
}
複数のルールがファイルに一致する場合、結果はすべてのルール評価の結果のOR
になります。つまり、ファイルが評価に一致するルールがtrue
場合、結果はtrue
になります。
上記のルールでは、ファイル "images / profilePhoto.png"は、 condition
またはother_condition
のいずれかがtrueと評価された場合に読み取ることができますが、ファイル "images / users / user:12345 / profilePhoto.png"は、 other_condition
の結果のみの対象となります。 。
Cloud Storageのセキュリティルールはカスケードされません。ルールは、リクエストパスが指定されたルールのパスと一致する場合にのみ評価されます。
バージョン1
Firebaseセキュリティルールはデフォルトでバージョン1を使用します。バージョン1では、再帰的なワイルドカードは、0個以上の要素ではなく、1つ以上のファイル名要素に一致します。したがって、 match /images/{filenamePrefixWildcard}/{imageFilename=**}
は、/ images / profilePics / profile.pngのようなファイル名と一致しますが、/ images /badge.pngとは一致しません。代わりに/images/{imagePrefixorFilename=**}
を使用してください。
再帰的なワイルドカードは、一致ステートメントの最後に配置する必要があります。
より強力な機能のために、バージョン2を使用することをお勧めします。
バージョン2
Firebaseセキュリティルールのバージョン2では、再帰的なワイルドカードは0個以上のパスアイテムと一致します。したがって、 /images/{filenamePrefixWildcard}/{imageFilename=**}
はファイル名/images/profilePics/profile.pngおよび/images/badge.pngと一致します。
rules_version = '2';
を追加して、バージョン2にオプトインする必要があります。セキュリティルールの上部:
rules_version = '2';
service cloud.storage {
match /b/{bucket}/o {
...
}
}
一致ステートメントごとに最大で1つの再帰ワイルドカードを使用できますが、バージョン2では、このワイルドカードを一致ステートメントの任意の場所に配置できます。例えば:
rules_version = '2';
service firebase.storage {
match /b/{bucket}/o {
// Matches any file in a songs "subdirectory" under the
// top level of your Cloud Storage bucket.
match /{prefixSegment=**}/songs/{mp3filenames} {
allow read, write: if <condition>;
}
}
}
きめ細かい操作
状況によっては、 read
とwrite
をより詳細な操作に分割すると便利です。たとえば、アプリは、ファイルの削除とは異なる条件をファイルの作成に適用したい場合があります。
read
操作は、 get
とlist
に分けることができます。
write
ルールは、 create
、 update
、およびdelete
に分割できます。
service firebase.storage { match /b/{bucket}/o { // A read rule can be divided into read and list rules match /images/{imageId} { // Applies to single file read requests allow get: if <condition>; // Applies to list and listAll requests (Rules Version 2) allow list: if <condition>; // A write rule can be divided into create, update, and delete rules match /images/{imageId} { // Applies to writes to file contents allow create: if <condition>; // Applies to updates to (pre-existing) file metadata allow update: if <condition>; // Applies to delete operations allow delete: if <condition>; } } } }
重複する一致ステートメント
ファイル名が複数のmatch
ステートメントと一致する可能性があります。複数のallow
式がリクエストに一致する場合、条件のいずれかがtrue
であればアクセスが許可されます。
service firebase.storage {
match b/{bucket}/o {
// Matches any filename containing string '/images/'.
match /images/{imageId} {
allow read, write: if false;
}
// Matches all filenames containing string `/images/`
match /images/{imageId=**} {
allow read, write: if true;
}
}
}
上記の例では、最初のルールが常にfalse
であっても、2番目のルールは常にtrue
であるため、ファイル名の任意の場所に文字列/images/
が含まれるファイルへのすべての読み取りと書き込みが許可されます。
ルールはフィルターではありません
データを保護してファイル操作の実行を開始したら、セキュリティルールはフィルターではないことに注意してください。ファイル名パターンに一致する一連のファイルに対して操作を実行することはできず、CloudStorageが現在のクライアントがアクセスする権限を持っているファイルにのみアクセスすることを期待できます。
たとえば、次のセキュリティルールを考えてみましょう。
service firebase.storage {
match /b/{bucket}/o {
// Allow the client to read files with contentType 'image/png'
match /aFileNamePrefix/{aFileName} {
allow read: if resource.contentType == 'image/png';
}
}
}
拒否: contentType
がimage/png
でないファイルを結果セットに含めることができるため、このルールは次の要求を拒否します:
ウェブ
filesRef = storage.ref().child("aFilenamePrefix"); filesRef.listAll() .then(function(result) { console.log("Success: ", result.items); }) });
Cloud Storageのルールセキュリティルールは、潜在的な結果に対して各クエリを評価し、クライアントが読み取る権限を持たないファイルを返す可能性がある場合、リクエストを失敗させます。アクセス要求は、ルールで設定された制約に従う必要があります。
次のステップ
クラウドストレージのFirebaseセキュリティルールについての理解を深めることができます。
ルール言語の次の主要な概念である動的条件について学びます。これにより、ルールでユーザーの承認を確認したり、既存のデータと受信データを比較したり、受信データを検証したりできます。
一般的なセキュリティのユースケースと、それらに対処するFirebaseセキュリティルールの定義を確認します。
CloudStorageに固有のFirebaseセキュリティルールのユースケースを調べることができます。