Firebase Cloud Firestore-仍然可以获得;任何用户都可以写入您的整个数据库;错误
我已将规则添加到我的Cloud Firestore数据库,但仍收到以下问题消息:Firebase Cloud Firestore-仍然可以获得;任何用户都可以写入您的整个数据库;错误,firebase,google-cloud-firestore,firebase-security,Firebase,Google Cloud Firestore,Firebase Security,我已将规则添加到我的Cloud Firestore数据库,但仍收到以下问题消息: 任何用户都可以读取整个数据库 任何用户都可以写入整个数据库 请参见下面的规则 实际上,任何用户都可以读写“users”集合,否则我将无法登录/注册用户。如何解决这个问题 谢谢当前规则的问题在于,它允许任何用户读/写任何其他用户的用户文档。您需要对其进行更多的限制,以查看文档的用户ID。像这样的 service cloud.firestore { match /databases/{database}/do
- 任何用户都可以读取整个数据库
- 任何用户都可以写入整个数据库
谢谢当前规则的问题在于,它允许任何用户读/写任何其他用户的用户文档。您需要对其进行更多的限制,以查看文档的用户ID。像这样的
service cloud.firestore {
match /databases/{database}/documents {
// Ensure the user is authenticated
function userIsAuthenticated() {
return request.auth != null && request.auth.uid != null
}
// Ensure the user owns the existing resource
function userOwnsResource() {
return userIsAuthenticated() && request.auth.uid == resource.data.userId
}
// Ensure the user owns the new resource
function userOwnsNewResource() {
return userIsAuthenticated() && request.auth.uid == request.resource.data.userId
}
match /users/{userId=**} {
allow read, update, delete: if userOwnsResource();
allow create: if userOwnsNewResource();
}
}
}
userOwnsResource
检查用户是否可以访问现有资源userownsenerSource
检查用户是否可以为其用户ID创建新文档。显然,您可以在其他方便的规则中重用这些函数。问题在于您的规则与/{document=**}
匹配,因为它与数据库中的每个文档都匹配。这个太宽了。根据需要将您的规则缩小到单个集合。谢谢Chris!这就是我需要的!