I want to make an own static file view that returns the file defined in the GET request. The file must be in an extra directory. The URL must be like /e?s=NAME_OF_FILE. My problem is, hackers can use this like /e?s=/PATH/TO/DATABASE to get any file from the server. I have already a workaround, but i think there are better solutions.
My code:
path = os.path.abspath(os.path.join(script_path, filename))
if path.startswith(script_path):
# Good
else:
# Bad
This is for “hidden static files”, that should not be handled by the server.
What you are doing is of not much help. Some things you could do –
/e?s=MD5_HASH_OF_FILENAME. What this does is make it extremely difficult for the ‘hacker’ to ‘guess’ the filename. Brute-force does not help as MD5 are not easy to guess. So in effect, only people who have been some how sent this URL will have access to it.