Skip to content
Open
Show file tree
Hide file tree
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
19 changes: 14 additions & 5 deletions core/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
from config import WEB_UPLOADDIR
from jwt import decode
from werkzeug.utils import secure_filename
from core.models import ServerMode

def run_cmd(cmd):
Expand All @@ -21,12 +22,20 @@ def get_identity(token):
return decode(token, options={"verify_signature":False, "verify_exp":False}).get('identity')

def save_file(filename, text):
try:
f = open(WEB_UPLOADDIR + filename, 'w')
"""Write an uploaded paste inside WEB_UPLOADDIR only.

The client filename is reduced to a safe basename and the resolved path
must stay inside the upload directory. Raises ValueError otherwise.
"""
safe_name = secure_filename(filename or '')
if not safe_name:
raise ValueError('Invalid filename')
upload_dir = os.path.realpath(WEB_UPLOADDIR)
destination = os.path.realpath(os.path.join(upload_dir, safe_name))
if os.path.dirname(destination) != upload_dir:
raise ValueError('Invalid filename')
with open(destination, 'w') as f:
f.write(text)
f.close()
except Exception as e:
text = str(e)
return text

def is_level_easy():
Expand Down
6 changes: 5 additions & 1 deletion core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,11 @@ class Arguments:
result = graphene.String()

def mutate(self, info, filename, content):
result = helpers.save_file(filename, content)
try:
result = helpers.save_file(filename, content)
except (ValueError, OSError):
# Generic message: never echo filesystem paths or OS errors to the client.
raise GraphQLError('Could not save the uploaded file.')
owner = Owner.query.filter_by(name='DVGAUser').first()

Paste.create_paste(
Expand Down
Loading