【问题标题】:Why isn't this Firestore security rule being enforced?为什么不执行此 Firestore 安全规则?
【发布时间】:2019-03-11 03:58:59
【问题描述】:

我有以下 Firestore 规则。我正在使用带有服务帐户密钥的 Admin SDK 来访问 Firestore。

service cloud.firestore {
  match /databases/{database}/documents {
    match /users/{user} {
      allow read, delete: if true;
      allow update, write: if getAfter(/databases/$(database)/documents/users/$(request.resource.id)).data.AccountId is int;
    }
  }
}

我想在users 集合中将AccountId 字段强制为int,但我仍然可以写入(和批量写入)AccountIdstring

我正在尝试验证 users 集合中每个文档中的数据。

下面是更新user文档的代码:

try {
  await db.collection('users').doc('1').set({
    AccountId: '1234'
  })

} catch(e) {
  console.log(e)
}

这是不允许的,因为 AccountId 不是 int。

try {
  await db.collection('users').doc('1').set({
    AccountId: 1234
  })

} catch(e) {
  console.log(e)
}

这应该是允许的,因为AccountId 是一个整数。

使用 Admin SDK,我可以将 AccountId 设置为 string

编辑: 我已经更新了我的规则,我仍然可以将AccountId 写成string

service cloud.firestore {
  match /databases/{database}/documents {
    match /users/{user} {
        allow read: if true;
        allow create, update: if request.resource.data.AccountId is int;
    }
  }
}

【问题讨论】:

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


    【解决方案1】:

    Admin SDK 无条件绕过所有安全规则。这些规则仅适用于移动和 Web 客户端。如果代码正确,您需要检查代码本身。

    【讨论】:

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