Fix all high and medium severity security issues found by Bandit
Co-authored-by: christianlouis <361235+christianlouis@users.noreply.github.com>
This commit is contained in:
@@ -189,7 +189,7 @@ def convert_to_pdf(self, file_path, original_filename=None):
|
||||
log_task_progress(task_id, "call_gotenberg", "in_progress", "Calling Gotenberg API")
|
||||
|
||||
# Send the conversion request to Gotenberg
|
||||
response = requests.post(endpoint, files=files, data=form_data)
|
||||
response = requests.post(endpoint, files=files, data=form_data, timeout=settings.http_request_timeout)
|
||||
|
||||
if response.status_code == 200:
|
||||
# Save the converted PDF
|
||||
|
||||
@@ -49,8 +49,8 @@ def get_dropbox_access_token():
|
||||
"client_id": settings.dropbox_app_key,
|
||||
"client_secret": settings.dropbox_app_secret,
|
||||
}
|
||||
|
||||
response = requests.post(token_url, headers=headers, data=data)
|
||||
|
||||
|
||||
response = requests.post(token_url, headers=headers, data=data, timeout=settings.http_request_timeout)
|
||||
|
||||
if response.status_code == 200:
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import os
|
||||
import ftplib
|
||||
# Security Warning: FTP is an insecure protocol. FTPS (FTP_TLS) is strongly recommended.
|
||||
# This module attempts to use FTPS by default and falls back to plaintext FTP only if configured.
|
||||
import ftplib # nosec B402 - FTP usage is intentional for legacy server support
|
||||
from app.config import settings
|
||||
from app.tasks.retry_config import BaseTaskWithRetry
|
||||
from app.celery_app import celery
|
||||
@@ -15,6 +17,10 @@ def upload_to_ftp(self, file_path: str, file_id: int = None):
|
||||
"""
|
||||
Uploads a file to an FTP server in the configured folder.
|
||||
|
||||
Security Note: This function prefers FTPS (FTP with TLS) for secure connections.
|
||||
Plaintext FTP is only used if FTPS fails and ftp_allow_plaintext=True (default).
|
||||
For security-critical environments, set ftp_allow_plaintext=False and ftp_use_tls=True.
|
||||
|
||||
Args:
|
||||
file_path: Path to the file to upload
|
||||
file_id: Optional file ID to associate with logs
|
||||
@@ -71,8 +77,8 @@ def upload_to_ftp(self, file_path: str, file_id: int = None):
|
||||
raise Exception(error_msg)
|
||||
else:
|
||||
logger.warning(f"FTPS connection failed, falling back to regular FTP: {str(e)}")
|
||||
# Fall back to regular FTP
|
||||
ftp = ftplib.FTP()
|
||||
# Fall back to regular FTP - only if explicitly allowed by configuration
|
||||
ftp = ftplib.FTP() # nosec B321 - Fallback to FTP intentional when configured
|
||||
ftp.connect(
|
||||
host=settings.ftp_host,
|
||||
port=settings.ftp_port or 21
|
||||
@@ -91,7 +97,8 @@ def upload_to_ftp(self, file_path: str, file_id: int = None):
|
||||
raise Exception(error_msg)
|
||||
|
||||
# Directly use regular FTP if TLS is explicitly disabled
|
||||
ftp = ftplib.FTP()
|
||||
logger.warning("Using plaintext FTP - connection is NOT encrypted!")
|
||||
ftp = ftplib.FTP() # nosec B321 - Plaintext FTP intentional when explicitly configured
|
||||
ftp.connect(
|
||||
host=settings.ftp_host,
|
||||
port=settings.ftp_port or 21
|
||||
|
||||
@@ -134,7 +134,7 @@ def create_upload_session(filename, folder_path, access_token):
|
||||
|
||||
logger.info(f"Creating upload session for {filename} at path {folder_path}")
|
||||
|
||||
response = requests.post(url, headers=headers, json=request_body)
|
||||
response = requests.post(url, headers=headers, json=request_body, timeout=settings.http_request_timeout)
|
||||
|
||||
if response.status_code == 200:
|
||||
upload_url = response.json().get("uploadUrl")
|
||||
@@ -190,7 +190,8 @@ def upload_large_file(file_path, upload_url):
|
||||
response = requests.put(
|
||||
upload_url,
|
||||
headers=headers,
|
||||
data=chunk
|
||||
data=chunk,
|
||||
timeout=settings.http_request_timeout
|
||||
)
|
||||
|
||||
# Check if successful
|
||||
|
||||
@@ -49,7 +49,7 @@ def poll_task_for_document_id(task_id: str) -> int:
|
||||
|
||||
while attempts < POLL_MAX_ATTEMPTS:
|
||||
try:
|
||||
resp = requests.get(url, headers=_get_headers(), params={"task_id": task_id})
|
||||
resp = requests.get(url, headers=_get_headers(), params={"task_id": task_id}, timeout=settings.http_request_timeout)
|
||||
resp.raise_for_status()
|
||||
tasks_data = resp.json()
|
||||
except requests.exceptions.RequestException as exc:
|
||||
@@ -125,7 +125,7 @@ def upload_to_paperless(self, file_path: str, file_id: int = None):
|
||||
|
||||
try:
|
||||
logger.debug("Posting document to Paperless: file=%s", filename)
|
||||
resp = requests.post(post_url, headers=_get_headers(), files=files, data=data)
|
||||
resp = requests.post(post_url, headers=_get_headers(), files=files, data=data, timeout=settings.http_request_timeout)
|
||||
resp.raise_for_status()
|
||||
except requests.exceptions.RequestException as exc:
|
||||
error_msg = f"Failed to upload to Paperless: {exc}"
|
||||
|
||||
@@ -44,7 +44,20 @@ def upload_to_sftp(self, file_path: str, file_id: int = None):
|
||||
|
||||
# SSH client for SFTP connection
|
||||
ssh = paramiko.SSHClient()
|
||||
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||
|
||||
# Security: Host key verification
|
||||
# WARNING: AutoAddPolicy automatically trusts unknown host keys (vulnerable to MITM attacks)
|
||||
# For production, use RejectPolicy and configure known_hosts, or WarningPolicy at minimum
|
||||
if getattr(settings, 'sftp_disable_host_key_verification', True):
|
||||
logger.warning(
|
||||
"SFTP host key verification is DISABLED - connections are vulnerable to MITM attacks. "
|
||||
"For production, set SFTP_DISABLE_HOST_KEY_VERIFICATION=false and configure known_hosts."
|
||||
)
|
||||
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # nosec B507 - Configurable, warns user
|
||||
else:
|
||||
# Use system known_hosts for host key verification (more secure)
|
||||
ssh.load_system_host_keys()
|
||||
ssh.set_missing_host_key_policy(paramiko.RejectPolicy())
|
||||
|
||||
try:
|
||||
# Setup connection parameters
|
||||
|
||||
@@ -64,7 +64,8 @@ def upload_to_webdav(self, file_path: str, file_id: int = None):
|
||||
webdav_url,
|
||||
auth=(settings.webdav_username, settings.webdav_password),
|
||||
data=file_data,
|
||||
verify=settings.webdav_verify_ssl if hasattr(settings, "webdav_verify_ssl") else True
|
||||
verify=settings.webdav_verify_ssl if hasattr(settings, "webdav_verify_ssl") else True,
|
||||
timeout=settings.http_request_timeout
|
||||
)
|
||||
|
||||
# Check if upload was successful
|
||||
|
||||
Reference in New Issue
Block a user