首页 > 解决方案 > Firestore 最近的哪些变化使它抱怨“重叠递归通配符匹配语句”?

问题描述

今天我注意到我无法部署我的 Firestore 规则,即使它们到目前为止运行良好并且我没有更改它们。这是它不喜欢的部分的摘录:

match /databases/{database}/documents {

    function userMatchesId(userId) {
      return request.auth != null
          && request.auth.uid == userId
    }

    function userIsAdmin() {
      return request.auth != null
          && get(/databases/$(database)/documents/users/$(request.auth.uid)).data.role == "admin"
    }

    // === Admins ====
    // Admin users are allowed to access everythings.
    // Writes should be performed via code executed by a service account
    match /{document=**} {
      allow read: if userIsAdmin()
    }

    // ==== Private ====
    // Collections private to the user. Documents read access is matched
    // with the authenticated user id.
    match /users/{userId} {
      allow get: if userMatchesId(userId)
    }

    match /userCredits/{userId} {
      allow get: if userMatchesId(userId)
    }
}

在实践中,这些规则就像我想象的那样有效。允许管理员从非管理员无法直接查询的集合中读取。但是,现在我在部署期间收到此错误:

错误:firestore.rules 中的编译错误:

[W] 42:5 - 重叠递归通配符匹配语句。

我不太明白这里的问题。你会如何解决这个问题?

标签: firebasegoogle-cloud-firestorefirebase-cli

解决方案


(这里是 Google 员工) 这是我们的一个错误。我们正在向规则添加新的编译器警告,以帮助您注意可能引入的错误。许多人没有意识到,如果您有多个match与特定路径匹配的语句,那么这些块中的规则是OR'ed在一起的。该警告应该可以帮助您发现这一点。

但是,如果您了解自己在做什么,这绝不是为了阻止您部署有效规则!我们会解决这个问题。


太平洋标准时间 8 月 1 日上午 11:50 更新

我们在这里做了两个改变:

  • 我们正在发布 CLI (npm firebase-tools) 版本4.0.2,并修复此问题以使警告不致命。这应该暂时发生。
  • 我们将更改服务器行为以撤消/澄清此行为,直到我们正确为止。

推荐阅读