【发布时间】:2018-08-08 19:13:11
【问题描述】:
读取集合的 Firestore 规则会覆盖文档规则以拒绝未经授权访问其他用户数据。
这是我的场景,我正在获取用户信息以及数据库中的身份验证和文档关联的电话号码。我正在使用 where 子句和 Firestore 规则查询整个 /users 集合,我让任何人阅读 /users 集合,但我认为这是不安全的。
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 规则
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;
}
}
}
我正在尝试解决此问题,谢谢。
【问题讨论】:
-
第一个
match /users允许公共读取访问,对吧?你不想删除它吗? -
但没有它,我无法使用 where 查询集合
-
我想我现在明白了,如果只有一个查询结果不被允许,整个请求都会被拒绝,对吧?
-
默认访问(空规则文件)是禁止任何/全部。
-
@ViniciusFontoura 您应该能够查询多个文档,只要读取安全规则允许每个文档。情况似乎如此,因为安全规则和查询都使用 phone_number。所以我认为您可以删除第一个条件以允许所有读取。
标签: javascript firebase firebase-authentication google-cloud-firestore firebase-security