这个问题在这里已有答案:
我无法使用Firestore安全规则限制创建的文档中的字段数。 https://firebase.google.com/docs/firestore/security/rules-structure
我的用户文档中有7个字段。我想限制任何文件CREATE尝试我期望的字段数。我已将数字设置为100,这显然不是我发送给故意创建失败的内容。我希望100失败,但事实并非如此。
match /users/{uid} {
// Applies to writes to nonexistent documents
allow create: if request.resource.data.size() == 100;
}
我还尝试将size属性直接放在资源对象上但没有成功。
allow create:if request.resource.size()== 100;
这是尝试CREATE操作的代码;
const DEFAULT_systemRole = 'User';
const DEFAULT_disabled = false;
// creates up a static sentinel value to be set on the Firestore server
var serverTimestamp = admin.firestore.FieldValue.serverTimestamp()
admin.firestore().collection('users').doc(req.body.uid).set({
// sent from client
usernameSlug: req.body.usernameSlug,
username: req.body.username,
email: req.body.email,
// set only here on server
systemRole: DEFAULT_systemRole,
disabled: DEFAULT_disabled,
dateModified: serverTimestamp,
dateCreated: serverTimestamp,
}).then(function() {
console.log("Successfully created FireStore user");
}).catch(function(error) {
console.log("Error creating user:", error);
throw(error)
})
根据设计,我碰巧使用的Firebase Admin sdk似乎不支持Firebase安全规则。相反,Firebase管理员sdk BYPASSES所有安全规则。所以看起来我会将所有调用移动到Firebase Admin sdk并简单地使用以下规则锁定Firestore数据库,以防止客户端黑客攻击的任何可能性。
match /users/{uid} {
allow read, write: if false;
}
以下帖子也发现了同样的情况; How to get Firestore rules working with service accounts