Cloud Storage for Firebase 提供宣告式路徑型安全性模型 呼叫 Cloud Storage 的 Firebase Security Rules, 確保檔案安全無虞
瞭解規則
Cloud Storage 的 Firebase Security Rules 可用於判斷誰具備讀取和寫入權限
可存取儲存在 Cloud Storage 中的檔案,以及
以及所含中繼資料規則的基本類型是 allow
規則,如果選擇性指定,可允許 read
和 write
要求
條件指定,例如:
// If no condition is specified, the rule evaluates to true allow read; // Rules can optionally specify a condition allow write: if <condition>; // Rules can also specify multiple request methods allow read, write: if <condition>;
比對路徑
Cloud Storage Security Rules match
存取用來存取檔案的檔案路徑
Cloud Storage。規則可以match
確切路徑或萬用字元路徑,以及
也可以建立巢狀結構如果沒有比對規則允許要求方法,或
條件評估為 false
,系統就會拒絕要求。
完全相符的結果
// Exact match for "images/profilePhoto.png" match /images/profilePhoto.png { allow write: if <condition>; } // Exact match for "images/croppedProfilePhoto.png" match /images/croppedProfilePhoto.png { allow write: if <other_condition>; }
巢狀比對
// Partial match for files that start with "images" match /images { // Exact match for "images/profilePhoto.png" match /profilePhoto.png { allow write: if <condition>; } // Exact match for "images/croppedProfilePhoto.png" match /croppedProfilePhoto.png { allow write: if <other_condition>; } }
萬用字元比對
規則也可以使用萬用字元來 match
模式。萬用字元是
具名變數,代表單一字串,例如
profilePhoto.png
,或是多個路徑區隔,例如
images/profilePhoto.png
。
建立萬用字元的方法是在萬用字元名稱前後加上大括號,例如:
{string}
。如要宣告多個片段萬用字元,請在=**
萬用字元名稱,例如 {path=**}
:
// Partial match for files that start with "images" match /images { // Exact match for "images/*" // e.g. images/profilePhoto.png is matched match /{imageId} { // This rule only matches a single path segment (*) // imageId is a string that contains the specific segment matched allow read: if <condition>; } // 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
。
您可以在 match
提供檔案中參照萬用字元變數
名稱或路徑授權:
// Another way to restrict the name of a file match /images/{imageId} { allow read: if imageId == "profilePhoto.png"; }
Cloud Storage Security Rules 不會串聯,且系統只會評估 要求路徑與包含指定規則的路徑相符。
要求評估
上傳、下載、中繼資料變更和刪除等作業,
request
已匯款給 Cloud Storage。request
變數包含
執行要求的檔案路徑、要求執行要求的時間
如果要求是寫入,則新的 resource
值。HTTP 標頭
以及驗證狀態
request
物件也會包含使用者的專屬 ID,以及
request.auth
物件中的 Firebase Authentication 酬載,
如要進一步瞭解,請參閱「使用者層級安全性」
一節。
以下是 request
物件中的完整屬性清單:
屬性 | 類型 | 說明 |
---|---|---|
auth |
map<string, string> | 使用者登入時,請提供 uid 、使用者的專屬 ID,以及
token ,Firebase Authentication JWT 憑證附加資訊的對應。否則,
null 。 |
params |
map<string, string> | 包含要求查詢參數的地圖。 |
path |
路徑 | path 代表要求目前所在的路徑
執行的所有工作 |
resource |
map<string, string> | 新資源值,只會顯示在 write 要求中。
|
time |
時間戳記 | 代表要求評估要求的伺服器時間的時間戳記。 |
資源評估
評估規則時,建議您評估檔案的中繼資料 上傳、下載、修改或刪除這可讓您 複雜且功能強大的規則,例如僅允許符合特定條件的檔案 要上傳的內容類型,或只上傳大於特定大小的檔案 已刪除。
Cloud Storage 的 Firebase Security Rules 提供 resource
中的檔案中繼資料
物件,其中包含出現在
Cloud Storage 物件。如要查看這些屬性,請前往 read
或
用於確保資料完整性的 write
要求。
針對 write
要求 (例如上傳、中繼資料更新與刪除),
以及 resource
物件,其中包含檔案的檔案中繼資料
目前存在於要求路徑上 的物件,您也可以使用
request.resource
物件,其中包含要匯入的檔案中繼資料子集
但若允許寫入。您可以運用這兩個值來確保
完整性或強制執行應用程式限制,例如檔案類型或大小。
以下是 resource
物件中的完整屬性清單:
屬性 | 類型 | 說明 |
---|---|---|
name |
字串 | 物件全名 |
bucket |
字串 | 這個物件所在值區名稱。 |
generation |
int | GCS 產生這個物件的物件 |
metageneration |
int | GCS 這個物件的物件中繼產生。 |
size |
int | 物件大小 (以位元組為單位)。 |
timeCreated |
時間戳記 | 代表物件建立時間的時間戳記。 |
updated |
時間戳記 | 代表物件上次更新時間的時間戳記。 |
md5Hash |
字串 | 物件的 MD5 雜湊。 |
crc32c |
字串 | 物件的 crc32c 雜湊。 |
etag |
字串 | 與這個物件相關聯的 ETag。 |
contentDisposition |
字串 | 與此物件相關聯的內容配置。 |
contentEncoding |
字串 | 與此物件相關聯的內容編碼。 |
contentLanguage |
字串 | 與這個物件相關聯的內容語言。 |
contentType |
字串 | 與這個物件相關聯的內容類型。 |
metadata |
map<string, string> | 開發人員指定的其他自訂中繼資料鍵/值組合。 |
request.resource
包含以上所有項目,但 generation
除外。
metageneration
、etag
、timeCreated
和updated
。
完整範例
總結來說,您可以建立完整的圖片規則範例 儲存空間解決方案:
service firebase.storage { match /b/{bucket}/o { match /images { // Cascade read to any image type at any path match /{allImages=**} { allow read; } // Allow write files to the path "images/*", subject to the constraints: // 1) File is less than 5MB // 2) Content type is an image // 3) Uploaded content type matches existing content type (if it exists) // 4) File name (stored in imageId wildcard variable) is less than 32 characters match /{imageId} { allow write: if request.resource.size < 5 * 1024 * 1024 && request.resource.contentType.matches('image/.*') && (resource == null || request.resource.contentType == resource.contentType) && imageId.size() < 32 } } } }
現在,讓我們整合 Firebase Authentication,在 「使用者安全性」部分。