Is the following code snippet from a Python WSGI app safe from directory traversal? It reads a file name passed as parameter and returns the named file.
file_nam
There's a much simpler solution here:
relative_path = os.path.relpath(path, start=self.test_directory) has_dir_traversal = relative_path.startswith(os.pardir)
relpath takes care of normalising path for us. And if the relative path starts with .., then you don't allow it.
relpath
..