N Kaushik

FirebaseFirestoreException- PERMISSION_DENIED- Missing or insufficient permissions

November 30, 2021

FirebaseFirestoreException: PERMISSION_DENIED: Missing or insufficient permissions:

This is a common issue you will face for any Firebase product. This might be thrown for Firestore, Realtime database or Firebase storage. This exception was thrown in my Android project and I was using Kotlin for that project.

Full exception:

Below is the full exception.

2021-11-30 19:47:11.353 11339-11388/com.nkd.myapplication W/Firestore: (24.0.0) [WriteStream]: (29f59da) Stream closed with status: Status{code=PERMISSION_DENIED, description=Missing or insufficient permissions., cause=null}.

2021-11-30 19:47:11.386 11339-11388/com.nkd.myapplication W/Firestore: (24.0.0) [Firestore]: Write failed at subscribers/username@email.com: Status{code=PERMISSION_DENIED, description=Missing or insufficient permissions., cause=null}
2021-11-30 19:47:11.390 11339-11339/com.nkd.myapplication D/CompatibilityChangeReporter: Compat change id reported: 147798919; UID 10154; state: ENABLED
2021-11-30 19:47:11.400 11339-11339/com.nkd.myapplication W/System.err: com.google.firebase.firestore.FirebaseFirestoreException: PERMISSION_DENIED: Missing or insufficient permissions.
2021-11-30 19:47:11.400 11339-11339/com.nkd.myapplication W/System.err:     at com.google.firebase.firestore.util.Util.exceptionFromStatus(Util.java:117)
2021-11-30 19:47:11.401 11339-11339/com.nkd.myapplication W/System.err:     at com.google.firebase.firestore.core.SyncEngine.notifyUser(SyncEngine.java:575)
2021-11-30 19:47:11.401 11339-11339/com.nkd.myapplication W/System.err:     at com.google.firebase.firestore.core.SyncEngine.handleRejectedWrite(SyncEngine.java:457)
2021-11-30 19:47:11.401 11339-11339/com.nkd.myapplication W/System.err:     at com.google.firebase.firestore.core.MemoryComponentProvider$RemoteStoreCallback.handleRejectedWrite(MemoryComponentProvider.java:114)
2021-11-30 19:47:11.401 11339-11339/com.nkd.myapplication W/System.err:     at com.google.firebase.firestore.remote.RemoteStore.handleWriteError(RemoteStore.java:729)
2021-11-30 19:47:11.402 11339-11339/com.nkd.myapplication W/System.err:     at com.google.firebase.firestore.remote.RemoteStore.handleWriteStreamClose(RemoteStore.java:685)
2021-11-30 19:47:11.402 11339-11339/com.nkd.myapplication W/System.err:     at com.google.firebase.firestore.remote.RemoteStore.access$600(RemoteStore.java:53)
2021-11-30 19:47:11.402 11339-11339/com.nkd.myapplication W/System.err:     at com.google.firebase.firestore.remote.RemoteStore$2.onClose(RemoteStore.java:206)
2021-11-30 19:47:11.402 11339-11339/com.nkd.myapplication W/System.err:     at com.google.firebase.firestore.remote.AbstractStream.close(AbstractStream.java:356)
2021-11-30 19:47:11.402 11339-11339/com.nkd.myapplication W/System.err:     at com.google.firebase.firestore.remote.AbstractStream.handleServerClose(AbstractStream.java:410)
2021-11-30 19:47:11.402 11339-11339/com.nkd.myapplication W/System.err:     at com.google.firebase.firestore.remote.AbstractStream$StreamObserver.lambda$onClose$3$AbstractStream$StreamObserver(AbstractStream.java:151)
2021-11-30 19:47:11.402 11339-11339/com.nkd.myapplication W/System.err:     at com.google.firebase.firestore.remote.AbstractStream$StreamObserver$$ExternalSyntheticLambda2.run(Unknown Source:4)
2021-11-30 19:47:11.403 11339-11339/com.nkd.myapplication W/System.err:     at com.google.firebase.firestore.remote.AbstractStream$CloseGuardedRunner.run(AbstractStream.java:67)
2021-11-30 19:47:11.403 11339-11339/com.nkd.myapplication W/System.err:     at com.google.firebase.firestore.remote.AbstractStream$StreamObserver.onClose(AbstractStream.java:137)
2021-11-30 19:47:11.403 11339-11339/com.nkd.myapplication W/System.err:     at com.google.firebase.firestore.remote.FirestoreChannel$1.onClose(FirestoreChannel.java:142)
2021-11-30 19:47:11.403 11339-11339/com.nkd.myapplication W/System.err:     at io.grpc.internal.DelayedClientCall$DelayedListener$3.run(DelayedClientCall.java:463)
2021-11-30 19:47:11.403 11339-11339/com.nkd.myapplication W/System.err:     at io.grpc.internal.DelayedClientCall$DelayedListener.delayOrExecute(DelayedClientCall.java:427)
2021-11-30 19:47:11.403 11339-11339/com.nkd.myapplication W/System.err:     at io.grpc.internal.DelayedClientCall$DelayedListener.onClose(DelayedClientCall.java:460)
2021-11-30 19:47:11.403 11339-11339/com.nkd.myapplication W/System.err:     at io.grpc.internal.ClientCallImpl.closeObserver(ClientCallImpl.java:557)
2021-11-30 19:47:11.403 11339-11339/com.nkd.myapplication W/System.err:     at io.grpc.internal.ClientCallImpl.access$300(ClientCallImpl.java:69)
2021-11-30 19:47:11.404 11339-11339/com.nkd.myapplication W/System.err:     at io.grpc.internal.ClientCallImpl$ClientStreamListenerImpl$1StreamClosed.runInternal(ClientCallImpl.java:738)
2021-11-30 19:47:11.404 11339-11339/com.nkd.myapplication W/System.err:     at io.grpc.internal.ClientCallImpl$ClientStreamListenerImpl$1StreamClosed.runInContext(ClientCallImpl.java:717)
2021-11-30 19:47:11.404 11339-11339/com.nkd.myapplication W/System.err:     at io.grpc.internal.ContextRunnable.run(ContextRunnable.java:37)
2021-11-30 19:47:11.404 11339-11339/com.nkd.myapplication W/System.err:     at io.grpc.internal.SerializingExecutor.run(SerializingExecutor.java:133)
2021-11-30 19:47:11.404 11339-11339/com.nkd.myapplication W/System.err:     at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:462)
2021-11-30 19:47:11.404 11339-11339/com.nkd.myapplication W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:266)
2021-11-30 19:47:11.404 11339-11339/com.nkd.myapplication W/System.err:     at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:301)
2021-11-30 19:47:11.404 11339-11339/com.nkd.myapplication W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
2021-11-30 19:47:11.404 11339-11339/com.nkd.myapplication W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
2021-11-30 19:47:11.405 11339-11339/com.nkd.myapplication W/System.err:     at com.google.firebase.firestore.util.AsyncQueue$SynchronizedShutdownAwareExecutor$DelayedStartFactory.run(AsyncQueue.java:234)
2021-11-30 19:47:11.405 11339-11339/com.nkd.myapplication W/System.err:     at java.lang.Thread.run(Thread.java:923)
2021-11-30 19:47:11.405 11339-11339/com.nkd.myapplication W/System.err: Caused by: io.grpc.StatusException: PERMISSION_DENIED: Missing or insufficient permissions.
2021-11-30 19:47:11.406 11339-11339/com.nkd.myapplication W/System.err:     at io.grpc.Status.asException(Status.java:543)
2021-11-30 19:47:11.406 11339-11339/com.nkd.myapplication W/System.err:     at com.google.firebase.firestore.util.Util.exceptionFromStatus(Util.java:115)

Reason and solution:

As I have mentioned you before, the cause of this type of Firebase error is the permission. When you create a Firebase project, It makes the project public for one month since you create that project. Also, it enables read and write for that project true, i.e. anyone can read or write data on that project without any authentication.

This is actually dangerous and this is the reason Firebase resets the rule after one month.

These rules are different for different products, e.g. you can set different rule for Firebase Firestore and different rule for Firebase realtime database and different rule for Firebase Database.

If you click on any of these options, there is a tab called Rules that shows the current active rule.

It looks as like below:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if
          request.time < timestamp.date(2021, 12, 30);
    }
  }
}

This is for a Firestore.

From the above snippet, you can understand that read and write access for the Firestore is allowed until 30th december, 2021.

This setting is not recommended in production apps and that is the reason Google provides a way to stop it after a specific time.

This is the most common reason of the above error. You can edit this settings directly. If you edit something, it will show you a popup:

Firebase rules update

You can hit the Publish button to publish it. It should remove this error.

Note:

It throws an error with similar message for other Firebase SDKs as well. You can also replace the condition with true to enable it always without a time-limit.


Subscribe to my Newsletter