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

Restrict file types #370

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
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
1 change: 1 addition & 0 deletions server/Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ shapely = "==2.0.6"
psycogreen = "==1.0.2"
importlib-metadata = "==8.4.0" # https://github.com/pallets/flask/issues/4502
typing_extensions = "==4.12.2"
python-magic = "==0.4.27"
# requirements for development on windows
colorama = "==0.4.5"

Expand Down
132 changes: 68 additions & 64 deletions server/Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 11 additions & 3 deletions server/mergin/sync/public_api_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
import binascii
import functools
import json
import mimetypes
import os
import logging
from dataclasses import asdict
from typing import Dict
from urllib.parse import quote
import uuid
from datetime import datetime

import psycopg2
from blinker import signal
from connexion import NoContent, request
Expand Down Expand Up @@ -87,6 +87,9 @@
get_project_path,
get_device_id,
is_valid_path,
is_supported_type,
is_supported_extension,
get_mimetype,
)
from .errors import StorageLimitHit
from ..utils import format_time_delta
Expand Down Expand Up @@ -406,7 +409,7 @@ def download_project_file(
if not is_binary(abs_path):
mime_type = "text/plain"
else:
mime_type = mimetypes.guess_type(abs_path)[0]
mime_type = get_mimetype(abs_path)
resp.headers["Content-Type"] = mime_type
resp.headers["Content-Disposition"] = "attachment; filename={}".format(
quote(os.path.basename(file).encode("utf-8"))
Expand Down Expand Up @@ -813,7 +816,9 @@ def project_push(namespace, project_name):
if not all(ele.path != item.path for ele in project.files):
abort(400, f"File {item.path} has been already uploaded")
if not is_valid_path(item.path):
abort(400, f"File {item.path} contains invalid characters.")
abort(400, f"File '{item.path}' contains invalid characters.")
if not is_supported_extension(item.path):
abort(400, f"Unsupported extension of '{item.path}' file.")

# changes' files must be unique
changes_files = [
Expand Down Expand Up @@ -1042,6 +1047,9 @@ def push_finish(transaction_id):
)
corrupted_files.append(f.path)
continue
if not is_supported_type(dest_file):
logging.info(f"Rejecting blacklisted file: {dest_file}")
abort(400, f"Unsupported file type of '{f.path}' file.")

if expected_size != os.path.getsize(dest_file):
logging.error(
Expand Down
Loading
Loading