Firestore rule to read collection

自古美人都是妖i 提交于 2019-12-11 14:18:50

问题


Firestore rule to read collection overrides document rule to deny unauthorized access of other users data.

Here's my scenario, I'm getting the user information with the phone number associated by the Authentication and the Document in the database. I'm querying the whole /users collection with where clause and in the Firestore Rules I'm letting anyone to read /users collection, but I think this is insecure.

Javascript

const phone_number = firebase.auth().currentUser.phoneNumber // Example: "+5521988887777"
const usersRef = firebase.firestore().collection('users')

usersRef.where("phone_number", "==", phone_number).limit(1).get()
  .then(snapshot => {
    const doc = snapshot.docs[0]

Firestore Rules

service cloud.firestore {
  match /databases/{database}/documents {
    match /users {
      allow read;
    }
    match /users/{user} {
      allow read, write: if request.auth != null && request.auth.token.phone_number == resource.data.phone_number;
    }
  }
}

I'm trying to achieve a workaround to the issue, thanks.


回答1:


To correct the security rule I have removed the first condition to allow all reads (as commented above).

Working Firestore Rules

service cloud.firestore {
  match /databases/{database}/documents {
    match /users/{user} {
      allow read, write: if request.auth != null && request.auth.token.phone_number == resource.data.phone_number;
    }
  }
}


来源:https://stackoverflow.com/questions/51754070/firestore-rule-to-read-collection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!