mirror of
https://github.com/Comfy-Org/ComfyUI-Manager.git
synced 2026-07-26 10:07:38 +08:00
fix(security): add input sanitization and path traversal protection
Python Linting / Run Ruff (push) Waiting to run
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:
+19
-2
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user