jan jan - 6 months ago 61
Python Question

Sanitizing a file path in python

I have a file browser application that exposes a directory and its contents to the users.

I want to sanitize the user input, which is a file path, so that it does not allow absolute paths such as '/tmp/' and relative paths such as '../../etc'

Is there a python function that does this across platforms?

Answer

Perhaps instead of just cleaning the path (blacklisting) you could only allow (whitelist) a valid path.

python provides a variety of tools in os.path to do this in an os independent way

Comments