Dev.to Security 🔐 Cybersecurity 👁 0 📖 6 min read

I attacked 11 Firebase security rules in the emulator. Here is what got through

TL;DR. request.auth != null let a stranger read, change and delete everyone's orders. An owner check on resource alone let a user hand an order to someone else. One broad {document=**} match opened a collection that had

I attacked 11 Firebase security rules in the emulator. Here is what got through

TL;DR. request.auth != null let a stranger read, change and delete everyone's orders. An owner check on resource alone let a user hand an order to someone else. One broad {document=**} match opened a collection that had a correct owner rule. And a role stored in a document the user can write made them admin in one request. Each one has a fix below.

A Firebase app has no server of yours between the browser and the database. The project config ships in the page, so security rules are the only thing between anyone holding it and your data. Most lists of rule examples show you a rule that looks right. I wanted the attack next to the rule, so I ran it.

I used the Firebase Local Emulator Suite from CLI 15.34.2 on 3 November 2026. For each of 11 rulesets I reset the data and called the Firestore REST API with real tokens from the Auth emulator. No token, an anonymous user, users A and B, and a stranger who only signed up. Fake data, orders a1 and a2 owned by A and b1 owned by B.

Which rules leaked

Rule Request Result
if true (test mode) Guest lists orders 200, all 3
if true (test mode) Guest deletes B's order 200
request.auth != null Anonymous user lists orders 200, all 3
request.auth != null Stranger deletes A's order 200
Owner check on resource only A creates an order owned by B 200
Owner check on resource only A changes ownerId of own order 200
Owner rule plus {document=**} read A reads B's order 200
User can write their own users doc A sets role: admin, reads B's order 200

Test mode looks like this.

match /{document=**} {
  allow read, write: if true;
}

It opens everything to everyone. A guest with no token listed everything, deleted one order and created a new one. All you need is the project id.

The dated version has a different failure. With if request.time < timestamp.date(2026, 1, 1) and that date in the past, the guest got 403, and so did user A reading their own order. The leak is over and so is your app.

request.auth != null is not access control

match /orders/{id} {
  allow read, write: if request.auth != null;
}

The guest got 403, which is why this feels safe when you test it quickly. Then a stranger who had only signed up listed all 3 orders, changed the total on B's order and deleted A's order, each with 200. An anonymous user listed all 3 too. If sign up is open, this rule means everyone who bothers to register.

An owner check needs both resource and request.resource

resource is the document as stored. request.resource is the document after the write. Check only the first and you know who owns the row now, but nothing about what they turn it into.

match /orders/{id} {
  allow read, update, delete: if resource.data.ownerId == request.auth.uid;
  allow create: if request.auth != null;
}

Reads looked right. A got 200 on their own order and 403 on B's. Writes didn't. A created a new order with ownerId set to B and got 200. Then A changed ownerId on their own order to B, also 200, and B could read it from then on. If orders mean invoices or balances, that's a user writing into someone else's account.

This one refused all of it.

match /orders/{id} {
  allow read, delete: if request.auth != null
    && resource.data.ownerId == request.auth.uid;
  allow create: if request.auth != null
    && request.resource.data.ownerId == request.auth.uid;
  allow update: if request.auth != null
    && resource.data.ownerId == request.auth.uid
    && request.resource.data.ownerId == resource.data.ownerId;
}

It still let A change the total on their own order. If the client must not set that field, list what it may change.

allow update: if request.auth != null
  && resource.data.ownerId == request.auth.uid
  && request.resource.data.diff(resource.data).affectedKeys().hasOnly(['note']);

Now changing total returned 403 and changing note returned 200.

Your query fails because rules are not filters

Under that owner rule, A asking for the whole collection got 403, even though A owns 2 of the 3 orders. A query with ownerId == A returned 200 with 2 docs. A query with ownerId == B got 403. Firestore judges a query by what it could return, and it doesn't look at what is really there.

So when a list breaks after you tighten rules, fix the query. Loosening the rule to make the list work is how the broad rules above get written.

A broad match cancels a correct rule

This is the one I'd look for first in a long rules file, because Firestore allows a request if any matching rule allows it.

match /orders/{id} {
  allow read: if request.auth != null
    && resource.data.ownerId == request.auth.uid;
}
match /{document=**} {
  allow read: if request.auth != null;
}

The first block is a good owner rule. With the second block there, A read B's order with 200, a stranger listed all 3 orders, and the same stranger read A's document in users. There is no way for a later rule to take access back, so delete the broad match.

Admin in one request

This pattern shows up whenever roles live in a document.

function isAdmin() {
  return get(/databases/$(database)/documents/users/$(request.auth.uid)).data.role == 'admin';
}
match /users/{uid} {
  allow read, write: if request.auth.uid == uid;
}
match /orders/{id} {
  allow read: if resource.data.ownerId == request.auth.uid || isAdmin();
}

"Users can edit their own profile" and "admins can read all orders" are both reasonable. Together they're a privilege escalation.

Admin in one request. User A reads B's order and gets 403, writes role admin on their own document and gets 200, then reads B's order and gets 200.
My test in the Firebase emulator (CLI 15.34.2), 3 November 2026.

A read B's order, got 403. A set their own role to admin, 200. A read B's order again, 200. Adding request.resource.data.role == resource.data.role to the write rule turned that write into 403. Custom claims set from a server work too, a client can't change those.

Two things that turned out fine

Rules on a parent don't leak into subcollections. With allow read on /orders/{id} for any signed in user, A read the order, and /orders/b1/notes/n1 under it returned 403.

A guest doesn't slip through a comparison with a missing token either. resource.data.ownerId == request.auth.uid returned 403 for the guest even without a separate request.auth != null. I still write the check out so the rule says what it means.

How to check yours

Run your requests in the emulator as a guest, a stranger and a second user. That is what exposed every case above, reading the rules didn't.

For a first pass I built a free Firebase rules checker that reads pasted rules without touching your project. On these rulesets it flagged test mode, sign in only access, the broad match and the self written role. It reads text only, so it can't tell you what your data looks like or whether a rule was deployed.

Your apiKey in the page isn't the problem by itself, Firebase web config is public by design. Files have different rules and a leak rules can't stop, which I wrote up in Firebase Storage rules.

Limits of this run. Firestore only, local emulator, over REST, fake data. I didn't test the Realtime Database, which has another rule language. App Check limits which apps may call your backend, and it doesn't replace rules. Production may differ from the emulator in details I didn't check.

Originally published at madsalt.tech. Written with AI assistance, checked by me.

📰 Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.