Skip to content

Lesson 2 of 2

Paths and file names

Listen to an AI summary of Paths and file names

0:00 / 0:29

What is a file path

A file path is the list of folders that leads to a file. The sports shop website saves a receipt for each order as a file. The picture cuts the file path of one receipt into pieces.

/srv/receipts/1042.pdf

  1. /the top folder of the disk
  2. srva folder
  3. receiptsa folder inside srv
  4. 1042.pdfthe file
The file path of the receipt for order 1042.

An absolute path is a file path that starts from the top folder of the disk. On the shop's server, an absolute path starts with /.

  1. /the top folder of the disk
  2. etc/a folder of settings for the server
  3. passwda file that lists the accounts on the server
  4. srv/a folder for the files of websites
  5. receipts/the folder for receipts
  6. 1042.pdfthe receipt for order 1042
A name that ends in / is a folder. A name set further right is inside the folder above.

What do two dots mean in a path

In a file path, two dots mean the folder above. The file path /srv/receipts/.. leads to the folder /srv. The picture follows the file path /srv/receipts/../../etc/passwd one step at a time.

  1. /srv/receipts

    the receipts folder

  2. ../

    /srv

    one folder up

  3. ../

    /

    the top folder

  4. etc/passwd

    /etc/passwd

    a file outside the receipts folder

The file path /srv/receipts/../../etc/passwd leads to the file /etc/passwd.

The receipt page in the sports shop

Sam buys a hoodie, and the shop saves the receipt as 1042.pdf in the folder /srv/receipts. The link to the receipt is shop.example/receipt?file=1042.pdf. The part after the question mark is a query parameter named file.

Sam's browser sends the link to the server in a request. The server runs four lines of Python. Follow the file name from line 2 to line 4:

def receipt():
    name = request.args["file"]
    path = "/srv/receipts/" + name
    return send_file(path)

Line 2 reads the query parameter file from the request, so name holds 1042.pdf. Line 3 joins the folder and the name into the file path /srv/receipts/1042.pdf. Line 4 passes the file path to send_file, a function that opens the file and sends the contents to the browser.

The source and the sink

Line 2 is the source, where the file name comes in. Line 4 is the sink, where the code opens the file. No line between the two checks the file name.

What is path traversal

The sender of a request chooses every query parameter. An attacker changes the address to shop.example/receipt?file=../../etc/passwd. Line 3 makes the file path /srv/receipts/../../etc/passwd, and line 4 sends the file /etc/passwd to the attacker.

Path traversal (CWE-22) is a file name from a request that leads out of the intended folder. An attacker can read any file the website can read, such as the website's code. Where the code saves or deletes a file, the attacker can change or delete files.

External control of a file name or path (CWE-73) is a request choosing which file the code reads, saves or deletes.

The Python function os.path.join also joins a folder and a name. Given the absolute path /etc/passwd as the name, os.path.join drops the folder and returns /etc/passwd.

Why deleting the dots is not enough

One idea for a fix is a line that deletes every ../ from the name. An attacker sends the name ....//....//etc/passwd.

....//

  1. ..stays
  2. ../deleted
  3. /stays
The code deletes the middle ../. The pieces that stay, .. and /, join into ../.

The name after the deletion is ../../etc/passwd, and the attack works as before.

Resolve the path then check the folder

Resolving a path is working out the one real location that a file path points to, after every ../ is followed.

  1. 1.Resolve the path

    follow every ../ to the real location

  2. 2.Check the folder

    the real location must be inside /srv/receipts

  3. 3.Open the file

    only when the check passes

The steps of the fix, in order.

Lines 3 and 4 of the safer receipt code are the two steps:

def receipt():
    name = request.args["file"]
    real = os.path.realpath("/srv/receipts/" + name)
    if not real.startswith("/srv/receipts/"):
        abort(404)
    return send_file(real)

The Python function os.path.realpath in line 3 resolves the path, so /srv/receipts/../../etc/passwd becomes /etc/passwd. Line 4 tests whether the resolved path starts with /srv/receipts/. /etc/passwd does not, so line 5 ends the request with "not found".

The check comes after resolving. The unresolved path /srv/receipts/../../etc/passwd starts with /srv/receipts/, so a check made before resolving lets the attack through.

Line 4 needs the / after receipts. Without the /, the folder /srv/receipts-old passes the check, because /srv/receipts-old starts with /srv/receipts. The safer code stops path traversal and nothing more. Sam can still ask for 1043.pdf, so the receipt page needs an ownership check as well (Object references).

What is a symbolic link

A symbolic link is a file that points to another file or folder. A symbolic link in the receipts folder can point outside the folder. os.path.realpath follows symbolic links too. Following a link out of the folder with no check is improper link resolution before file access (CWE-59).

Examples of other fixes

  • Take an order number from the request, not a file name. The code turns the value into a whole number with int() and builds the file path.
  • Allow only letters, digits, hyphens and one dot in a name, which is an allowlist.

Examples of what to look for

  • A file name from a request joined to a folder, with + or os.path.join, and used to open, save, delete or send a file.
  • A line that deletes ../ from a name, such as name.replace("../", "").
  • A folder check on a path that is not yet resolved.
  • A startswith check with no / at the end of the folder.

Four example grep searches for Python. Each search finds a function that builds a file path or works on a file, such as os.remove, which deletes a file:

Terminal
grep -rn "send_file(" shop/
grep -rn "open(" shop/
grep -rn "os.remove(" shop/
grep -rn "os.path.join(" shop/

Check your understanding

  1. State where the file path /srv/receipts/../../etc/passwd leads.

    Show the answer

    The file /etc/passwd.

  2. Identify the line in the first receipt code that joins the file name to a folder.

    Show the answer

    Line 3, which joins /srv/receipts/ and name with +.

  3. Give one reason why deleting ../ from a file name is not enough.

    Show the answer

    A name such as ....// turns into ../ after the deletion.

Practise it

Challenges with a weakness this lesson teaches.