Firebase 即時資料庫安全性規則可讓您控制對資料庫中儲存的資料的存取。靈活的規則語法可讓您建立符合任何內容的規則,從資料庫的所有寫入到單一節點上的操作。
即時資料庫安全性規則是資料庫的聲明性配置。這意味著規則是與產品邏輯分開定義的。這有很多優點:客戶端不負責執行安全性,有缺陷的實現不會損害您的數據,也許最重要的是,不需要中間裁判(例如伺服器)來保護資料免受外界侵害。
本主題介紹用於建立完整規則集的即時資料庫安全規則的基本語法和結構。
建立您的安全規則
即時資料庫安全規則由 JSON 文件中包含的類似 JavaScript 的表達式組成。規則的結構應遵循資料庫中儲存的資料的結構。
基本規則標識一組要保護的節點、涉及的存取方法(例如讀取、寫入)以及允許或拒絕存取的條件。在下面的範例中,我們的條件將是簡單的true
和false
語句,但在下一個主題中,我們將介紹更動態的方式來表達條件。
因此,例如,如果我們嘗試保護parent_node
下的child_node
,則要遵循的一般語法是:
{ "rules": { "parent_node": { "child_node": { ".read": <condition>, ".write": <condition>, ".validate": <condition>, } } } }
讓我們應用這個模式。例如,假設您正在追蹤訊息清單並擁有如下所示的資料:
{ "messages": { "message0": { "content": "Hello", "timestamp": 1405704370369 }, "message1": { "content": "Goodbye", "timestamp": 1405704395231 }, ... } }
您的規則應該以類似的方式建構。這是一組可能對此資料結構有意義的唯讀安全性規則。此範例說明了我們如何指定應用規則的資料庫節點以及在這些節點評估規則的條件。
{ "rules": { // For requests to access the 'messages' node... "messages": { // ...and the individual wildcarded 'message' nodes beneath // (we'll cover wildcarding variables more a bit later).... "$message": { // For each message, allow a read operation if <condition>. In this // case, we specify our condition as "true", so read access is always granted. ".read": "true", // For read-only behavior, we specify that for write operations, our // condition is false. ".write": "false" } } } }
基本規則操作
根據資料執行的操作類型,可以使用三種類型的規則來實作安全性: .write
、 .read
和.validate
。以下是其目的的快速摘要:
規則類型 | |
---|---|
。讀 | 描述是否以及何時允許使用者讀取資料。 |
。寫 | 描述是否以及何時允許寫入資料。 |
。證實 | 定義格式正確的值的外觀、是否具有子屬性以及資料類型。 |
通配符捕獲變數
所有規則語句都指向節點。語句可以指向特定節點或使用$
通配符捕捉變數來指向層次結構層級的節點集。使用這些擷取變數來儲存節點鍵的值,以便在後續規則語句中使用。這種技術可以讓您編寫更複雜的規則條件,我們將在下一個主題中更詳細地介紹這一點。
{ "rules": { "rooms": { // this rule applies to any child of /rooms/, the key for each room id // is stored inside $room_id variable for reference "$room_id": { "topic": { // the room's topic can be changed if the room id has "public" in it ".write": "$room_id.contains('public')" } } } } }
動態$
變數也可以與常數路徑名並行使用。在此範例中,我們使用$other
變數來宣告.validate
規則,以確保小widget
除了title
和color
之外沒有子項目。任何會導致建立額外子項的寫入都會失敗。
{ "rules": { "widget": { // a widget can have a title or color attribute "title": { ".validate": true }, "color": { ".validate": true }, // but no other child paths are allowed // in this case, $other means any key excluding "title" and "color" "$other": { ".validate": false } } } }
讀寫規則級聯
.read
和.write
規則從上到下工作,較淺的規則覆蓋較深的規則。如果規則授予特定路徑的讀取或寫入權限,那麼它也會授予對其下所有子節點的存取權限。考慮以下結構:
{ "rules": { "foo": { // allows read to /foo/* ".read": "data.child('baz').val() === true", "bar": { /* ignored, since read was allowed already */ ".read": false } } } }
此安全結構允許在/foo/
包含值為true
子baz
時讀取/bar/
。 /foo/bar/
下的".read": false
規則在這裡不起作用,因為子路徑無法撤銷存取。
雖然它看起來可能不是很直觀,但這是規則語言的強大部分,並且允許以最少的努力實現非常複雜的訪問權限。當我們在本指南後面討論基於使用者的安全性時,將對此進行說明。
請注意, .validate
規則不會級聯。為了允許寫入,必須在層次結構的所有層級上滿足所有驗證規則。
規則不是過濾器
規則以原子方式應用。這意味著,如果該位置或父位置沒有授予存取權限的規則,則讀取或寫入操作將立即失敗。即使每個受影響的子路徑都可以訪問,在父位置的讀取也將完全失敗。考慮這個結構:
{ "rules": { "records": { "rec1": { ".read": true }, "rec2": { ".read": false } } } }
如果不了解規則是自動評估的,那麼取得/records/
路徑可能會回傳rec1
但不會rec2
。但實際結果是錯誤的:
JavaScript
var db = firebase.database(); db.ref("records").once("value", function(snap) { // success method is not called }, function(err) { // error callback triggered with PERMISSION_DENIED });
Objective-C
FIRDatabaseReference *ref = [[FIRDatabase database] reference]; [[_ref child:@"records"] observeSingleEventOfType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) { // success block is not called } withCancelBlock:^(NSError * _Nonnull error) { // cancel block triggered with PERMISSION_DENIED }];
迅速
var ref = FIRDatabase.database().reference() ref.child("records").observeSingleEventOfType(.Value, withBlock: { snapshot in // success block is not called }, withCancelBlock: { error in // cancel block triggered with PERMISSION_DENIED })
爪哇
FirebaseDatabase database = FirebaseDatabase.getInstance(); DatabaseReference ref = database.getReference("records"); ref.addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(DataSnapshot snapshot) { // success method is not called } @Override public void onCancelled(FirebaseError firebaseError) { // error callback triggered with PERMISSION_DENIED }); });
休息
curl https://docs-examples.firebaseio.com/rest/records/ # response returns a PERMISSION_DENIED error
由於/records/
處的讀取操作是原子的,並且沒有讀取規則授予對/records/
下所有資料的存取權限,因此這將引發PERMISSION_DENIED
錯誤。如果我們在Firebase 控制台的安全模擬器中評估此規則,我們可以看到讀取操作被拒絕,因為沒有讀取規則允許存取/records/
路徑。但是,請注意, rec1
的規則從未被評估,因為它不在我們要求的路徑中。要取得rec1
,我們需要直接存取它:
JavaScript
var db = firebase.database(); db.ref("records/rec1").once("value", function(snap) { // SUCCESS! }, function(err) { // error callback is not called });
Objective-C
FIRDatabaseReference *ref = [[FIRDatabase database] reference]; [[ref child:@"records/rec1"] observeSingleEventOfType:FEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) { // SUCCESS! }];
迅速
var ref = FIRDatabase.database().reference() ref.child("records/rec1").observeSingleEventOfType(.Value, withBlock: { snapshot in // SUCCESS! })
爪哇
FirebaseDatabase database = FirebaseDatabase.getInstance(); DatabaseReference ref = database.getReference("records/rec1"); ref.addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(DataSnapshot snapshot) { // SUCCESS! } @Override public void onCancelled(FirebaseError firebaseError) { // error callback is not called } });
休息
curl https://docs-examples.firebaseio.com/rest/records/rec1 # SUCCESS!
重疊的陳述
一個節點可以應用多個規則。在多個規則表達式標識一個節點的情況下,如果任何條件為false
,則存取方法將被拒絕:
{ "rules": { "messages": { // A rule expression that applies to all nodes in the 'messages' node "$message": { ".read": "true", ".write": "true" }, // A second rule expression applying specifically to the 'message1` node "message1": { ".read": "false", ".write": "false" } } } }
在上面的範例中,對message1
節點的讀取將被拒絕,因為第二個規則總是false
,即使第一個規則總是為true
。
下一步
您可以加深對Firebase即時資料庫安全規則的理解:
了解規則語言的下一個主要概念,動態條件,它可以讓您的規則檢查使用者授權、比較現有資料和傳入資料、驗證傳入資料、檢查來自客戶端的查詢結構等等。
查看典型的安全用例以及解決這些用例的 Firebase 安全性規則定義。