【问题标题】:Firestore Rules - why my teste on lab tests do not passFirestore 规则 - 为什么我的实验室测试没有通过
【发布时间】:2021-07-03 14:01:37
【问题描述】:

感谢您的帮助。

我有一个文档

/rooms/CAhYMQ3aDFvsDxrIxyg

和规则

rules_version = '2';
service cloud.firestore {

 [...]

 match /rooms/{roomId} {
   allow get;
 }

 [...]

}

但是当测试这条“路线”时,他们没有通过

(测试实验室)

  • 类型模拟:获取
  • 本地:/rooms/CAhYMQ3aDFvsDxrIxyga
  • 身份验证:真

编辑:

所有规则:

rules_version = '2';
service cloud.firestore {

  match /users/{userId}/{anyUserFile=**} {
    allow read: if IsAuth();
  }
  
  match /users/{userId}/{anyUserFile=**} {
    allow write: if IsOwner(userId);
  }
  
  match /rooms/{roomId} {
    allow get;
  }
  
  match /rooms/{roomId}/messages/{messageId}{
    allow write: if IsMessageOwner();
  }
}

删除的功能只是放东西就可以破坏了。

firestore picture

playground picture

Edit2:(工作)

rules_version = '2';
service cloud.firestore {

    function IsAuth(){
      return request.auth != null;
    }

    function IsOwner(userId){
      return IsAuth() && request.auth.uid == userId;
    }
  
    function IsMessageOwner(){
      return IsAuth() && resource.data.sender_auth.uid == request.auth.uid;
    }

    match /databases/{database}/documents {
      match /rooms/{roomId} {
        allow read, get, list: if true;
      }
    
      match /users/{userId}/{anyUserFile=**} {
        allow read: if IsAuth();
      }
    
      match /users/{userId}/{anyUserFile=**} {
        allow write: if IsOwner(userId);
      }
    
      match /rooms/{roomId}/messages/{messageId}{
        allow write: if IsMessageOwner();
      }
  } 
}

我不明白我需要始终声明数据库根目录。 匹配 /rooms/{roomId} 有效:D

【问题讨论】:

  • 这似乎对我有用。您能分享一下[...] 的其余规则是什么样的,并分享一个规则游乐场输出的屏幕截图吗?
  • 信息发送

标签: google-cloud-firestore firebase-security


【解决方案1】:

提供的当前安全规则是有效的,这很可能是一个级联问题。通常这是指定位置上方的通配符的结果,该通配符定义的条件在此处发生冲突。确保所有通配符和“默认”行为低于所有其他定义的路径。

service cloud.firestore {
  match /databases/{database}/documents {
    // Matches any document in the 'cities' collection.
    match /cities/{city} {
      allow read, write: if false;
    }

    // Matches any document in the 'cities' collection or subcollections.
    match /cities/{document=**} {
      allow read, write: if true;
    }
  }
}

注意默认列在指定字段之后

更新

确保您的规则使用条件if <condition> 解析,因为虽然它可能在模拟器中工作,但它不是与最终结果进行一对一比较,因为它的解释是模拟的。

【讨论】:

  • 我在 /rooms 路径上没有通配符。我提出了更多信息问题,感谢您的帮助^^
  • 已更新,您必须包含一个条件。没有条件,这将在现场产品中失败 - 这与 allow read: if null 相同
  • match /rooms/{roomId} { allow get: if true (or check if auth !== null); } 不起作用
  • 你能用你正在使用的查询更新问题吗?通常 get 仅应用于单个文档,这意味着如果您使用 where 查询,则通常还应考虑 listread
  • match /rooms/{roomId} { allow get, read, list: if request.auth != null; } 没用,是不是跟规则有关?我真的不知道。它与其他规则不冲突,我仅使用此规则进行了测试。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-04-18
  • 2021-06-17
  • 1970-01-01
  • 2021-08-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多