mirror of
https://github.com/vastsa/FileCodeBox.git
synced 2026-11-22 06:57:56 +08:00
fix: sanitize chunk upload file_name to prevent path traversal
This commit is contained in:
1 parent
f76d1e9841
commit
cebac32145
3 files changed
+37
No files matched your search
@@ -43,6 +43,7 @@ async def get_chunk_file_path_name(
|
||||
) -> Tuple[str, str, str, str, str]:
|
||||
today = await get_now()
|
||||
storage_path = settings.storage_path.strip("/")
|
||||
file_name = await sanitize_filename(unquote(file_name or ""))
|
||||
base_path = f"share/data/{today.strftime('%Y/%m/%d')}/{upload_id}"
|
||||
path = f"{storage_path}/{base_path}" if storage_path else base_path
|
||||
prefix, suffix = os.path.splitext(file_name)
|
||||
|
||||
@@ -116,6 +116,8 @@ class SystemFileStorage(FileStorageInterface):
|
||||
"""将相对路径解析到数据根目录内,阻止路径穿越。"""
|
||||
root = self.root_path.resolve()
|
||||
raw = str(relative_path or "").replace("\\", "/").lstrip("/")
|
||||
if any(part == ".." for part in raw.split("/")):
|
||||
raise ValueError("非法文件路径")
|
||||
candidate = (root / raw).resolve()
|
||||
try:
|
||||
candidate.relative_to(root)
|
||||
|
||||
@@ -10,6 +10,7 @@ from fastapi import HTTPException
|
||||
from apps.admin import dependencies as admin_dependencies
|
||||
from apps.admin.dependencies import create_token, verify_token
|
||||
from apps.admin.services import LocalFileClass
|
||||
from apps.base.utils import get_chunk_file_path_name
|
||||
from core.settings import data_root, settings
|
||||
from core.storage import SystemFileStorage
|
||||
from core.utils import hash_password, verify_password
|
||||
@@ -71,11 +72,44 @@ class SystemStoragePathTests(unittest.TestCase):
|
||||
with self.assertRaises(ValueError):
|
||||
self.storage._resolve_safe_path("../etc/passwd")
|
||||
|
||||
def test_resolve_safe_path_blocks_internal_dotdot(self):
|
||||
with self.assertRaises(ValueError):
|
||||
self.storage._resolve_safe_path("share/data/2026/08/29/x/../../../../../../filecodebox.db")
|
||||
|
||||
def test_resolve_safe_path_blocks_windows_dotdot(self):
|
||||
with self.assertRaises(ValueError):
|
||||
self.storage._resolve_safe_path("share\\data\\..\\..\\filecodebox.db")
|
||||
|
||||
def test_resolve_safe_path_allows_nested(self):
|
||||
target = self.storage._resolve_safe_path("share/data/a/b.txt")
|
||||
self.assertTrue(str(target).startswith(str(Path(self._tmpdir.name).resolve())))
|
||||
|
||||
|
||||
class ChunkFileNameSanitizeTests(unittest.TestCase):
|
||||
def test_chunk_file_path_name_strips_traversal(self):
|
||||
path, _, _, filename, save_path = asyncio.run(
|
||||
get_chunk_file_path_name("../../../../../../filecodebox.db", "a" * 32)
|
||||
)
|
||||
self.assertNotIn("..", save_path)
|
||||
self.assertEqual(filename, "filecodebox.db")
|
||||
self.assertEqual(save_path, f"{path}/filecodebox.db")
|
||||
self.assertIn("a" * 32, path)
|
||||
|
||||
def test_chunk_file_path_name_strips_windows_traversal(self):
|
||||
_, _, _, filename, save_path = asyncio.run(
|
||||
get_chunk_file_path_name("..\\..\\..\\..\\..\\..\\evil.bin", "b" * 32)
|
||||
)
|
||||
self.assertNotIn("..", save_path)
|
||||
self.assertEqual(filename, "evil.bin")
|
||||
|
||||
def test_chunk_file_path_name_keeps_normal_name(self):
|
||||
_, _, _, filename, save_path = asyncio.run(
|
||||
get_chunk_file_path_name("safe.txt", "c" * 32)
|
||||
)
|
||||
self.assertEqual(filename, "safe.txt")
|
||||
self.assertTrue(save_path.endswith("/safe.txt"))
|
||||
|
||||
|
||||
class AdminJwtUrlSafeTests(unittest.TestCase):
|
||||
def setUp(self):
|
||||
settings.jwt_secret = "j" * 48
|
||||
|
||||
Reference in new issue
Block a user