Firebase-android-sdk: Error on uploading file to firebase cloud storage: “Listing objects in a bucket is disallowed for rules_version = 1”

Created on 11 May 2019  ·  5Comments  ·  Source: firebase/firebase-android-sdk

[READ] Step 1: Are you in the right place?

Issues filed here should be about bugs in __the code in this repository__.
If you have a general question, need help debugging, or fall into some
other category use one of these other channels:

  • For general technical questions, post a question on StackOverflow
    with the firebase tag.
  • For general Firebase discussion, use the firebase-talk
    google group.
  • For help troubleshooting your application that does not fall under one
    of the above categories, reach out to the personalized
    Firebase support channel.

[REQUIRED] Step 2: Describe your environment

  • Android Studio version: TODO(Samuel Waxman)
  • Firebase Component: Storage
  • Component version: TODO(Samuel Waxman)

[REQUIRED] Step 3: Describe the problem

When trying to upload an object.

"Listing objects in a bucket is disallowed for rules_version = "1". Please update storage security rules to rules_verison = "2" to use list."

Steps to reproduce:

TODO(fredzqm): add more detailed steps.

Reference:

https://stackoverflow.com/questions/56072204/error-on-uploading-file-to-firebase-cloud-storage-listing-objects-in-a-bucket

Relevant Code:

For more information, my upload code (in Kotlin) is

 fun uploadImage(uri: Uri, path: String): Task<Uri> {
            val storageRef = FirebaseStorage.getInstance().reference
            val storagePath = storageRef.child(path)
            return storagePath.putFile(uri).continueWithTask{ storageRef.downloadUrl }
 }

I call it with

public override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        if (requestCode in 0..2 && resultCode == Activity.RESULT_OK) {
            val cropImageUri = CropImage.getActivityResult(data).uri
            val systemTime = System.currentTimeMillis()
            val path = "$userId/$systemTime"
            //IMAGE UPLOAD HERE:
            FirebaseImageResource.uploadImage(cropImageUri, path)
                    .addOnCompleteListener {
                if (it.isSuccessful) {
                    GlideApp.with(this)
                            .load(cropImageUri)
                            .into(imageViewForPosition(requestCode)!!)
                    imageUris[requestCode] = it.result.toString()
                }
            }
        }
    }

My firebase rules are the default:

service firebase.storage {
  match /b/{bucket}/o {
    match /{allPaths=**} {
      allow read, write: if request.auth != null;
    }
  }
}

I'm also successfully authing with Facebook Login

override fun onSuccess(loginResult: LoginResult) {
                val credential = FacebookAuthProvider.getCredential(loginResult.accessToken.token)
                auth.signInWithCredential(credential)
            }

(It lacks a success listener right now, but I know it's working because when I don't use it, I get an unauthorized error instead, and the file doesn't actually upload)

storage

Most helpful comment

Hi! I also encountered the issue with similar code (but I used my reference instead of the path), but fixed it by modifying my rules and prepending rules_version = '2';

Like this:

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

All 5 comments

Android Studio Version: 3.4

All of my firebase dependencies are listed below

    implementation 'com.google.firebase:firebase-core:16.0.1'
    implementation 'com.google.firebase:firebase-auth:17.0.0'
    implementation 'com.google.firebase:firebase-messaging:18.0.0'
    implementation 'com.google.firebase:firebase-storage:17.0.0'
    implementation 'com.firebaseui:firebase-ui-storage:4.3.2'

your trying to get URL from root reference

Just replace your downloadUrl code from

storageRef.downloadUrl
to
storagePath.downloadUrl

When a empty object path is used for get, it becomes a recursive list at the root of bucket, and generates a confusing error message.
For what it worth, I don't think there is a way for the error message to be better.

List example:
curl "https://firebasestorage.googleapis.com/v0/b/my-bucket/o?prefix=folder%2F&deimiter=/"

curl "https://firebasestorage.googleapis.com/v0/b/my-bucket/o/folder%2Ffile"

Hi! I also encountered the issue with similar code (but I used my reference instead of the path), but fixed it by modifying my rules and prepending rules_version = '2';

Like this:

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

I believe this problem happens because a get request on the root of a bucket is indistinguishable from a list request.

I am adding a validation to getDownloadUrl.

Was this page helpful?
0 / 5 - 0 ratings