Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase Storage, What's the proper rules for user based uploading/deleting?

I want to create user based security for Firebase/Storage. Below allow write works well if I do only upload images. But It prevents deleting a photo. How can I create proper security rule to this case?

service firebase.storage {
  match /b/<bucket>/o {
    match /{allPaths=**} {
      allow read: if request.auth != null;
    }
    match /users/{uid}/{filename} {
      allow write: if isCurrentUser(uid);
      allow write: if isImage() &&
                      isCurrentUser(uid) &&
                      lessThanNMegabytes(n) &&
                      request.resource !=null &&
                      filename.size() < 50;
    }
  }
}

function isCurrentUser(uid) {
    return request.auth.uid == uid;
}

function lessThanNMegabytes(n) {
    return request.resource.size < n * 1024 * 1024;
}

function isImage() {
    return request.resource.contentType.matches("image/.*");
}
like image 250
iamburak Avatar asked Jul 02 '16 07:07

iamburak


1 Answers

I would use this to check if you are creating/updating a file or removing it

match /users/{uid}/{filename} {
    allow write: if isCurrentUser(uid);
    allow write: if resource == null ||
         ( isImage() &&
         lessThanNMegabytes(n) &&
         request.resource !=null &&
         filename.size() < 50 );
}
like image 186
Devid Farinelli Avatar answered Nov 11 '22 03:11

Devid Farinelli