From 316702b7ab3318487cd553adfbea05e5f9202225 Mon Sep 17 00:00:00 2001 From: Will Miao Date: Thu, 9 Jul 2026 09:18:38 +0800 Subject: [PATCH] fix(hf): allow subdirectory paths in HF resolve URLs, strip repo-internal dirs on save (#1019) --- py/routes/handlers/hf_handlers.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/py/routes/handlers/hf_handlers.py b/py/routes/handlers/hf_handlers.py index 50025c6a..e87016a4 100644 --- a/py/routes/handlers/hf_handlers.py +++ b/py/routes/handlers/hf_handlers.py @@ -363,8 +363,8 @@ class HfHandler: if ".." in (author, repo_name) or "." in (author, repo_name): return web.json_response({"error": f"Invalid repo format: {repo}"}, status=400) - # Validate filename — must not contain path separators or .. - if "/" in filename or "\\" in filename or ".." in filename: + # Validate filename — must not contain path traversal + if ".." in filename: return web.json_response({"error": "Invalid filename"}, status=400) # Validate relative_path — must not be absolute or escape base directory @@ -393,8 +393,12 @@ class HfHandler: else: target_dir = base_dir + # Strip HF repo subdirectory — "diffusion_models/xxx.safetensors" + # is an HF repo convention, not meaningful for local storage. + file_base = os.path.basename(filename) + os.makedirs(target_dir, exist_ok=True) - dest_path = os.path.join(target_dir, filename) + dest_path = os.path.join(target_dir, file_base) # Check if already exists (simple skip) if os.path.exists(dest_path) and os.path.getsize(dest_path) > 0: