Firebase 安全规则权限不足
Firebase security rules insufficient permissions
我在我的 firebase 项目中拥有所有者角色,我正在尝试保护我的 Firestore 数据库。我希望我的 collections 在默认情况下受到保护,并且只有在我为它制定规则时才能访问。我在生产模式下创建了我的数据库。但是,出于某种原因,即使我指定了允许我访问 collection 的规则,我也会收到 Missing or insufficient permissions
错误
例如,当我注册一个用户时,我也想创建我自己的用户collection。
注册用户有效,但创建我的 collection 无效。
注册用户查询(工作并将用户添加到 firebase 身份验证)
register(value) : Promise<any> {
return new Promise<any>((resolve, reject) => {
this.afAuth.createUserWithEmailAndPassword(value.email, value.password)
.then(res => {
resolve(res);
}, err => {
console.log(err)
reject(err);
})
});
}
创建用户 collection 查询(CreateCurrentUserCollection 不起作用)
public async getCurrentUser() : Promise<any>
{
return new Promise<any>((resolve, reject) => {
let user = this.afAuth.onAuthStateChanged(function(user){
if (user) {
resolve(user);
} else {
reject('No user logged in');
}
});
});
}
public CreateCurrentUserCollection(): void
{
this.authService.getCurrentUser().then(res => {
this.firestore.collection<User>("usersBackup")
.add({
uid: res.uid,
username: res.email
})
});
}
Firebase 规则
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /users {
allow read, write: if
request.time < timestamp.date(2221, 7, 20);
}
match /usersBackup {
allow read, write: if
request.time < timestamp.date(2221, 7, 20);
}
}
}
如果我添加以下规则,例如
match /{document=**} {
allow read, write: if request.auth != null;
它确实有效。然而,我的其他安全规则 none 起作用了,我想阻止某些用户访问某些文档。我错过了什么或做错了什么?如果您需要更多信息,请提前告诉我。
更新
正如 Dharmaraj 在他的回答中指出的,安全规则必须指向文档而不是 collection。这解决了我的 collection 之一的问题。但是,我还有一个项目 collection,其中包含一个名为 projectMembers 的数组字段。我只希望在 projectMembers 数组中具有 uid 的用户可以访问项目文档。出于某种原因它总是给我 Missing or insufficient permissions
错误,即使项目 collection 是空的。
查询
public getProjectsOfUser(userUid: string) : Observable<IProject[]> {
return this.firestore.collection<IProject>("projects", ref =>
ref.where("projectMembers", "array-contains", userUid))
.valueChanges({ idField: 'id'});
}
Firebase 规则
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /users/{userID} {
allow read, write: if isSignedIn() && request.auth.uid == userID
}
match /usersBackup/{document=**} {
allow read, write: if
request.time < timestamp.date(2221, 7, 20);
}
match /projects/{projectId} {
allow read, write: if
isProjectMember(projectId, request.auth.uid);
}
//functions
function isSignedIn()
{
return request.auth != null;
}
function isProjectMember(projectId, userId) {
return userId in get(/databases/$(database)/documents/projects/$(projectId)).data.projectMembers;
}
}
}
数据库结构
Firebase 游乐场
JWT 令牌通过 Angular 发送到 Firebase
中所述
All match statements should point to documents, not collections. A match statement can point to a specific document, as in match /cities/SF or use wildcards to point to any document in the specified path, as in match /cities/{city}.
如果您希望用户只编辑他们自己的文档,您应该尝试:
match /users/{userID} {
allow read, write: request.auth != null && request.auth.uid == userID;
}
如果没有,那么您可以尝试添加一个 recursive wildcard,这样该规则将应用于该集合中的所有文档:
match /users/{document=**} {
allow read, write: request.time < timestamp.date(2221, 7, 20);
}
请注意,如果使用递归通配符,任何人都可以写入任何文档。
更新:
检查数组中是否存在 UID。
allow read, write: if request.auth != null && request.auth.uid in resource.data.projectMembers
我在我的 firebase 项目中拥有所有者角色,我正在尝试保护我的 Firestore 数据库。我希望我的 collections 在默认情况下受到保护,并且只有在我为它制定规则时才能访问。我在生产模式下创建了我的数据库。但是,出于某种原因,即使我指定了允许我访问 collection 的规则,我也会收到 Missing or insufficient permissions
错误
例如,当我注册一个用户时,我也想创建我自己的用户collection。 注册用户有效,但创建我的 collection 无效。
注册用户查询(工作并将用户添加到 firebase 身份验证)
register(value) : Promise<any> {
return new Promise<any>((resolve, reject) => {
this.afAuth.createUserWithEmailAndPassword(value.email, value.password)
.then(res => {
resolve(res);
}, err => {
console.log(err)
reject(err);
})
});
}
创建用户 collection 查询(CreateCurrentUserCollection 不起作用)
public async getCurrentUser() : Promise<any>
{
return new Promise<any>((resolve, reject) => {
let user = this.afAuth.onAuthStateChanged(function(user){
if (user) {
resolve(user);
} else {
reject('No user logged in');
}
});
});
}
public CreateCurrentUserCollection(): void
{
this.authService.getCurrentUser().then(res => {
this.firestore.collection<User>("usersBackup")
.add({
uid: res.uid,
username: res.email
})
});
}
Firebase 规则
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /users {
allow read, write: if
request.time < timestamp.date(2221, 7, 20);
}
match /usersBackup {
allow read, write: if
request.time < timestamp.date(2221, 7, 20);
}
}
}
如果我添加以下规则,例如
match /{document=**} {
allow read, write: if request.auth != null;
它确实有效。然而,我的其他安全规则 none 起作用了,我想阻止某些用户访问某些文档。我错过了什么或做错了什么?如果您需要更多信息,请提前告诉我。
更新
正如 Dharmaraj 在他的回答中指出的,安全规则必须指向文档而不是 collection。这解决了我的 collection 之一的问题。但是,我还有一个项目 collection,其中包含一个名为 projectMembers 的数组字段。我只希望在 projectMembers 数组中具有 uid 的用户可以访问项目文档。出于某种原因它总是给我 Missing or insufficient permissions
错误,即使项目 collection 是空的。
查询
public getProjectsOfUser(userUid: string) : Observable<IProject[]> {
return this.firestore.collection<IProject>("projects", ref =>
ref.where("projectMembers", "array-contains", userUid))
.valueChanges({ idField: 'id'});
}
Firebase 规则
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /users/{userID} {
allow read, write: if isSignedIn() && request.auth.uid == userID
}
match /usersBackup/{document=**} {
allow read, write: if
request.time < timestamp.date(2221, 7, 20);
}
match /projects/{projectId} {
allow read, write: if
isProjectMember(projectId, request.auth.uid);
}
//functions
function isSignedIn()
{
return request.auth != null;
}
function isProjectMember(projectId, userId) {
return userId in get(/databases/$(database)/documents/projects/$(projectId)).data.projectMembers;
}
}
}
数据库结构
Firebase 游乐场
JWT 令牌通过 Angular 发送到 Firebase
All match statements should point to documents, not collections. A match statement can point to a specific document, as in match /cities/SF or use wildcards to point to any document in the specified path, as in match /cities/{city}.
如果您希望用户只编辑他们自己的文档,您应该尝试:
match /users/{userID} {
allow read, write: request.auth != null && request.auth.uid == userID;
}
如果没有,那么您可以尝试添加一个 recursive wildcard,这样该规则将应用于该集合中的所有文档:
match /users/{document=**} {
allow read, write: request.time < timestamp.date(2221, 7, 20);
}
请注意,如果使用递归通配符,任何人都可以写入任何文档。
更新: 检查数组中是否存在 UID。
allow read, write: if request.auth != null && request.auth.uid in resource.data.projectMembers