这些安全规则适用于Firestore上公开可读的配置文件吗



这些规则对于创建新配置文件是否安全,或者我应该要求request.auth.uid等于userId吗?我希望任何登录的用户都能够获取任何配置文件,但仅限于当前登录的用户更新或删除它。

match /profiles/{userId} {
allow read, create: if request.auth != null;
allow update, delete: if request.auth != null && request.auth.uid == userId;
}

create是否也需要用户id与身份验证id匹配,或者考虑到我创建的配置文件如下:

// Listen to .onCreate trigger
exports.createUserAndProfile = functions.auth.user().onCreate((user) => {
// Create the user's public profile (any user can access this)
const newUserProfile = admin.firestore().doc(`/profiles/${user.uid}`).set({
familyName: null,
givenName: null,
preferredName: null,
});
return newUserProfile;
});

如果您希望所有配置文件都能被任何登录用户读取,但只能由UID等于文档ID的用户修改,那么您的规则看起来基本上是可以的。

唯一需要改变的是你已经想知道的:确保登录用户也只能创建自己的文档。因此:

match /profiles/{userId} {
allow read: if request.auth != null;
allow create, update, delete: if request.auth != null && request.auth.uid == userId;
}

可以简化为:

match /profiles/{userId} {
allow read: if request.auth != null;
allow write: if request.auth != null && request.auth.uid == userId;
}

最新更新