检查安全规则中的子字段时,Firestore权限错误

时间:2020-03-27 03:57:48

标签: firebase google-cloud-firestore firebase-security

当我有一个检查子字段的安全规则时,出现“缺少权限不足”的信息。例如:

"start-production": "npm run build && config/startserver.sh"

我在rules_version = '2'; service cloud.firestore { match /databases/{database}/documents { match /entries/{id} { allow read: if resource.data.business.id == "hh7AvLpTbFGRmCGodLV4"; } } } 收藏集中拥有的唯一文档:

entries

我的查询:

{
  "batchId": "bnzVkufp9mM6yGokUYug",
  "business": {
    "id": "hh7AvLpTbFGRmCGodLV4"
  }
}

如果我将安全规则更改为firestore() .collection('entries') .where('batchId', '==', 'bnzVkufp9mM6yGokUYug') ,它将正常工作。有什么我想念的吗?

注意:

  • 在Firebase控制台中使用“规则操场”进行测试时,该规则可以正常工作,但在生产中失败。
  • 我有另一个项目可以很好地与检查子字段的规则配合使用。但是,对于我创建的2个新的Firebase项目,它不起作用。
  • 我在2017年发现了一个相关问题(Firestore security rules, nested field)。会不会再次出现相同的问题?

1 个答案:

答案 0 :(得分:0)

要意识到的一件事是security rules are not filters

现在,您的规则是说,“条目”集合上的所有查询都必须为business.id指定一个等于“ hh7AvLpTbFGRmCGodLV4”的过滤器。您现在拥有的查询没有该过滤器,因此规则总是会拒绝它。如果您希望查询使用这些规则,则必须像这样:

firestore()
  .collection('entries')
  .where('batchId', '==', 'bnzVkufp9mM6yGokUYug')
  .where('business.id', '==', 'hh7AvLpTbFGRmCGodLV4')