为什么不强制执行此Firestore安全规则?

问题描述 投票:1回答:1

我有以下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;
    }
  }
}

我想在AccountId集合中强制执行int字段作为users,但我仍然可以写(和批量写入)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是一个int。

使用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-rules
1个回答
1
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.