Cloud Storage 用の Firebase セキュリティ ルールを理解する
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
これまで、セキュリティはアプリ開発において最も複雑な部分の 1 つでした。ほとんどのアプリでは、認証(ユーザーは誰なのか)と承認(ユーザーは何を行えるのか)を処理するサーバーの構築と実行をデベロッパーが行う必要があります。認証と承認は設定が難しく、特に、正確に設定することはとても困難ですが、プロジェクトの成功に欠かせない重要な要素です。
Firebase Authentication によってユーザーの認証が簡単になるように、Cloud Storage 用の Firebase Security Rules によってユーザーの承認とリクエストの検証が簡単になります。Cloud Storage Security Rules で複雑な構造に対応できるのは、パスベースのアクセス許可を指定できるためです。わずか数行のコードで、Cloud Storage リクエストを特定のユーザーに限定する承認ルールや、アップロード サイズを制限する承認ルールを作成できます。
Firebase Realtime Database には、Firebase Realtime Database Security Rules という類似した機能があります。
認証
ユーザーが誰なのかを把握することは、アプリケーション構築の重要な部分です。Firebase Authentication には、簡単に操作でき、しかも安全なクライアント側専用の認証ソリューションがあります。Cloud Storage の Firebase Security Rules は、ユーザーベースのセキュリティのために Firebase Authentication と関連付けられています。ユーザーが Firebase Authentication で認証されると、Cloud Storage Security Rules の request.auth
変数が、そのユーザーに固有の ID(request.auth.uid
)とトークン内の他のすべてのユーザー情報(request.auth.token
)を含むオブジェクトになります。ユーザーが認証されていない場合、request.auth
は null
になります。これにより、データアクセスをユーザー単位で安全に制御できます。詳しくは、認証のセクションをご覧ください。
承認
ユーザーの識別はセキュリティの一部にすぎません。ユーザーが誰なのか把握できたら、Cloud Storage 内のファイルに対するユーザーのアクセス権を管理する方法が必要になります。
Cloud Storage を使用すると、Google のサーバー上にあるファイルやパスごとに認可ルールを指定して、アプリ内のファイルへのアクセスを決定できます。たとえば、デフォルトの Cloud Storage Security Rules では、すべてのファイルに対して read
または write
オペレーションを行うために、常に Firebase Authentication が必要です。
service firebase.storage {
match /b/{bucket}/o {
match /someFolder/{fileName} {
allow read, write: if request.auth != null;
}
}
}
セキュリティ ルールは、Firebase コンソールで Firebase アプリを選択し、[Storage] セクションの [Rules
] タブを表示して編集できます。
データの検証
Cloud Storage 用の Firebase Security Rules は、ファイル名とパスの検証、contentType
や size
などのファイル メタデータ プロパティの検証といったデータ検証にも使用できます。
service firebase.storage {
match /b/{bucket}/o {
match /images/{imageId} {
// Only allow uploads of any image file that's less than 5MB
allow write: if request.resource.size < 5 * 1024 * 1024
&& request.resource.contentType.matches('image/.*');
}
}
}
次のステップ
特に記載のない限り、このページのコンテンツはクリエイティブ・コモンズの表示 4.0 ライセンスにより使用許諾されます。コードサンプルは Apache 2.0 ライセンスにより使用許諾されます。詳しくは、Google Developers サイトのポリシーをご覧ください。Java は Oracle および関連会社の登録商標です。
最終更新日 2025-08-24 UTC。
[null,null,["最終更新日 2025-08-24 UTC。"],[],[],null,["\u003cbr /\u003e\n\nTraditionally, security has been one of the most complex parts of app\ndevelopment. In most applications, developers must build and run a server that\nhandles authentication (who a user is) and authorization (what a user can do).\nAuthentication and authorization are hard to set up, harder to get right, and\ncritical to the success of your product.\n\nSimilar to how Firebase Authentication makes it easy for you to authenticate your\nusers, Firebase Security Rules for Cloud Storage makes it easy for you to authorize users\nand validate requests. Cloud Storage Security Rules manage the complexity for you by\nallowing you to specify path based permissions. In just a few lines of code, you\ncan write authorization rules that restrict Cloud Storage requests to a\ncertain user or limit the size of an upload.\n| **Note:** If you use Google App Engine and have a default Cloud Storage bucket with a name format of `*.appspot.com`, you may need to consider [how your security rules impact access to App Engine files](/docs/storage/gcp-integration#security-rules-and-app-engine-files).\n\nThe Firebase Realtime Database has a similar feature, called\n[Firebase Realtime Database Security Rules](/docs/database/security)\n\nAuthentication\n\nKnowing who your users are is an important part of building an application, and\nFirebase Authentication provides an easy to use, secure, client side only solution\nto authentication. Firebase Security Rules for Cloud Storage ties in to Firebase Authentication\nfor user based security. When a user is authenticated with Firebase Authentication,\nthe `request.auth` variable in Cloud Storage Security Rules becomes an object that\ncontains the user's unique ID (`request.auth.uid`) and all other user\ninformation in the token (`request.auth.token`). When the user is not\nauthenticated, `request.auth` is `null`. This allows you to securely control\ndata access on a per-user basis. You can learn more in the\n[Authentication](/docs/storage/security/rules-conditions#authentication) section.\n\nAuthorization\n\nIdentifying your user is only part of security. Once you know who they are, you\nneed a way to control their access to files in Cloud Storage.\n\nCloud Storage lets you specify per file and per path authorization\nrules that live on our servers and determine access to the files in your app.\nFor example, the default Cloud Storage Security Rules require Firebase Authentication in\norder to perform any `read` or `write` operations on all files: \n\n```css+lasso\nservice firebase.storage {\n match /b/{bucket}/o {\n match /someFolder/{fileName} {\n allow read, write: if request.auth != null;\n }\n }\n}\n```\n\nYou can edit these rules by selecting a Firebase app in the [Firebase console](//console.firebase.google.com/)\nand viewing the `Rules` tab of the Storage section.\n\nData Validation\n\nFirebase Security Rules for Cloud Storage can also be used for data validation, including\nvalidating file name and path as well as file metadata properties such as\n`contentType` and `size`. \n\n```gdscript\nservice firebase.storage {\n match /b/{bucket}/o {\n match /images/{imageId} {\n // Only allow uploads of any image file that's less than 5MB\n allow write: if request.resource.size \u003c 5 * 1024 * 1024\n && request.resource.contentType.matches('image/.*');\n }\n }\n}\n```\n\nNext steps\n\n- [Get started](/docs/storage/security/get-started) planning rules development\n for your Cloud Storage buckets.\n\n- Learn more about [securing your data](/docs/storage/security/core-syntax)\n using security rules."]]