-
Notifications
You must be signed in to change notification settings - Fork 409
Description
- Operating System version: Cloud Functions environment
- Firebase SDK version: 5.13.0
- Firebase Product: Firestore
Something similar to a SQL injection, would possible with the Firebase Admin SDK in a code like this:
app.post("/getDocument", async (req, res) => {
const ref = firestore.collection("users").doc(userId);
const snap = await ref.get();
const doc = snap.data();
res.status(200).send(JSON.stringify(doc));
});
A user could hack into the system by sending not only the sending the userId but also some additional path to a subdocument, like this: [[userId]]/secrectCollection/[[id]]
.
The code above would return this, even if the user isn't expected to be allowed to retrieve this document. I know I should have checked the Bearer and so on, but let's assume that I've done this, but including a middleware and so on.
There may be some cases where a developer won't expect to that there may be a path to a document and rather only would like to allow an id.
My suggestion would be to allow a path to a document or a collection only in the .collection()
and.doc()
functions of the interface FirebaseFirestore.Firestore
and not on FirebaseFirestore.DocumentReference
and FirebaseFirestore.CollectionReference
So this would be allowed:
VERSION 1:
admin.firestore().doc(`users/${userId}/secrectCollection/${someId}`)
And this will cause an error:
VERSION 2:
admin.firestore().collection("users").doc(`${userId}/secrectCollection/${someId}`)
I personally don't see any drawback on this, besides backwards compatility, as you could always use VERSION 1, which could be more complicated, but I think would make the Firestore in the Admin SDK more secure to prevent accidentially unexpected/unhandled paths.
What do you think?