Skip to content

Commit 96395c1

Browse files
committed
feat: 增强插件静态文件API安全性
1 parent 6065c29 commit 96395c1

1 file changed

Lines changed: 12 additions & 1 deletion

File tree

app/api/endpoints/plugin.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,18 @@ async def plugin_static_file(plugin_id: str, filepath: str):
360360
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="Forbidden")
361361

362362
plugin_base_dir = AsyncPath(settings.ROOT_PATH) / "app" / "plugins" / plugin_id.lower()
363-
plugin_file_path = plugin_base_dir / filepath
363+
plugin_file_path = plugin_base_dir / filepath.lstrip('/')
364+
365+
try:
366+
resolved_base = await plugin_base_dir.resolve()
367+
resolved_file = await plugin_file_path.resolve()
368+
except Exception:
369+
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Invalid path")
370+
371+
if not resolved_file.is_relative_to(resolved_base):
372+
logger.warning(f"Static File API: Path traversal attempt detected: {plugin_id}/{filepath}")
373+
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="Forbidden")
374+
364375
if not await plugin_file_path.exists():
365376
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"{plugin_file_path} 不存在")
366377
if not await plugin_file_path.is_file():

0 commit comments

Comments
 (0)