Skip to content

Specify the mode of uploaded files

Mathieu Bridon requested to merge upload-permissions into master

When uploading files, Django has 2 different code paths:

  • if the file is smaller than 2.5MB, it is entirely in memory, then written to the destination path;
  • if the file is bigger than 2.5MB, it is first written in /tmp, then moved to the destination path;

This means that in the first case, the mode of the destination file will be the default for that path, while in the second case it will be the default for /tmp!

As a result, small files were uploaded with their mode set to 666, and big ones to 600.

The latter means that a frontal server like Nginx, which directly serves static and media files, would not be able to read these uploaded files back as it doesn't run as the Ideascube user.

Fortunately, Django allows specifying the mode to use in the settings, and then it will ensure uploaded files always have this mode, no matter how small or big.

However, going further, 666 is also not a great idea as it means any process on the machine can modify these files. Only Ideascube itself should be able to write to these files, but they should be world-readable.

For these reasons, this commit makes us use 644 as the mode for all uploaded files.

Fixes #826 (closed)

Merge request reports