Files, paths and includes
Any value that becomes a file path is a traversal, inclusion or upload risk.
Remember: Check the final resolved path against the allowed directory, or use a fixed allowlist of files. Never build the path from the request.
A file path is a small language of its own: ../, absolute paths, symlinks and encodings all change where it points. Code that builds a path from request data has to control the final, resolved location, not the text it was given.
- Path traversal: ../ sequences or an absolute path make the final path leave the base directory.
- Local file inclusion: include or require with a path from the request runs PHP code from disk, or an attacker's file.
- Uploads: the file name and the MIME type sent by the client are not trustworthy. Store files outside the web root, under generated names.
- Log poisoning: if an attacker can write a string into a log that gets included, file inclusion becomes code execution.
path = os.path.join(UPLOAD_DIR, name) # name = '/etc/passwd' -> '/etc/passwd'
candidate = os.path.realpath(os.path.join(UPLOAD_DIR, name))
if not candidate.startswith(UPLOAD_DIR + os.sep):
abort(404)
os.path.join('/srv/uploads', '/etc/passwd') returns '/etc/passwd'. Test your path code with absolute paths, not only with ../. Most traversal tests only try the second.
Questions to ask yourself
- Where is the path built, and from which values?
- Is the check done on the resolved path, or on the raw input?
- Could this file be executed, not just read?
Practise this
- Serving files from an upload directory · Injection & input
- Front controller including a page from the URL · Injection & input