2015-09-04 68 views
0

有沒有辦法只允許(但不要求)Firebase對象中的鍵?我知道你可以使用.validate來確保對象具有某些鍵。是否有可能只有允許在某種白名單中的某些鍵?如果不是這樣,這似乎是一個很好的方式,讓不需要/不必要的數據從惡意客戶端進入數據庫。限制Firebase中允許的鍵

回答

4

您可以使用Firebase的$變量禁止所有非指定的子項。從Firebase guide on securing your data,談到這個例子:

{ 
    "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 } 
    } 
    } 
} 

所以widget節點可以有一個color和/或title財產。但如果它有任何其他屬性,它將被拒絕。

因此,這些都是有效的,根據這些安全規則:

ref.child('widget').set({ title: 'all is blue' }); 
ref.child('widget').set({ color: 'blue' }); 
ref.child('widget').set({ title: 'all is blue', color: 'blue' }); 

但根據上述規則,這些都無效:

ref.child('widget').set({ titel: 'all is blue' }); 
ref.child('widget').set({ title: 'all is blue', description: 'more...' }); 
+0

完美!謝謝! –