fix(security): add input sanitization and path traversal protection
Python Linting / Run Ruff (push) Waiting to run

- Sanitize config string values to prevent CRLF injection attacks
- Add get_safe_snapshot_path() helper to validate snapshot targets
- Block path traversal attempts in remove_snapshot and restore_snapshot endpoints
- Reject targets containing /, \, .., or null characters
This commit is contained in:
Dr.Lt.Data
2026-01-08 18:29:14 +09:00
parent 8d67702ab0
commit f4fa394e0f
3 changed files with 26 additions and 4 deletions
+19 -2
View File
@@ -997,6 +997,15 @@ async def get_snapshot_list(request):
return web.json_response({'items': items}, content_type='application/json')
def get_safe_snapshot_path(target):
"""
Safely construct a snapshot file path, preventing path traversal attacks.
"""
if '/' in target or '\\' in target or '..' in target or '\x00' in target:
return None
return os.path.join(core.manager_snapshot_path, f"{target}.json")
@routes.get("/snapshot/remove")
async def remove_snapshot(request):
if not is_allowed_security_level('middle'):
@@ -1005,8 +1014,12 @@ async def remove_snapshot(request):
try:
target = request.rel_url.query["target"]
path = get_safe_snapshot_path(target)
if path is None:
logging.error(f"[ComfyUI-Manager] Invalid snapshot target: {target}")
return web.Response(text="Invalid snapshot target", status=400)
path = os.path.join(core.manager_snapshot_path, f"{target}.json")
if os.path.exists(path):
os.remove(path)
@@ -1023,8 +1036,12 @@ async def restore_snapshot(request):
try:
target = request.rel_url.query["target"]
path = get_safe_snapshot_path(target)
if path is None:
logging.error(f"[ComfyUI-Manager] Invalid snapshot target: {target}")
return web.Response(text="Invalid snapshot target", status=400)
path = os.path.join(core.manager_snapshot_path, f"{target}.json")
if os.path.exists(path):
if not os.path.exists(core.manager_startup_script_path):
os.makedirs(core.manager_startup_script_path)