使用功能onSnapshot时会忽略Cloud Firestore安全规则

时间:2018-09-24 10:49:23

标签: javascript firebase google-cloud-firestore firebase-security-rules react-native-firebase

使用onSnapShot时,我的安全角色将被忽略-我将获取集合中的所有文档,而不仅是用户对象,是不是

角色:

service cloud.firestore {
match /databases/{database}/documents {

match /story/{mid} {

  function memberOf() {
    return resource.data.creator && request.auth.uid == resource.data.creator;
  }
  allow list: if request.query.limit <= 1000 &&
                 memberOf();
  allow get,read : if memberOf();
  allow write: if request.auth.uid == resource.data.creator;
  }
 }
}

,我的代码在Android上的react-native-firebase中: “ react-native-firebase”:“ ^ 4.3.8”, “ com.google.firebase:firebase-firestore:17.1.0”

ctor:    this.ref = firebase.firestore().collection('story');

componentDidMount() {
    this.unsubscribeDate = this.ref.onSnapshot(this.onCollectionUpdate);
}

componentWillUnmount() {
    this.unsubscribeDate();
}

onCollectionUpdate = querySnapshot => {
    //console.log('onCollectionUpdate', querySnapshot);
    querySnapshot.forEach(doc => {
        const { title, complete } = doc.data();
        console.log('onCollectionUpdate doc', title);
    });
};

参考文档: role based access roles query

1 个答案:

答案 0 :(得分:1)

此行为是一个错误。在PWA中使用javascript可以得到相同的结果。

我正在做测试,我注意到在Firestore中使用持久性时会发生问题。使用onSnapshot时,此命令将在启动时执行2次,如果我停用了Firestore的持久性,答案是空的,正如预期的那样,但是启用了持久性后,将显示缓存的结果,然后将执行onSnapshot事件的更新,但由于Google控制台中的规则拒绝了读取,因此它不会通过事件(onCollectionUpdate)更新结果,因此您正在读取不应显示的数据。

如果先前在高速缓存中有数据,这是第一次在设备中使用该应用程序,则该规则可以完美地起作用,因为先前在高速缓存中没有数据。我认为此行为是一个错误,因为即使知道缓存,它也应遵守该规则,因为它试图限制文档的读取。

要在onSnapshot上打印错误:

componentDidMount() {
    this.unsubscribeDate = this.ref.onSnapshot(this.onCollectionUpdate, this.onErrorUpdate);
}

onErrorUpdate = error => {
    console.log(error);
};