critical · M3 · MASVS-AUTH-1 · PTKD-BACKEND-FIREBASE-SELF-PROMOTE

    Users can make themselves admin or premium in Firestore

    Your Firestore rules decide who is an admin (or premium) by reading a field on the user's own profile document, such as users/{uid}.role. The same rules let each user write their own profile with no limit on which fields change. A user sets role to "admin" on their own document from the app or a script, and every rule that checks that field now lets them in.

    How it is exploited

    firestore.rules has `function isAdmin() { return get(/databases/$(database)/documents/users/$(request.auth.uid)).data.role == 'admin'; }` and `match /users/{userId} { allow read, write: if request.auth.uid == userId; }`. A signed-in user runs `updateDoc(doc(db, 'users', myUid), { role: 'admin' })` from the browser console with the config in the app, and every isAdmin() rule now lets them in.

    Why it matters

    Any user becomes an admin (or premium): they read and change every document the admin rules cover, or unlock paid features for free.

    How to fix it

    1. Find every `get(/databases/$(database)/documents/users/$(request.auth.uid)).data.<field>` in your rules: those fields are trusted.
    2. In the users/{userId} rule, limit what the owner may change: `allow update: if request.auth.uid == userId && request.resource.data.diff(resource.data).affectedKeys().hasOnly(['displayName', 'photoURL']);`.
    3. Do the same for create (a new user must not create their profile with role: 'admin'): `allow create: if request.auth.uid == userId && !('role' in request.resource.data);`.
    4. Better: store roles as custom claims set by a Cloud Function (admin.auth().setCustomUserClaims) and check `request.auth.token.admin == true`.

    firestore.rules

    match /users/{userId} {
      allow read: if request.auth.uid == userId;
      allow create: if request.auth.uid == userId && !('role' in request.resource.data);
      allow update: if request.auth.uid == userId
        && request.resource.data.diff(resource.data).affectedKeys().hasOnly(['displayName', 'photoURL']);
    }

    Does your app have this?

    PTKD checks your build or your source project for PTKD-BACKEND-FIREBASE-SELF-PROMOTE and every other check in this knowledge base.

    Scan your app

    References

    Related checks (M3)