Skip to content

Lesson 1 of 2

File uploads

Listen to an AI summary of File uploads

0:00 / 0:35

One upload to the sports shop

Sam, a customer of the sports shop, signs in and uploads a profile photo named sam.jpg. Sam's browser sends the photo in a request.

  1. POST /photo HTTP/1.1the first line, where POST means the request sends data
  2. filename="sam.jpg"the file name
  3. Content-Type: image/jpegthe content type
  4. (the data of the photo)the contents of the file
Part of Sam's upload request.

The request holds the file name, the content type and the contents. The file name ends in an extension, the letters after the last dot, such as .jpg. A content type is a label that says what kind of data a file holds, such as image/jpeg for a photo.

The person who sends a request writes every line, as What is in a request shows. An attacker can send a file of code and label the file image/jpeg.

What is the web root

The web root is the folder on a server whose files anyone can open through a URL.

  1. shop/the sports shop's folder on the server
  2. app.pythe shop's Python code
  3. public/the web root
  4. logo.pnganyone can open shop.example/logo.png
  5. uploads/a folder inside the web root
  6. sam.jpganyone can open shop.example/uploads/sam.jpg
  7. private/a folder outside the web root
  8. 8f3a9c.jpgno URL opens this file
The web root of the sports shop is the folder public.

The code that saves the photo

The Python code below runs on the shop's server when Sam's request arrives. Look at the name the code saves the file under:

  1. def upload_photo():
  2. photo = request.files["photo"]# reads the uploaded file from the request
  3. photo.save("public/uploads/" + photo.filename)# saves the file under the name the customer gave
The sports shop saves an uploaded photo.

photo.filename is the file name from the request. The last line saves the file under that name in the folder public/uploads, inside the web root. The code checks nothing about the file.

A file named shell.php

PHP is a programming language that web servers can run. The shop's own code is Python. The web server in front of the shop is also set up to run any .php file in the web root as code, as many web servers are.

An attacker signs in and uploads a file of PHP code named shell.php. The shop saves the file as public/uploads/shell.php. The attacker opens the URL shop.example/uploads/shell.php, and the server runs the attacker's code. The code can read every order in the shop. On a server that runs no PHP, an uploaded HTML file with a script still runs in the browser of anyone who opens the file from the shop's own address (Cross-site scripting).

What is an unrestricted file upload

An unrestricted file upload is a feature that accepts any kind of file and stores the file where the file can run. The full name is unrestricted upload of file with dangerous type (CWE-434).

Who chooses each part of an upload

The file name
The customer chooses. The name can end in .php, or match another file and replace that file.
The content type
The customer chooses. The label can be false.
The contents
The customer chooses. A huge file can fill the server's disk, and a file can hold malware.
The stored name
The server chooses. A safer name has nothing from the request.
The folder
The server chooses. A safer folder is outside the web root.

Allowlist and blocklist

The shop's allowlist holds two types of photo, JPEG and PNG.

A blocklist is a list of forbidden values, and any value not on the list is allowed. A blocklist that forbids .php still allows .phtml, which some servers also run as PHP code. A blocklist allows every dangerous type the programmer did not think of. An allowlist refuses every type the programmer did not name.

Finding the real type of a file

The extension and the content type both come from the customer, so neither proves the type. The first few bytes of a file also name a type. An attacker can copy those bytes from a photo onto a file of code.

A stronger check opens the file as a photo with a library. The Python library Pillow stops with an error when a file is not a photo. A new copy that Pillow saves holds only the picture.

A safer version

The safer code below opens the upload with Pillow, chooses a new name and saves a new copy. Look at where the stored name comes from:

  1. def upload_photo():
  2. image = Image.open(request.files["photo"])# stops with an error if the file is not a photo
  3. if image.format not in ["JPEG", "PNG"]:# the allowlist
  4. return "Only JPEG and PNG photos", 400# refuses the upload, and 400 is the code for a request the server cannot accept
  5. name = secrets.token_hex(16) + ".jpg"# a random name that the server chose
  6. image.convert("RGB").save("private/" + name)# sets the colours a JPEG needs and saves a new copy outside the web root
A safer version of the code that saves a photo.

image.format is the type that Pillow found in the contents of the file. The if line refuses any type that is not on the allowlist.

secrets.token_hex(16) returns 32 random characters, so the stored name has nothing from the request. The last line saves a new JPEG copy in the folder private.

The example leaves out a sign-in check and a limit on the size and number of files. A malware scan before other people download a file is a further defence.

What is object storage

Object storage is a service that keeps files outside the server and never runs the files. The shop can send each upload there.

Examples of what to look for

  • A new line that saves an upload under the name from the request, such as photo.save("public/uploads/" + photo.filename).
  • An upload saved in a folder inside the web root.
  • A type check that reads only the extension or the content type, or a blocklist that refuses .php.
  • A photo saved as uploaded, with no library that opens the photo and saves a new copy.
  • An upload feature with no size limit, no sign-in check or no malware scan.
  • A check that runs after the file is saved, which leaves time for an attacker to open the file.

Check your understanding

  1. Name the three things about an uploaded file that the customer chooses.

    Show the answer

    The file name, the content type and the contents.

  2. Which line of the first version of upload_photo saves the file under the name the customer gave?

    Show the answer

    The last line, photo.save("public/uploads/" + photo.filename).

  3. Give one reason why an allowlist of file types is stronger than a blocklist.

    Show the answer

    A blocklist allows every dangerous type the programmer did not think of.

Practise it

Challenges with a weakness this lesson teaches.