The contact form on this site writes straight to Firestore from the browser. There is no server in between, which sounds dangerous: the Firebase config is public, so anyone can send requests to the database. What keeps it safe is the security rules. This is how they are written and why.
The config is not a secret
A Firebase web config contains an API key, a project ID and a few other identifiers. They are in every visitor's browser by design. They say which project to talk to, not what the caller may do. Access is decided entirely by rules that run on Google's servers for every request.
So the question is never "how do I hide the config" but "what is the smallest thing an anonymous visitor needs to be able to do?"
Start from deny
match /{document=**} {
allow read, write: if false;
}
Everything is closed unless a more specific rule opens it. A new collection added later is private by default instead of public by accident.
Allow create, and nothing else
A contact form needs exactly one operation: adding a document.
match /mail/{id} {
allow create: if <conditions>;
allow read, update, delete: if false;
}
write would be wrong here, because it includes update and delete. Splitting it means a visitor can add a message but can never list other people's messages, edit one or remove one.
Validate the document
request.resource.data is the document the client is trying to write. The rules can inspect it before it is stored.
allow create: if
request.resource.data.keys().hasOnly(['to', 'replyTo', 'message'])
&& request.resource.data.to == 'owner@example.com'
&& request.resource.data.replyTo is string
&& request.resource.data.replyTo.size() < 200
&& request.resource.data.replyTo.matches('^[^@ ]+@[^@ ]+[.][^@ ]+$')
&& request.resource.data.message.keys().hasOnly(['subject', 'text'])
&& request.resource.data.message.subject.size() < 200
&& request.resource.data.message.text is string
&& request.resource.data.message.text.size() < 5000;
Each line closes a specific hole:
hasOnlyrejects extra fields, so nobody can attach data the app does not expect.- The fixed recipient matters most. These documents trigger an email, and without this check the form would be an open relay that sends mail to any address.
- Type checks stop a client from sending a map or a number where a string is expected.
- Size limits cap how much a single request can store.
What rules cannot do
Rules validate one request at a time. They do not rate-limit, so a script could still create many valid messages. For a public form, add Firebase App Check, which only lets requests through from your real site, and consider a daily budget alert on the project.
Rules also do not sanitize. Treat the stored text as untrusted wherever it is displayed later.
Test before deploying
The Firebase Emulator Suite runs Firestore locally with your rules. Write tests for what should fail, not only for what should work:
- a document with an extra field
- a different recipient
- a 10,000-character message
- a read of the collection
If any of those succeed, the rules have a gap. Rules are the only thing standing between the public internet and the data, so they deserve the same tests as any other code.