如何调试 firestore.rules 变量和函数?

Mat*_*ner 8 firebase firebase-security google-cloud-firestore

我在尝试诊断 firestore.rules 文件中的特定规则时遇到困难。请在此处查看该问题以了解上下文。

有没有办法调试 firestore.rules 文件和/或函数?我正在使用单元测试和模拟器来测试我的规则,但我真的很想看看规则引擎正在评估哪些值。

例如,这是我的 firestore.rules 文件:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /organizations/{orgId} {
      allow read: if isAdmin();
      allow create, update: if isAdmin();

      match /classes/{classId} {
        allow read: if request.auth.uid != null;
        allow create, update: if isAdmin();

        match /students/{studentId} {
          allow read: if isAdmin() || belongsToCurrentClass();
          allow create, update: if isAdmin();
        }
      }
    }
  }
}

function isAdmin() {
  // removed for security
}

function belongsToCurrentClass() {
  // retuns true if the authenticated user is the teacher of the requested class
  return get(/databases/$(database)/documents/organizations/$(orgId)/classes/$(classId)).data.teacherUid == request.auth.uid;
}
Run Code Online (Sandbox Code Playgroud)

我想做的是设置断点或单步执行代码。在组织/{orgId}/classes/{classId}/students/{studentId} 路径上尝试 CRUD 操作时,我很想检查 orgId、classId 和 studentId 变量所持有的确切值,以及资源和请求参数。我很想检查belongsToCurrentClass 中的get 请求返回的是哪个文档(如果有)以及返回值是什么。

有谁知道有什么方法可以做到这一点?如果我能看到正在评估的数据,我想我会在 10 秒内回答我上面提到的问题。

Dou*_*son 4

Cloud Firestore 安全规则有一个本地模拟器。这是深入研究安全规则执行的最佳(也是唯一的)工具。没有逐步调试,但可以在控制台中看到大量调试输出。

https://firebase.google.com/docs/rules/emulator-setup

  • 在本地测试规则时,您会在控制台中看到链接此错误:“false for 'get'@L5, false for 'get'@L38”。完全没用 (15认同)