|
|
|
|
@ -1,3 +1,4 @@
|
|
|
|
|
import os
|
|
|
|
|
import re
|
|
|
|
|
from datetime import datetime, timedelta, timezone
|
|
|
|
|
from typing import Optional
|
|
|
|
|
@ -139,3 +140,42 @@ def save_to_pastebin(data, title="Result Log", expire_date="1H"):
|
|
|
|
|
return response.text # This will be the paste URL
|
|
|
|
|
else:
|
|
|
|
|
return f"Error: {response.status_code} - {response.text}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def sanitize_filename(filename: str) -> str:
|
|
|
|
|
"""
|
|
|
|
|
Sanitize the filename to ensure it is safe.
|
|
|
|
|
- Remove path info.
|
|
|
|
|
- Remove unsafe characters.
|
|
|
|
|
- Limit length.
|
|
|
|
|
"""
|
|
|
|
|
if not filename:
|
|
|
|
|
raise ValueError("Filename cannot be empty")
|
|
|
|
|
|
|
|
|
|
# Get the basename (remove any path)
|
|
|
|
|
filename = os.path.basename(filename)
|
|
|
|
|
|
|
|
|
|
# Remove control characters and non-printable characters
|
|
|
|
|
filename = re.sub(r'[\x00-\x1f\x7f]', '', filename)
|
|
|
|
|
|
|
|
|
|
# Allow alphanumeric, underscore, hyphen, space, and dots
|
|
|
|
|
# Remove other potentially dangerous characters.
|
|
|
|
|
filename = re.sub(r'[^a-zA-Z0-9_\-\.\ ]', '_', filename)
|
|
|
|
|
|
|
|
|
|
# Remove consecutive dots to prevent directory traversal attempts like '..'
|
|
|
|
|
filename = re.sub(r'\.{2,}', '.', filename)
|
|
|
|
|
|
|
|
|
|
# Ensure filename is not practically empty after sanitization
|
|
|
|
|
if not filename.strip() or filename.strip().replace('.', '') == '':
|
|
|
|
|
raise ValueError("Filename invalid after sanitization")
|
|
|
|
|
|
|
|
|
|
# Limit length (e.g. 200 chars)
|
|
|
|
|
if len(filename) > 200:
|
|
|
|
|
base, ext = os.path.splitext(filename)
|
|
|
|
|
# Preserve extension if possible
|
|
|
|
|
if len(ext) < 20:
|
|
|
|
|
filename = base[:(200-len(ext))] + ext
|
|
|
|
|
else:
|
|
|
|
|
filename = filename[:200]
|
|
|
|
|
|
|
|
|
|
return filename.strip()
|
|
|
|
|
|