Skip to content
Path traversal Disclosed report Intermediate CVE-2021-3281

Path traversal via archive.extract - CVE 2021-3281 incomplete patch

The fix for CVE-2021-3281 in Django's archive extraction is incomplete because the path check uses a prefix match that can be bypassed by a sibling directory whose name starts with the same characters. An attacker could write files outside the intended base directory when extracting a malicious tar or zip archive.

stackered Follow

Originally disclosed on HackerOne

  • Triaged in 13 days
  • Fixed in 26 days
  • Disclosed after 3 months

Hi,

While performing a code review on previous vulnerability patches, I found that the fix for the CVE-2021-3281 is incomplete. Just as a reminder, in order to mitigate this vulnerability, the following guard logic has been added in the extract function of both ZipArchive and TarArchive classes to ensure that all files are extracted under the base folder (target_path).

Python
# django/django/utils/archive.py
def target_filename(self, to_path, name):
    target_path = os.path.abspath(to_path)
    filename = os.path.abspath(os.path.join(target_path, name))
    if not filename.startswith(target_path):
        raise SuspiciousOperation("Archive contains invalid path: '%s'" % name)
    return filename

The abspath function is used to retrieve the absolute and normalized unique path. Then, the startswith function is leveraged to verify that the destination path is part of the intended base folder. However, the abspath function removes terminating path separators, which makes the guard logic protection insufficient to protect against partial-path traversal.

Assuming the base folder is defined as /var/lib/, and the initial file name as /var/library/test.txt. As the trailing slash is removed, an attacker could still partially path traversal to another directory starting with the same characters since this passes the check.

Python
"/var/library/test.txt".startswith("/var/lib") == true

As an example, executing the following lines in a Python CLI does not raise the intended Exception.

import os
target_path = os.path.abspath("/var/lib/")
filename = os.path.abspath(os.path.join(target_path, "/var/library/test.txt"))
if not filename.startswith(target_path):
   raise Exception

A real-life example of such a partial path traversal vulnerability can be found here.

Impact

This leads to a partial-path traversal vulnerability, which allows an attacker to write files outside of the base directory when unzipping archives (tar and zip).

The original report as published on HackerOne, by stackered. Images and videos are our own copies; raw HTML is shown as text. View it on HackerOne.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

How it worked

Root cause

Likely the guard uses a simple string prefix check on the absolute path without ensuring a path separator boundary, so a sibling directory with a name that begins with the base path passes the check.

Impact

An attacker could write files outside the base directory when unzipping tar and zip archives, but only to sibling directories whose names share the same prefix.

What the attacker needed

Ability to supply a crafted archive; extraction uses the vulnerable Django code

The fix

The check should compare path components or ensure the base path ends with a separator before the prefix test, so that only true subdirectories are allowed.

How it was found

  1. Set the base folder to a path ending with a slash, such as '/var/lib/'.
  2. Provide a file name that resolves to a sibling directory starting with the same characters, such as '/var/library/test.txt'.
  3. Observe that the startswith check passes and no exception is raised.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

When you unzip a file, the program should only write files inside a chosen folder. The old fix checked if the destination path started with the folder name, but that check is too loose: a folder like '/var/library' also starts with '/var/lib', so files could be written there. This report shows the check can be tricked.

What is Path traversal?

Input that builds a file path escapes the intended folder, reading or writing files the application should never touch.

More by stackered

View all

3 more disclosed reports, best first.

More from Django

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Path traversal and file inclusion

The lesson behind this report.

Input that builds a file path escapes the intended folder, reading or writing files the application should never touch.