mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2025-12-15 14:22:55 +08:00
⚡ 修复路径检测
This commit is contained in:
parent
f583306dae
commit
005cafb90d
@ -1,5 +1,6 @@
|
|||||||
import os
|
import os
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
import re
|
||||||
import shutil
|
import shutil
|
||||||
|
|
||||||
import aiofiles
|
import aiofiles
|
||||||
@ -25,20 +26,36 @@ IMAGE_TYPE = ["jpg", "jpeg", "png", "gif", "bmp", "webp", "svg"]
|
|||||||
description="获取文件列表",
|
description="获取文件列表",
|
||||||
)
|
)
|
||||||
async def _(path: str | None = None) -> Result[list[DirFile]]:
|
async def _(path: str | None = None) -> Result[list[DirFile]]:
|
||||||
base_path = Path(path) if path else Path()
|
try:
|
||||||
data_list = []
|
# 清理和验证路径
|
||||||
for file in os.listdir(base_path):
|
if path:
|
||||||
file_path = base_path / file
|
# 移除任何可能的路径遍历尝试
|
||||||
is_image = any(file.endswith(f".{t}") for t in IMAGE_TYPE)
|
path = re.sub(r"[\\/]\.\.[\\/]", "", path)
|
||||||
data_list.append(
|
# 规范化路径
|
||||||
DirFile(
|
base_path = Path(path).resolve()
|
||||||
is_file=not file_path.is_dir(),
|
# 验证路径是否在项目根目录内
|
||||||
is_image=is_image,
|
if not base_path.is_relative_to(Path().resolve()):
|
||||||
name=file,
|
return Result.fail("访问路径超出允许范围")
|
||||||
parent=path,
|
else:
|
||||||
|
base_path = Path().resolve()
|
||||||
|
|
||||||
|
data_list = []
|
||||||
|
for file in os.listdir(base_path):
|
||||||
|
file_path = base_path / file
|
||||||
|
is_image = any(file.endswith(f".{t}") for t in IMAGE_TYPE)
|
||||||
|
data_list.append(
|
||||||
|
DirFile(
|
||||||
|
is_file=not file_path.is_dir(),
|
||||||
|
is_image=is_image,
|
||||||
|
name=file,
|
||||||
|
parent=str(base_path.relative_to(Path().resolve()))
|
||||||
|
if path
|
||||||
|
else None,
|
||||||
|
)
|
||||||
)
|
)
|
||||||
)
|
return Result.ok(data_list)
|
||||||
return Result.ok(data_list)
|
except Exception as e:
|
||||||
|
return Result.fail(f"获取文件列表失败: {e!s}")
|
||||||
|
|
||||||
|
|
||||||
@router.get(
|
@router.get(
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user