I'm writing a personal wiki-style program in Python that stores text files in a user configurable directory.
The program should be able to take a string (e.g. foo
) from a user and create a filename of foo.txt
. The user will only be able to create the file inside the wiki directory, and slashes will create a subdir (e.g. foo/bar
becomes (path-to-wiki)/foo/bar.txt
).
What is the best way to check that the input is as safe as possible? What do I need to watch out for? I know some common pitfalls are:
- Directory traversal:
../
- Null bytes:
\0
I realize that taking user input for filenames is never 100% safe, but the program will only be run locally and I just want to guard for any common errors/glitches.