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

Implemented possible solution for path traversal #28

Open
wants to merge 5 commits into
base: dev-0.3
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
added path filtering for other domains
chris18191 committed Jul 2, 2020
commit f09f1d064e85057206c3bd7a2eab20b88ebafc00
22 changes: 16 additions & 6 deletions app/api/endpoints.py
Original file line number Diff line number Diff line change
@@ -8,11 +8,11 @@
def renderError(msg, error_header="An error occured!", statusCode=500):
return flask.render_template('error.html', error_header=error_header, error_message=msg), statusCode

def getValidPath(f: str, d: str):
def getValidSubpath(f: str, d: str):
"""
Returns the absolute path of wanted file or directory, but only if it is contained in the given directory.
:param f: File name to retireve the path for.
:param f: File name or directory to retireve the path for.
:type f: str
:param d: Directory, the file should be in.
@@ -25,6 +25,7 @@ def getValidPath(f: str, d: str):
file_path = os.path.realpath(f)
common_path = os.path.commonpath((file_path, d))

# a valid sub path must contain the whole parent directory in its own path
if common_path == d:
return file_path

@@ -43,7 +44,11 @@ def get_config(name: str):
"""
nginx_path = flask.current_app.config['NGINX_PATH']

with io.open(os.path.join(nginx_path, name), 'r') as f:
path = getValidSubpath(os.path.join(nginx_path, name), nginx_path)
if path == None:
return renderError(f'Could not read file "{path}".')

with io.open(path, 'r') as f:
_file = f.read()

return flask.render_template('config.html', name=name, file=_file), 200
@@ -65,8 +70,9 @@ def post_config(name: str):

config_file = os.path.join(nginx_path, name)

if not os.path.commonprefix(os.path.realpath(config_file), nginx_path):
return flask.make_response({'success': False}), 200
path = getValidSubpath(config_file, nginx_path)
if path == None:
return flask.make_response({'success': False}), 500

with io.open(config_file, 'w') as f:
f.write(content['file'])
@@ -162,8 +168,12 @@ def post_domain(name: str):
new_domain = flask.render_template('new_domain.j2', name=name)
name = name + '.conf.disabled'

path = getValidSubpath(os.path.join(config_path, name), config_path)
if path == None:
return flask.jsonify({'success': False, 'error_msg': 'invalid domain path'}), 500

try:
with io.open(os.path.join(config_path, name), 'w') as f:
with io.open(path, 'w') as f:
f.write(new_domain)

response = flask.jsonify({'success': True}), 201