如何保护我的 Firestore 数据库免受不必要的访问?
How can I secure my Firestore database agains unwanted access?
我正在使用 Firebase Firestore 将交易列表存储在名为 transactions
的集合中。
我使用 React 从集合中获取交易,使用带有交易文档 ID 的 URL:http://myurl.com/h1kj54h2jk35h
const id = match.params.id;
firebase.firestore().collection('transactions').doc(id).get()
与我创建新文档的方式相同。我没有 firebase 身份验证。
如何保护我的 Firestore 规则:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /transactions/{id} {
allow read, write;
}
}
}
如果我不允许写,我就不能创建新的交易。
如果我不允许读取,我不能读取交易,但我不想允许读取 all 个交易。只有来自 URL 的 id 有效的一次。
因此,我正在寻找一种方法来保护我的数据库免受不需要的文档创建和不需要的文档读取的影响。
我认为您正在寻找所谓的granular operations。从那个 link:
In some situations, it's useful to break down read and write into more granular operations. For example, your app may want to enforce different conditions on document creation than on document deletion. Or you may want to allow single document reads but deny large queries.
A read
rule can be broken into get
and list
, while a write
rule can be broken into create
, update
, and delete
.
因此,如果您只想允许创建新文档和获取用户必须知道 ID 的文档,那就是:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /transactions/{id} {
allow create, get;
}
}
}
我正在使用 Firebase Firestore 将交易列表存储在名为 transactions
的集合中。
我使用 React 从集合中获取交易,使用带有交易文档 ID 的 URL:http://myurl.com/h1kj54h2jk35h
const id = match.params.id;
firebase.firestore().collection('transactions').doc(id).get()
与我创建新文档的方式相同。我没有 firebase 身份验证。
如何保护我的 Firestore 规则:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /transactions/{id} {
allow read, write;
}
}
}
如果我不允许写,我就不能创建新的交易。 如果我不允许读取,我不能读取交易,但我不想允许读取 all 个交易。只有来自 URL 的 id 有效的一次。
因此,我正在寻找一种方法来保护我的数据库免受不需要的文档创建和不需要的文档读取的影响。
我认为您正在寻找所谓的granular operations。从那个 link:
In some situations, it's useful to break down read and write into more granular operations. For example, your app may want to enforce different conditions on document creation than on document deletion. Or you may want to allow single document reads but deny large queries.
A
read
rule can be broken intoget
andlist
, while awrite
rule can be broken intocreate
,update
, anddelete
.
因此,如果您只想允许创建新文档和获取用户必须知道 ID 的文档,那就是:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /transactions/{id} {
allow create, get;
}
}
}