【发布时间】:2020-10-30 12:48:49
【问题描述】:
考虑以下规则:
match /triplogs/{anyDocument} {
allow create: if request.auth != null;
allow read, update, delete: if request.auth.uid == resource.data.userId;
}
如果我用我的登录用户点击它,我会得到标准:
错误:FirebaseError:[code=permission-denied]:权限缺失或不足。
需要了解的几点:
- 我确实登录了,可以输出当前用户的UID,是正确的。
- 我可以通过移动版本走同样的路线,而且效果很好。
- 如果我删除
resource.data.userId检查,并仅检查用户是否已通过身份验证(甚至硬编码 uid),它会起作用(因此该规则似乎得到了适当的检查)
模拟器说“资源”为空。我不明白资源对象怎么可能为空,也许是模拟器的问题?
任何帮助将不胜感激,在过去的几个小时里我已经解决了问题并在谷歌上搜索了,但没有成功。
有问题的查询:
// ... My firebase wrapper:
import { firebaseApp } from '../utils/firebase';
// ...
const collectionName = 'triplogs';
export const fetchTrips = async (filters?: Filter[]) => {
let query;
const ref = db?.collection(collectionName);
const trips: TripLog[] = [];
if (filters) {
filters.forEach((filter) => {
query = ref?.where(filter.field, filter.operator, filter.value);
});
}
try {
const obj = query || ref;
console.log(firebaseApp()?.auth().currentUser?.uid); // <-- this is populated with my logged in UID, FWIW
const docs = await obj?.get();
docs?.forEach((doc) => {
const data = doc.data() as TripLog;
trips.push({
...data,
id: doc.id
});
});
return trips;
} catch (err) {
throw new Error(err);
}
};
这是我尝试在模拟器中运行示例请求时的信息,违反了这些身份验证规则:
{
"path": "/databases/%28default%29/documents/triplogs/%7BanyDocument%7D"
"method": "get"
"auth": {
"uid": "5ompaySrXQcL9veWr3QlSujwlDS2"
"token": {
"sub": "5ompaySrXQcL9veWr3QlSujwlDS2"
"aud": "....omitted"
"firebase": {
"sign_in_provider": "google.com"
}
"email": "...omitted..."
"email_verified": true
"phone_number": ""
"name": "...."
}
}
"time": "2020-10-30T16:48:39.601Z"
}
sim中的错误是:
Error: simulator.rules line [32], column [58]. Null value error.
^^ 哪个是资源对象
如果我将身份验证规则更改为以下,那么我将取回数据
allow read, update, delete: if request.auth.uid != null;
类似的数据:
[{
"createdAt": 1597527979495,
"name": "Foo Bar Trip",
"date": 1597527979495,
"userId": "5ompaySrXQcL9veWr3QlSujwlDS2",
"id": "FnH2E9WfDkpRHPLXxlDy"
}]
但只要我检查一个字段,“资源”对象就为空
allow read, update, delete: if request.auth.uid == resource.data.userId;
【问题讨论】:
-
如果没有看到minimal code that reproduces the problem 的这些规则,任何人都很难说出发生了什么。由于您声称已登录,请确保代码在引发错误的调用之前记录活动用户,然后在您的问题中包含该代码及其输出。
-
我们看不到客户端代码符合您的规则。您的问题应包含足够的信息,以便我们可以自行复制问题。
-
对不起,我添加了一个代码示例。如果今晚晚些时候我能有时间,我会在某个地方创建一个 repro 应用程序。
-
很高兴您找到了解决问题的方法。但是,实际的答案/解决方案不应编辑到您的问题中。一般来说,您应该edit 来澄清这个问题,但不要在其中包含答案。您应该使用用于解决问题的代码/解决方案创建自己的答案,然后接受它(系统可能需要 48 小时延迟才能这样做)。当您自己解决问题后,answering your own question is encouraged。
标签: google-cloud-firestore firebase-authentication firebase-security