开发者

Serving uploaded images securely in Django?

开发者 https://www.devze.com 2022-12-23 20:40 出处:网络
My Django site lets u开发者_C百科sers upload images. It\'s running on Apache. Files are uploaded via a FileUpload form. The folder to which files are uploaded is outside the Django project, and prot

My Django site lets u开发者_C百科sers upload images. It's running on Apache.

Files are uploaded via a FileUpload form. The folder to which files are uploaded is outside the Django project, and protected as described here, i.e. the folder has 755 permissions and files have 644 permissions.

I now want to serve the images up to users - but I need to do it securely, so that executable scripts don't run, and so that users can't e.g. delete all the images in the directory.

My question is, how do I serve the uploaded images to users in a secure way? Can I serve them safely as static media directly from that folder, with those permissions? Or should I copy them into another directory with different permissions, and serve them from there?

I'm serving the other static media (/media/css) on the site as a separate, static application.

Thanks!


The way to do this is to configure your web server to serve files with the names it expects, and with a correct image content-type. Use Django's ImageField for some level of validation by PIL/Pillow that uploaded files are images. For this directory, disable webserver features like autogenerating directory indexes, autoserving everything from the filesystem, guessing at mime types, and running cgi scripts.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号