【问题标题】:Firestore Security Rules breaking with update ruleFirestore 安全规则违反更新规则
【发布时间】:2019-01-09 01:09:41
【问题描述】:

我昨天发布了一个关于此的问题,但我正在创建一个包含更多详细信息的新问题。 Firestore .setData is blocked by update rule not create

我已经运行了模拟器并且规则在那里工作。此外,当我创建文档并将 swift 代码中的 setData 更改为 update 时,代码有效。它似乎只在创建文档时失败。但问题是,当我删除 update 规则或将其更改为 allow update: if false; 时,setData(或被规则视为创建)会正确执行。我不知道发生了什么,也不知道有什么工具可以更好地了解情况。

 match /users_real/{userID} {
    allow create: if true;
    allow read: if isOwner(userID);
    allow update: if (request.writeFields.size() == 1);

}

设置数据:

self.docRef.collection("users_real").document("adfadsf").setData(post) { (error) in

            if let error = error {
                print("He dead!: \(error.localizedDescription)")


            }
            else {
                print("it worked, for now")


            }
        }

【问题讨论】:

  • The documentation 表示当文档存在时应用create规则,当文档存在时应用update规则> 存在。这意味着文档的存在是决定因素,而不是操作类型(setData()updateData()。您是说当您将相同的数据库状态更改为 setData() 时会看到不同的行为@ 987654335@?
  • 不,我看到的是,对于数据库状态 setData(),权限成功或失败取决于实践中的“允许更新”规则(这没有意义)(模拟器工作美好的)。我还观察到,当我实际更新 (updateData) 时,安全规则会被正确调用,并且事情应该会通过。只有 setData 上的特定更新规则失败(这些特定规则存在问题)。
  • 发布的代码使用setData()。如果文档“adfadsf”不存在,则使用create 规则。如果“adfadsf”确实存在。使用update 规则。 updateData() 只能用于现有文档 (it will fail if the doc does not exist),因此始终使用 update 规则。
  • 但这就是我如此困惑的原因。当我运行模拟时,创建通过并指示创建规则。但实际上它仅在存在更新规则时才会失败。您是否知道从 db 端获取有关安全规则的更多信息的方法(可能类似于云功能的日志)?
  • 您的更新规则有一个额外的(。这只是您帖子中的错字吗?

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


【解决方案1】:

Firebase 支持确认存在与评估 request.writeFields.size() 相关的错误。没有给出何时修复的估计。

可以通过以下规则来证明bug的存在:

service cloud.firestore {
  match /databases/{database}/documents {

    match /cities/{city} {
      // This should always evaluate to true, but does not.
      allow create: if (request.writeFields.size() == 1) || (request.writeFields.size() != 1);
      allow update: if true;
    }
  }
}

虽然create 规则的计算结果应始终为真,但创建城市的尝试会因权限被拒绝而失败。似乎request.writeFields 的问题不仅影响它出现的规则,还影响路径的其他规则。对于上面显示的规则,更新现有城市的尝试也会因 Permission Denied 而失败。

【讨论】:

    猜你喜欢
    • 2020-06-19
    • 2019-01-15
    • 2018-03-27
    • 1970-01-01
    • 2018-10-19
    • 2019-07-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多