firebase / firebase/firebase-tools

Rules using resource.data run twice in Firestore emulator

Open
#6,252 24 comments 26 reactions 0 assignees View on GitHub
emulators: firestore type: bug
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:Firestore emulator screenshot

### [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.

![Screenshot 2023-08-13 140609](https://github.com/firebase/firebase-tools/assets/4888172/1967a046-6f6b-467f-8dd8-b3bc3f6fccc2)

### [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.

![Screenshot 2023-08-13 140651](https://github.com/firebase/firebase-tools/assets/4888172/0e108b91-fdea-464f-9620-8dc11e555431)

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)`:

![Screenshot 2023-08-13 140915](https://github.com/firebase/firebase-tools/assets/4888172/a2811c96-1d82-49cc-8b52-4e6e6dcf6008)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.