I have an Angular web-app with Firebase as the serverless backend technology. The current purpose of the web-app is to let registered users create documents for offered products. While business users can create unlimited product-docs, free users should only be able to create one product-doc.
Collections:
- users -
{id: string, isBusiness: boolean, name: string, email: string, productIds: string[]} - products -
{id: string, title: string, description: string, userId: string}
Whenever a user creates a new product-doc with
public createProduct(id: string, product: Product) {
return this.afStore.collection('products').doc(id).set(product, { merge: true });
}
I run a cloud function with the trigger onCreate to add the id of the created product-doc to the productIds field of the according user-doc. This allows me to check frontend-side if(!user.isBusiness && productIds.length > 1) to limit the creation of multiple product-docs for free user accounts.
However, I recently did a pen-test and the testers accidentality found out that if you open the website on multiple browser tabs and simultaneously run the createProduct() function on each tab at the same time, all of them will execute allowing a non business user to have several product-docs.
Has anybody dealt with a similar situation? I don´t know if there is a way to avoid this with Firestore Security Rules, or if there is something to wait until active transactions are finished.