From e288850e1d44b56a1ead3212077e85c7d91dcb56 Mon Sep 17 00:00:00 2001 From: MBK-fr <41642346+MBK-fr@users.noreply.github.com> Date: Sat, 21 Nov 2026 11:16:52 +0100 Subject: [PATCH] fix: Path traversal (arbitrary file write) in uploadPaste `save_file` reduces the client filename with `secure_filename`, resolves the destination and refuses anything outside the upload directory; failures return a generic GraphQL error instead of the raw OS error (which leaked internal paths). Findings: vuln_2d1ec6, vuln_f52b32 --- core/helpers.py | 19 ++++++++++++++----- core/views.py | 6 +++++- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/core/helpers.py b/core/helpers.py index c03cc87..8154587 100644 --- a/core/helpers.py +++ b/core/helpers.py @@ -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): @@ -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(): diff --git a/core/views.py b/core/views.py index 9168b88..4ce25b0 100644 --- a/core/views.py +++ b/core/views.py @@ -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(