firebase / firebase/firebase-tools
Rules using resource.data run twice in Firestore emulator
- Dominant language
- TypeScript
- Stars
- 4.5k
- Forks
- 1.3k
- Avg merge
- 1d 12h
- Merged PRs (30d)
- 84
Description
Rules that use `resource.data` appear to be evaluated twice in the Firestore emulator. The first time always fails with an error, but the second one succeeds and allows/denies access as expected. This feels similar to #4325, but doesn't involve the use of `onSnapshot` (although I observed the same issue when using `onSnapshot` instead of `getDoc`).
Everything appears correct from the client side - the code doesn't appear to run twice, nor are any errors returned from the request. My rules using `resource.data` appear to work correctly when deployed to Firebase and don't show any errors in the Firebase console.
### [REQUIRED] Environment info
**firebase-tools:** 12.4.6
**Platform:** Windows 11
### [REQUIRED] Test case
Rules:
```
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /projects/{projectId} {
allow read: if (request.auth != null) && (request.auth.uid != null) && (resource.data.owner == request.auth.uid);
}
}
}
```
Firestore data at `/projects/doc_id_here` (replacing `doc_id_here` with the ID of the doc and `uuid_here` with the UUID of the currently authenticated user):
```
{ owner: "uuid_here" }
```
Screenshot from Firestore emulator:
### [REQUIRED] Steps to reproduce
Set up a project with the rules and data listed above, then make a request to it from the JS client SDK. For example:
```js
import {doc, getDoc, getFirestore, connectFirestoreEmulator, Firestore} from 'firebase/firestore';
const firebaseApp = { /* Firebase app config here */ }
const firestore = getFirestore(firebaseApp);
connectFirestoreEmulator(firestore, location.hostname, 8080);
export function getProject(projectId) {
getDoc(doc(firestore, `projects/${projectId}`))
}
```
### [REQUIRED] Expected behavior
The Firestore emulator shows one entry with the result of the request on the Requests tab.

### [REQUIRED] Actual behavior
The Firestore emulator shows two entries for each request; the first one failing with an unknown error and the second one successfully evaluating the rules.

Click on the request to view details. There's an error icon at the top of the table and a green checkmark on the line containing the related rule, and the `resource` section under "Detailed information" in the right column reads `(Error: unknown) (undefined)`:

Contributor guide
Assessment
This issue has not been assessed yet.