OOmnistu

Blog / Web & Ecommerce

Web & Ecommerce

Serve files from one folder, and test that string cleanup leaks

Keep file downloads inside one folder. Python fixtures run six inputs against five designs and show two plausible shortcuts that still leak.

OOmnistu
Reviewed 2026-11-18 · 4 min read

A download route takes ?file=report.txt and reads it from a folder. If the name is ../secret.txt, the read lands one level up. If it is an absolute path, the folder is ignored. The cleanup that comes to mind first, removing ../, is the one this guide tests.

The OWASP Path Traversal page describes the attack: variables that reference files are manipulated with dot-dot-slash sequences and their variations, or with absolute file paths, to reach files outside the web root. Among its protections are: do not let the user supply all parts of the path, validate by accepting only known-good input and do not sanitize, use indexes in place of file names where you can, and normalize input before file calls. This guide turns those lines into a runnable test. It was prepared with AI assistance and reviewed on 18 November 2026. The folder, files and symlink are created in a temporary directory.

The setup

A temporary directory holds files/ with report.txt and sub/a.txt, a secret.txt beside it, a sibling folder files-private/ and a symlink files/link.txt that points at secret.txt. Six inputs are tried: ../secret.txt, ....//secret.txt, an absolute path to the secret, ../files-private/x.txt, the symlink name, and a name that was already decoded once from %2e%2e%2f.

Five designs, one table

def contained(base, name):
    real_base = os.path.realpath(base)
    p = os.path.realpath(os.path.join(real_base, name))
    if not p.startswith(real_base + os.sep):
        raise PermissionError('outside base')
    return open(p).read()

The two mutations are the point of the test. Each adds exactly one named leak compared with C while matching C's refusal-or-missing result on the other inputs. The assertions check every result in the matrix, distinguishing an explicit containment refusal from a missing file. Unexpected errors fail the fixture instead of counting as protection. A check that only tried ../secret.txt would miss both mutations.

The design that avoids the question

Design D follows the index advice from the OWASP page. The caller supplies an already-parsed integer 1 or 2, and a server-side table maps that to report.txt or sub/a.txt. No client filename reaches the file system. The fixture does not test HTTP parsing or strict ID types. It reads both entries, and indexes 0, -1 and 3 raise KeyError. The cost is that the table must be kept up to date, a design trade-off and not something the page measures.

Encoding

The page lists encoded forms such as %2e%2e%2f and notes that web containers decode percent encoding once. The fixture shows both sides of that. After one decode, the input is a plain dot-dot and design C rejects it. If the name were still encoded, C treats it as a missing file and raises FileNotFoundError. It does not escape. Which layer decodes, and how many times, depends on your framework and is not tested here.

This is path containment, not permission to download any file inside the folder. Per-file authorization, a root-directory base, hard links, mount layout and files changing during a request need separate design and tests. The example base is a non-root directory. Raw percent-encoded input is not rejected as traversal by this function: it remains a literal filename and could be served if that literal file existed. The fixture establishes only that its particular literal file is absent.

All checks pass on Python 3.10.12 on Linux.

What this does not cover

It does not test Windows paths, null bytes, alternate separators, or case rules, all of which the OWASP page mentions. It does not test a real web framework or an archive extractor. A race between the check and the read, where a link is swapped in between, is not tested either, so a hard boundary such as a chroot or container mount is still worth having. The symlink case relies on the test machine allowing symlinks.

Sources