IOError: Errno 13 Permission denied for specific files
Question: I'm running a flask API which can be used to upload jpg files. It's been
working fine for about a year, but today, out of the blue, uploaded files are
rejected for a specific user of the API. There's actually not any difference
in the treatment of the request from one user to another, but the same file
gets rejected for user A and accepted for user B, and user A never had this
problem before. It's driving me crazy !

Here's the code in the flask API :

    
    
    from werkzeug.utils import secure_filename
    
    ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])
    
    def allowed_file(filename):
        return '.' in filename and filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
    
    
    @app.route('/uploadFile/', methods=['POST'])
    @auth.login_required
    def newImage():
        if request.method == 'POST':
            if request.files:
                file = request.files['file']
                if allowed_file(file.filename):
                    ImageId = request.args.get('ImageId')
                    newImage = DB_Image()
                    newImage.ObjectId = ImageId
    
                    filename = secure_filename(file.filename)
                    pictureName, fileExtension = os.path.splitext(filename)
                    fullFileNameToBeSaved = str(newImage.ObjectId) + fileExtension
                    imagesPath = os.path.join(app.static_folder, 'images')
    
                    file.save(os.path.join(imagesPath, fullFileNameToBeSaved))
                    (...)
    

Everything works fine until the file.save, where I get the following error
when user A tries to upload a file:

    
    
    mod_wsgi (pid=14170): Exception occurred processing WSGI script '/var/www/myApi/wsgi/myapivenv.wsgi'.
    Traceback (most recent call last):
       File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1836, in __call__
         return self.wsgi_app(environ, start_response)
       File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1820, in wsgi_app
         response = self.make_response(self.handle_exception(e))
       File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1403, in handle_exception
         reraise(exc_type, exc_value, tb)
       File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1817, in wsgi_app
         response = self.full_dispatch_request()
       File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1477, in full_dispatch_request
         rv = self.handle_user_exception(e)
       File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1381, in handle_user_exception
         reraise(exc_type, exc_value, tb)
       File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1475, in full_dispatch_request
         rv = self.dispatch_request()
       File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1461, in dispatch_request
         return self.view_functions[rule.endpoint](**req.view_args)
       File "/usr/local/lib/python2.7/dist-packages/flask_httpauth.py", line 61, in decorated
         return f(*args, **kwargs)
       File "/var/www/myApi/code/ImageUpload.py", line 227, in newImage
         file.save(os.path.join(imagesPath, fullFileNameToBeSaved))
       File "/usr/local/lib/python2.7/dist-packages/werkzeug/datastructures.py", line 2576, in save
         dst = open(dst, 'wb')
     IOError: [Errno 13] Permission denied: '/var/www/path_to_upload_folder/files/images/2a6d85db-7ef0-40e3-8031-b7ed490bc512.jpg'
    

The permissions were set when I installed the API, for the unix user created
specifically to run the API :

    
    
    $ sudo usermod -a -G myApi $USER
    $ sudo chown -R $USER:myApi var/www/path_to_upload_folder/ path_to_upload_folder/files/images
    $ sudo chmod -R g+w var/www/path_to_upload_folder/ path_to_upload_folder/files/images
    

But the users of the API are managed in a database, they have nothing to do
with unix users, so I highly doubt the problem is from there.

What could cause an error like this on some specific files sent by a specific
user ?

I'm using Werkzeug 0.9.4 and flask 0.10.1.


Answer: I did some more digging, and it turns out I was trying to overwrite some files
that were previously added by the root user (the myApi user didn't have
overwrite permissions on them).

Checking if the problematic files were already on the server was the first
thing I did, but I did it by searching for the files with the Finder and
Transmit (on Mac), which was apparently not as efficient as using the "ls"
command, in a folder containing hundreds of files.

Using "ls -l" I saw that the problematic files were owned by the root user,
and not the myApi user (I actually had to move those files from one server to
this one some time ago, which explains why the myApi user is not the owner).

Now I'll try to find a UNIX command to change the permissions of every file
added by the root user in that folder, or change the permissions of the myApi
user so it can overwrite any file in that folder...

Edit: I changed the permissions using

    
    
    $sudo chown -R myApi:myApi files/images
    

