我根據 ID 串列獲取我的檔案。
db.collection("fruits").where(db.FieldPath.documentId(), "in", fruitIds).get()
我應該如何撰寫我的安全規則以允許上述呼叫并拒絕以下呼叫
db.collection("fruits").get()
uj5u.com熱心網友回復:
不可能完全按照您的要求。你可以做的是這樣設定你的規則:
match /fruits/{id} {
allow get: true;
allow list: false;
}
這允許客戶在知道 ID 的情況下獲取檔案,但無法批量查詢檔案。
然后,您必須使用 DocumentReference get()(而不是帶有 where 子句的 Query)單獨撰寫客戶端應用程式請求每個檔案的代碼。對此的性能影響可以忽略不計(不,使用您在此處顯示的方式“輸入”查詢沒有任何明顯的性能提升 - 無論如何您每批只能處理 10 個檔案)。
uj5u.com熱心網友回復:
正如@Doug在他們的回答中所涵蓋的那樣,目前不支持您期望的方式。
但是,通過查看參考,您至少可以限制(查詢)操作,方法是在和list上放置任何查詢使用的條件,以使其更加困難,而不是直接阻止它。orderBylimit
考慮這個答案具有教育意義,只需一個接一個地獲取專案,并在您的安全規則中禁用串列/查詢訪問。對于那些只想混淆而不是直接阻止此類查詢的人,它包含在這里。
這意味著將您的查詢更改為:
db.collection("fruits")
.where(db.FieldPath.documentId(), "in", fruitIds)
.orderBy(db.FieldPath.documentId()) // probably implicitly added by the where() above, but put here for good measure
.limit(10) // this limit applies to `in` operations anyway, but for this to work needs to be added
.get()
service cloud.firestore {
match /databases/{database}/documents {
// Matches any document in the cities collection as well as any document
// in a subcollection.
match /fruits/{fruit} {
allow read: if <condition>
// Limit documents per request to 10 and only if they provide an orderBy clause
allow list: if <condition>
&& request.query.limit <= 10
&& request.query.orderBy = "__name asc" // __name is FieldPath.documentId()
allow write: if <condition>;
}
}
}
使用這些限制,這應該不再起作用:
db.collection("fruits").get()
但是您仍然可以使用以下方法將所有內容刮成較小的塊:
const fruits = [];
const baseQuery = db.collection("fruits")
.orderBy(db.FieldPath.documentId())
.limit(10);
while (true) {
const snapshot = await (fruits.length > 0
? baseQuery.startAt(fruits[fruits.length-1]).get()
: baseQuery.get())
Array.prototype.push.apply(fruits, snapshot.docs);
if (snapshot.empty) {
break;
}
}
// here, fruits now contains all snapshots in the collection
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/446123.html
標籤:javascript 火力基地 谷歌云火库 firebase-安全
