在 Firestore 中,如何忽略不需要的创建输入?

In Firestore, how to ignore unwanted inputs for creation?

众所周知,我们可以这样创建记录:

const docRef = db.collection('forms').add({
    name: 'Some name',
    address: 'Some address'
    userId: 'some user ID'
});

但是,如果某些用户(经过身份验证的用户)POST 到您的商店:

const docRef = db.collection('forms').add({
    name: 'Some name',
    address: 'Some address'
    userId: 'some user ID',
    unwantedData1: 'xxx,
    unwantedData2: 'xxx,
    unwantedData3: 'xxx,
    unwantedData4: 'xxx,
    unwantedData5: 'xxx,
    unwantedData6: 'xxx,
    unwantedData6: 'xxx,
    ... // <- too many rubbish params
});

虽然我们有安全规则,但是,一些攻击者可以注册一个虚拟帐户并执行类似的操作。如何在 Firestore 中防止这种情况?

非常感谢。

您可以使用 hasOnly.
通过 Firestore 安全规则执行此操作 你会做这样的事情:

allow write: if resource.data.keys().hasOnly([ <your whitelisted properties> ])

如果由于某种原因您不能以这种方式使用规则,或者您需要进行更高级的检查,您可以创建一个带有 Firestore 触发器的 Cloud Function,它可以验证文档并在出现问题时将其删除.