Cloud Storage の Firebase セキュリティ ルールを使用すると、Cloud Storage バケットに保存されているオブジェクトへのアクセスを制御できます。柔軟なルール構文により、Cloud Storage バケットへのすべての書き込みから特定のファイルに対する操作まで、あらゆる操作を制御するルールを作成できます。
このガイドでは、完全なルールセットを作成するための Cloud Storage セキュリティ ルールの基本的な構文と構造について説明します。
サービスとデータベースの宣言
Cloud Storage の Firebase セキュリティ ルールは、常に次の宣言で始まります。
service firebase.storage {
// ...
}
service firebase.storage
宣言は、ルールの範囲を Cloud Storage に限定し、Cloud Storage セキュリティ ルールと Cloud Firestore などの他のプロダクトのルールとの間の競合を防ぎます。
基本的な読み取り/書き込みルール
基本ルールは、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
のように、特定のファイルを指すことができます。
ワイルドカードに一致
単一のファイルを指すだけでなく、ルールはワイルドカードを使用して、 match /images/{imageId}
のように、スラッシュを含む、名前に特定の文字列プレフィックスを持つ任意のファイルを指すことができます。
上記の例では、match ステートメントで{imageId}
ワイルドカード構文を使用しています。これは、 /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";
}
階層データ
前に述べたように、Cloud Storage バケット内には階層構造はありません。しかし、多くの場合、ファイル名にスラッシュが含まれるファイル命名規則を使用することで、ネストされた一連のディレクトリとサブディレクトリのような構造を模倣できます。 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=**}
を使用してください。
再帰ワイルドカードは、match ステートメントの最後にある必要があります。
より強力な機能を利用するには、バージョン 2 を使用することをお勧めします。
バージョン 2
バージョン 2 の Firebase セキュリティ ルールでは、再帰ワイルドカードは 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 {
...
}
}
match ステートメントごとに再帰ワイルドカードを最大 1 つ使用できますが、バージョン 2 では、このワイルドカードを match ステートメントの任意の場所に配置できます。例えば:
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 ステートメントの重複
ファイル名が複数のmatch
ステートメントに一致する可能性があります。複数のallow
式がリクエストに一致する場合、いずれかの条件がtrue
であればアクセスが許可されます。
service firebase.storage {
match b/{bucket}/o {
// Matches file names directly inside of '/images/'.
match /images/{imageId} {
allow read, write: if false;
}
// Matches file names anywhere under `/images/`
match /images/{imageId=**} {
allow read, write: if true;
}
}
}
上記の例では、最初のルールがfalse
であっても、2 番目のルールが常にtrue
であるため、名前が/images/
で始まるファイルへのすべての読み取りと書き込みが許可されます。
ルールはフィルターではありません
データを保護し、ファイル操作の実行を開始したら、セキュリティ ルールはフィルターではないことに注意してください。ファイル名パターンに一致する一連のファイルに対して操作を実行することはできず、現在のクライアントがアクセス権を持っているファイルのみに Cloud Storage がアクセスすることを期待できます。
たとえば、次のセキュリティ ルールを考えてみましょう。
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';
}
}
}
Denied : contentType
がimage/png
ではないファイルが結果セットに含まれる可能性があるため、このルールは次の要求を拒否します。
ウェブ
filesRef = storage.ref().child("aFilenamePrefix"); filesRef.listAll() .then(function(result) { console.log("Success: ", result.items); }) });
Cloud Storage セキュリティ ルールのルールは、潜在的な結果に対して各クエリを評価し、クライアントが読み取り権限を持っていないファイルを返す可能性がある場合、リクエストは失敗します。アクセス要求は、ルールによって設定された制約に従う必要があります。
次のステップ
Cloud Storage の Firebase セキュリティ ルールの理解を深めることができます。
ルール言語の次の主要な概念である動的条件について学びます。これにより、ルールでユーザーの承認を確認したり、既存のデータと受信データを比較したり、受信データを検証したりできます。
一般的なセキュリティ ユースケースと、それらに対処する Firebase セキュリティ ルールの定義を確認します。
Cloud Storage に固有の Firebase セキュリティ ルールのユースケースを調べることができます。