Lesson 1 of 2
File uploads
Listen to an AI summary of File uploads
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.
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.
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:
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:
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
Name the three things about an uploaded file that the customer chooses.
Show the answer
The file name, the content type and the contents.
Which line of the first version of
upload_photosaves the file under the name the customer gave?Show the answer
The last line,
photo.save("public/uploads/" + photo.filename).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.
- Gift photo uploadsGiftwrap, BeginnerOpen
- Avatar uploadChirp, IntermediateOpen
- Hand in homeworkGradebook, BeginnerOpen