firebase / firebase/firebase-js-sdk
Firestore returns permission-denied when node instance restarts even if session cookie is verified
- Dominant language
- TypeScript
- Stars
- 5.1k
- Forks
- 1k
- Avg merge
- 2d 21h
- Merged PRs (30d)
- 37
Description
Environment:
* Operating System version: macOS Mojave 10.14.6
* Browser version: Chrome 80.0.3987.116
* Firebase SDK version: 7.9.1
* Firebase Product: database
### Issue:
I believe I have found a bug with firestore, and have finally found what causes the issue to occur. Essentially firestore **incorrectly** returns a "permission-denied" response despite security rules being correct and the session cookie being verified.
**This happens only in development when the node instance restarts!**
Whether or not the NodeJS server restarts, if verifySessionCookie() passes and the user is logged in there should be no issue fetching documents which require auth from firestore.
### How to reproduce:
I have created a minimal repo which demonstrates this exact issue: https://github.com/Krucial92/firestore-bug-example
Please follow these steps to reproduce:
1) Clone the repo and install dependencies for both the client & server
2) Inside /server/src/private/firebase-cert.json add your private key
3) Update /server/.firebaserc with your project, also go into /server/src/firebase.js and fill out the TODOs
4) Add this basic security rule into firestore:
```
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
// Users
match /users/{userId} {
allow read, write: if request.auth.uid == userId;
}
}
}
```
5) Go into ./client and run `npm start`, also go into ./server and run `npm start`
6) Now in the browser click "Sign Up", this will take a second.
7) Click "Fetch current user", you will see in the network requests that this is successful and you can fetch the users name "Johnny" from firestore.
8) Now restart the NodeJS server (you can also write something and let it hot-reload)
9) Now in the browser again click "Fetch current user" and you will see the network request fails with permission-denied
### Other info
I really hope that this can be fixed soon, as it causes a very confusing developer experience. This lead me to believe that there was something wrong with my application or security rules, but further investigation into the issue seems like it is a problem with firestore.
What is happening on the backend and why would this occur? Appreciate any help or pointers in regards to this issue.
Contributor guide
Research direction
Clone the linked minimal repository and follow its client/server setup, starting with server/src/firebase.js and the session-cookie verification flow. Restart the NodeJS server, repeat the Firestore request, and compare behavior before and after the restart; done means the authenticated user can fetch the protected document without permission-denied.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, nodejs
- Domain
- authentication, databases
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100