I have a form that submits a string to my Flask application when submitting the form. The string is the path to the file, so I want to make sure that it does not contain anything contrary, for example ../../../etc/passwd . Werkzeug, which uses Flask, has a convenient feature called secure_filename that removes nasty things from file names. Unfortunately, when supplying the full path, such as templates/example.html , it converts / to _ , so we get templates_example.html .
It seems reasonable to split the path into levels, so I send the templates and example.html separately, and then combine them on the server again. This works great, except that the path can be arbitrarily deep. I could just connect dir1/dir2/dir3/dir4 and hope no one gets deeper than dir4 , but that seems dumb.
What is the correct way to handle validation of paths of unknown depth? Confirm differently? Send data in different ways? Encode the path in different ways and then decode it on the server?
source share