2015-09-04 70 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

完美!谢谢! –