我有以下Firestore规则。我正在使用带有服务帐户密钥的Admin SDK来访问Firestore。
service cloud.firestore {
match /databases/{database}/documents {
match /users/{user} {
allow read, delete: if true;
allow update, write: if getAfter(/databases/$(database)/documents/users/$(request.resource.id)).data.AccountId is int;
}
}
}
我想在AccountId
集合中强制执行int
字段作为users
,但我仍然可以写(和批量写入)AccountId
是string
。
我正在尝试验证users
集合中每个文档中的数据。
这是将更新user
文档的代码:
try {
await db.collection('users').doc('1').set({
AccountId: '1234'
})
} catch(e) {
console.log(e)
}
这不应该被允许,因为AccountId
不是int。
try {
await db.collection('users').doc('1').set({
AccountId: 1234
})
} catch(e) {
console.log(e)
}
这应该被允许,因为AccountId
是一个int。
使用Admin SDK,我可以将AccountId
设置为string
。
编辑:我已经更新了我的规则,我仍然可以写AccountId
作为string
。
service cloud.firestore {
match /databases/{database}/documents {
match /users/{user} {
allow read: if true;
allow create, update: if request.resource.data.AccountId is int;
}
}
}
Admin SDK无条件绕过所有安全规则。这些规则仅适用于移动和Web客户端。如果它正在做正确的事情,您将需要检查代码本身。