Home:ALL Converter>Firebase storage, onFinalize function triggered even though permission was denied

Firebase storage, onFinalize function triggered even though permission was denied

Ask Time:2021-12-09T15:16:16         Author:Lucas

Json Formatter

I am trying to make an android app with firebase, in which users can only upload when the server approved it.

So, I created firebase storage rules to forbid users from creating files, they are only allowed to update. Then, when uploading is approved, the server creates an empty file users can update.

Now I have the problem that onFinalize is triggered even though permission was denied (confirmed in android, task fails with permission denied error).

Should I be verifying in the callback itself that permissions were granted? I find this pretty misleading, or I am doing something wrong...

storage.rules :

rules_version = '2';
service firebase.storage {
  match /b/{bucket}/o {
    match /{allPaths=**} {
      allow read, write: if false;
    }

    function isSignedIn() {
      return request.auth != null;
    }

    match /jobFiles/{userId}/{jobId} {
      function uploadAuthorized() {
        return request.resource.size < 128 * 1024 * 1024;   
      }
      allow update: if isSignedIn() && uploadAuthorized();
    }
  }
}

index.js


exports.jobFile = functions.storage.object()
.onFinalize(async (object) => {
    const fileBucket = object.bucket; // The Storage bucket that contains the file.
    const filePath = object.name; // File path in the bucket.
    const contentType = object.contentType; // File content type.
    const metadata = object.metadata

    log("File finalized...")
...
});

EDIT Android upload:

        val task = storage
            .child("jobFiles")
            .child(uid)
            .putFile(uri, metadata)

        task
            .addOnSuccessListener {
                ...
            }
            .addOnFailureListener {
                // Permission denied error caught here
            }

Exception: exception enter image description here

Firebase log:

i  functions: Beginning execution of "us-central1-jobFile"
>  {"0":"File finalized...","severity":"INFO","message":""}

Author:Lucas,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/70285984/firebase-storage-onfinalize-function-triggered-even-though-permission-was-denie
yy