我需要防止directory traversal attack
我的代码使用 Python。我的代码如下:
if request.GET.get('param') is not None and request.GET.get('param') != '':
param = request.GET.get('param')
startdir = os.path.abspath(os.curdir)
requested_path = os.path.relpath(param, startdir)
requested_path = os.path.abspath(requested_path)
print(requested_path)
tfile = open(requested_path, 'rb')
return HttpResponse(content=tfile, content_type="text/plain")
在这里我需要用户像http://127.0.0.1:8000/createfile/?param=../../../../../../../../etc/passwd
这样运行它应该防止目录遍历攻击。