gpt4 book ai didi

firebase - 了解 Cloud Firestore 安全规则的限制

转载 作者:行者123 更新时间:2023-12-01 22:18:22 27 4
gpt4 key购买 nike

我正在为 Firestore 数据库编写安全规则,但我可能编写了太多检查,导致授权自动失败。

例如特定路径的规则是

service cloud.firestore {
match /databases/{database}/documents {
match /pending/{userId} {
match /rate/{vendorId}/events/{eventId}/ratings/{rateId} {
allow write: if request.auth.uid == userId
&& exists(/databases/$(database)/documents/vendors/$(vendorId)) // The vendor must exist
&& exists(/databases/$(database)/documents/users/$(userId)/subscriptions/$(vendorId)) // The user must be subscribed to the vendor
&& exists(/databases/$(database)/documents/vendors/$(vendorId)/events/$(eventId)) // The event must exist
&& !exists(/databases/$(database)/documents/vendors/$(vendorId)/events/$(eventId)/ratings/$(userId)) // The user must not have already voted for the event
}
}
}
}

这些规则适用于写入/pending/{userId}/rate/{vendorId}/events/{eventId}/ratings/{rateId}

删除一个或一组规则可以使一切恢复正常。我在文档中读到有关 10 个开发人员定义函数的限制 here ,但exists 和get 被列为服务定义的,不应计算在内。即使有,我在这里也只使用了五个。

有没有更有效的方法来检查相同的字段?如何计算单行达到 10 个函数限制的数量?

谢谢

最佳答案

这里是 Firebase PM:目前,我们将给定规则评估中的 get()exists() 调用数量限制为 3 个,这就是您的原因添加第四个后看到行为失败。我将确保文档得到适当更新以包含此信息。

编辑 (4/2/18):这些限制现已记录在案:https://firebase.google.com/docs/firestore/security/rules-structure#security_rule_limits

编辑(2018 年 5 月 14 日):我们将限制增加到 10:https://firebase.google.com/docs/firestore/security/rules-structure#security_rule_limits

get()exists() 调用比“正常”规则评估的计算成本更高,我们希望确保评估时间的严格限制,以便不要减慢传入请求的速度。我很确定我们可以将数量增加到三个以上,但请注意,我们将查找所有这些键/值,并且评估每个请求可能需要更长的时间/花费更多的时间。

请注意,在这种特定情况下,您可以通过三个调用来完成此操作:

service cloud.firestore {
match /databases/{database}/documents {
match /pending/{userId} {
match /rate/{vendorId}/events/{eventId}/ratings/{rateId} {
// Only allow a document to be created
allow create:
// The user must not have already voted for the event
if request.auth.uid == userId
&& request.auth.uid == rateId
// The vendor must exist
&& exists(/databases/$(database)/documents/vendors/$(vendorId))
// The user must be subscribed to the vendor
&& exists(/databases/$(database)/documents/users/$(userId)/subscriptions/$(vendorId))
// The event must exist
&& exists(/databases/$(database)/documents/vendors/$(vendorId)/events/$(eventId));
// Not necessary unless you want to allow updates
allow update: if ...;
}
}
}
}

关于firebase - 了解 Cloud Firestore 安全规则的限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47619549/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com