Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added protection against directory traversal vulnerability. #39

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions fapws/contrib/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import os
import time

from os.path import abspath, relpath, commonprefix


class Staticfile:
""" Generic class that you can use to dispatch static files
Expand All @@ -25,17 +27,22 @@ class Staticfile:
NOTE: you must be consistent between /rootpath/ and /static/ concerning the ending "/"
"""
def __init__(self, rootpath="", maxage=None):
self.rootpath = rootpath
self.rootpath = abspath(rootpath)
self.maxage = maxage

def __call__(self, environ, start_response):
fpath = self.rootpath + environ['PATH_INFO']
# Figure out the absolute path requested, removing any relative path segments
fpath = abspath(relpath(environ['PATH_INFO'], self.rootpath))

# Make sure that the requested path is inside the root path folder
if not fpath.startswith(self.rootpath):
return self.respond_not_found(environ, start_response, fpath)

try:
f = open(fpath, "rb")
except:
print "ERROR in Staticfile: file %s not existing" % (fpath)
start_response('404 File not found', [])
return []
return self.respond_not_found(environ, start_response, fpath)

fmtime = os.path.getmtime(fpath)
if environ.get('HTTP_IF_NONE_MATCH', 'NONE') != str(fmtime):
headers = []
Expand All @@ -52,3 +59,8 @@ def __call__(self, environ, start_response):
#print "SAME", environ['fapws.uri']
start_response('304 Not Modified', [])
return []

def respond_not_found(self, environ, start_response, fpath):
print "ERROR in Staticfile: file %s not existing" % (fpath)
start_response('404 File not found', [])
return []