fix(security): replace os.popen/os.system with subprocess for command injection prevention
Replace all deprecated and unsafe command execution methods with secure subprocess.run() calls using list arguments. Changes: - Replace os.popen() with subprocess.run() in tisbackup_gui.py - Replace os.system() with subprocess.run() in tasks.py and backup_xva.py - Add input validation for device/partition names (regex-based) - Fix file operations to use context managers (with statement) - Remove wildcard import from shutil - Add timeout protection to all subprocess calls (5-30s) - Improve error handling with proper try/except blocks Security improvements: - Prevent command injection vulnerabilities in USB disk operations - Validate device paths with regex before system calls - Use list arguments instead of shell=True to prevent injection - Add proper error handling instead of silent failures Code quality improvements: - Replace deprecated os.popen() (deprecated since Python 2.6) - Use context managers for file operations - Remove wildcard imports for cleaner namespace - Add comprehensive error handling and logging Documentation: - Add SECURITY_IMPROVEMENTS.md documenting all changes - Document remaining security issues and recommendations - Include testing recommendations and migration notes BREAKING CHANGE: None - all changes are backward compatible 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import logging
|
||||
import os
|
||||
import subprocess
|
||||
|
||||
from huey import RedisHuey
|
||||
|
||||
@@ -34,8 +35,12 @@ def run_export_backup(base, config_file, mount_point, backup_sections):
|
||||
return str(e)
|
||||
|
||||
finally:
|
||||
os.system("/bin/umount %s" % mount_point)
|
||||
os.rmdir(mount_point)
|
||||
# Safely unmount using subprocess instead of os.system
|
||||
try:
|
||||
subprocess.run(["/bin/umount", mount_point], check=True, timeout=30)
|
||||
os.rmdir(mount_point)
|
||||
except (subprocess.CalledProcessError, subprocess.TimeoutExpired, OSError) as e:
|
||||
logger.error(f"Failed to unmount {mount_point}: {e}")
|
||||
return "ok"
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user