Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

125
Views
Preventing a user from adding a different fields when updating a document in firestore?

I am trying to prevent a user from creating spam fields inside a document, fields that are different form that state of the original doc (after it has been created) my current rules looks like the following :

//Create
allow create : if request.auth != null  
&& request.auth.uid == userId
&& request.resource.data.keys().hasOnly(
  ['clients', 'verifiedEmail', 'accountStatus' ,'createdAt', 'endsAt'])
 
allow read: if request.auth != null && request.auth.uid == userId          

allow update : if request.auth.uid == userId 
&& request.auth.uid == userId
&& request.resource.data.keys().hasAny(
 ['clients', 'verifiedEmail', 'accountStatus' ,'createdAt', 'endsAt']])

The idea here is in one instance I will want to update clients

  await updateDoc(doc(db, 'Data', userCredential.user.uid), {
    clients: //data,
  });

in other instance I want to updated verified Email. while prevent a user from creating a filed like

await updateDoc(doc(db, 'Data', userCredential.user.uid), {
    shouldnotbecreated: //some spam,
});

In the Rules update chunk

if I use hasOnly() it is so strict that it need the whole payload to be the same. as in ['clients', 'verifiedEmail', 'accountStatus' ,'createdAt', 'endsAt'].

If I use hasAll() or hasAny() a spam filed gets created.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

It worked for my by using a combination of request.resource.data.diff(resource.data).affectedKeys().hasOnly()

//Create
allow create : if request.auth != null  
&& request.auth.uid == userId
&& request.resource.data.keys().hasOnly(
  ['clients', 'verifiedEmail', 'accountStatus' ,'createdAt', 'endsAt'])
 
allow read: if request.auth != null && request.auth.uid == userId          

allow update : if request.auth.uid == userId 
&& request.auth.uid == userId
&& request.resource.data.diff(resource.data).affectedKeys().hasOnly(
 ['clients', 'verifiedEmail', 'accountStatus' ,'createdAt', 'endsAt']])
 

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!