feat: 整合官方 2.7.0 安全修复与本地文件分享
This commit is contained in:
1 parent
b10113e5ab
commit
9e9571b031
42 files changed
+2824
-268
No files matched your search
@@ -0,0 +1,207 @@
|
||||
"""Local NAS share: nested browse + zero-copy extract codes (issue #532)."""
|
||||
import asyncio
|
||||
import datetime
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
from tempfile import TemporaryDirectory
|
||||
from unittest.mock import patch
|
||||
|
||||
from fastapi import HTTPException
|
||||
|
||||
from apps.admin.services import FileService, LocalFileClass, LocalFileService
|
||||
LOCAL_REF_MARKER = "local-ref"
|
||||
|
||||
def is_local_ref(record):
|
||||
return record.file_path == LOCAL_REF_MARKER
|
||||
from apps.base.models import FileCodes
|
||||
from apps.base.quota import get_storage_usage, reserve_storage
|
||||
from core.tasks import delete_expire_files
|
||||
from tests.helpers import SettingsOverrideMixin, close_db, init_memory_db
|
||||
from core.settings import settings
|
||||
from core.utils import get_now
|
||||
|
||||
|
||||
class SleepSentinel(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class LocalFileClassTests(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self._tmpdir = TemporaryDirectory()
|
||||
self.root = Path(self._tmpdir.name)
|
||||
(self.root / "local").mkdir()
|
||||
(self.root / "local" / "safe.txt").write_bytes(b"ok")
|
||||
(self.root / "local" / "movies").mkdir()
|
||||
(self.root / "local" / "movies" / "a.bin").write_bytes(b"nested")
|
||||
self._patch = patch("core.settings.data_root", self.root)
|
||||
self._patch.start()
|
||||
|
||||
def tearDown(self):
|
||||
self._patch.stop()
|
||||
self._tmpdir.cleanup()
|
||||
|
||||
def test_rejects_dotdot_filename(self):
|
||||
with self.assertRaises(HTTPException) as ctx:
|
||||
LocalFileClass("../etc/passwd")
|
||||
self.assertEqual(ctx.exception.status_code, 400)
|
||||
|
||||
def test_rejects_nested_dotdot(self):
|
||||
with self.assertRaises(HTTPException) as ctx:
|
||||
LocalFileClass("movies/../../etc/passwd")
|
||||
self.assertEqual(ctx.exception.status_code, 400)
|
||||
|
||||
def test_rejects_absolute_filename(self):
|
||||
with self.assertRaises(HTTPException) as ctx:
|
||||
LocalFileClass("/etc/passwd")
|
||||
self.assertEqual(ctx.exception.status_code, 400)
|
||||
|
||||
def test_allows_basename_inside_local(self):
|
||||
local_file = LocalFileClass("safe.txt")
|
||||
self.assertTrue(asyncio.run(local_file.exists()))
|
||||
self.assertEqual(local_file.file, "safe.txt")
|
||||
self.assertEqual(local_file.path, (self.root / "local" / "safe.txt").resolve())
|
||||
|
||||
def test_allows_nested_relative(self):
|
||||
local_file = LocalFileClass("movies/a.bin")
|
||||
self.assertTrue(asyncio.run(local_file.exists()))
|
||||
self.assertEqual(local_file.file, "movies/a.bin")
|
||||
self.assertEqual(local_file.name, "a.bin")
|
||||
self.assertEqual(local_file.size, 6)
|
||||
|
||||
|
||||
class LocalFileServiceListTests(unittest.TestCase):
|
||||
def test_lists_nested_dirs_and_files(self):
|
||||
asyncio.run(self._scenario())
|
||||
|
||||
async def _scenario(self):
|
||||
with TemporaryDirectory() as tmpdir:
|
||||
root = Path(tmpdir)
|
||||
movies = root / "local" / "movies"
|
||||
movies.mkdir(parents=True)
|
||||
(root / "local" / "readme.txt").write_bytes(b"hi")
|
||||
(movies / "a.bin").write_bytes(b"nested")
|
||||
with patch("core.settings.data_root", root):
|
||||
service = LocalFileService()
|
||||
root_listing = await service.list_files("")
|
||||
self.assertEqual(root_listing["path"], "")
|
||||
names = {item["name"]: item for item in root_listing["items"]}
|
||||
self.assertEqual(names["movies"]["type"], "dir")
|
||||
self.assertEqual(names["readme.txt"]["type"], "file")
|
||||
|
||||
nested = await service.list_files("movies")
|
||||
self.assertEqual(nested["path"], "movies")
|
||||
self.assertEqual(nested["parent"], "")
|
||||
self.assertEqual(nested["items"][0]["path"], "movies/a.bin")
|
||||
self.assertEqual(nested["items"][0]["file"], "a.bin")
|
||||
|
||||
|
||||
class ShareLocalFileTests(SettingsOverrideMixin, unittest.TestCase):
|
||||
def test_share_local_file_is_zero_copy(self):
|
||||
asyncio.run(self._scenario())
|
||||
|
||||
async def _scenario(self):
|
||||
with TemporaryDirectory() as tmpdir:
|
||||
await init_memory_db()
|
||||
try:
|
||||
root = Path(tmpdir)
|
||||
movies = root / "local" / "movies"
|
||||
movies.mkdir(parents=True)
|
||||
source = movies / "doc.txt"
|
||||
source.write_bytes(b"hello local share")
|
||||
with patch("core.settings.data_root", root), patch(
|
||||
"core.storage.data_root", root
|
||||
):
|
||||
class Item:
|
||||
filename = "movies/doc.txt"
|
||||
expire_value = 1
|
||||
expire_style = "day"
|
||||
|
||||
result = await FileService().share_local_file(Item())
|
||||
self.assertIn("code", result)
|
||||
self.assertEqual(result["name"], "doc.txt")
|
||||
self.assertEqual(result["path"], "movies/doc.txt")
|
||||
|
||||
record = await FileCodes.get(code=result["code"])
|
||||
self.assertTrue(is_local_ref(record))
|
||||
self.assertEqual(record.file_path, LOCAL_REF_MARKER)
|
||||
self.assertEqual(record.uuid_file_name, "movies/doc.txt")
|
||||
self.assertTrue(source.exists())
|
||||
share_files = [
|
||||
p for p in (root / "share").rglob("*") if p.is_file()
|
||||
] if (root / "share").exists() else []
|
||||
self.assertFalse(share_files)
|
||||
finally:
|
||||
await close_db()
|
||||
|
||||
|
||||
class LocalRefCleanupTests(SettingsOverrideMixin, unittest.TestCase):
|
||||
def test_expire_trashes_record_but_keeps_original(self):
|
||||
asyncio.run(self._scenario())
|
||||
|
||||
async def _scenario(self):
|
||||
with TemporaryDirectory() as tmpdir:
|
||||
await init_memory_db()
|
||||
try:
|
||||
root = Path(tmpdir)
|
||||
source = root / "local" / "keep.bin"
|
||||
source.parent.mkdir(parents=True)
|
||||
source.write_bytes(b"keep-me")
|
||||
now = await get_now()
|
||||
await FileCodes.create(
|
||||
code="nas1",
|
||||
prefix="keep",
|
||||
suffix=".bin",
|
||||
uuid_file_name="keep.bin",
|
||||
file_path=LOCAL_REF_MARKER,
|
||||
size=7,
|
||||
expired_at=now - datetime.timedelta(days=1),
|
||||
expired_count=-1,
|
||||
)
|
||||
|
||||
def _sleep(_seconds):
|
||||
raise SleepSentinel
|
||||
|
||||
with patch("core.settings.data_root", root), patch(
|
||||
"core.storage.data_root", root
|
||||
), patch("core.tasks.data_root", root), patch(
|
||||
"core.tasks.asyncio.sleep", side_effect=_sleep
|
||||
):
|
||||
try:
|
||||
await delete_expire_files()
|
||||
except SleepSentinel:
|
||||
pass
|
||||
|
||||
self.assertTrue(source.exists())
|
||||
self.assertTrue((await FileCodes.get(code="nas1")).is_trashed)
|
||||
finally:
|
||||
await close_db()
|
||||
|
||||
|
||||
class LocalRefQuotaTests(SettingsOverrideMixin, unittest.TestCase):
|
||||
def test_local_ref_does_not_consume_quota(self):
|
||||
asyncio.run(self._scenario())
|
||||
|
||||
async def _scenario(self):
|
||||
settings.storageLimit = 100
|
||||
await init_memory_db()
|
||||
try:
|
||||
await FileCodes.create(
|
||||
code="nas-big",
|
||||
size=10_000,
|
||||
expired_count=-1,
|
||||
file_path=LOCAL_REF_MARKER,
|
||||
uuid_file_name="movies/huge.bin",
|
||||
)
|
||||
await FileCodes.create(code="owned", size=20, expired_count=-1)
|
||||
usage = await get_storage_usage()
|
||||
self.assertEqual(usage["used"], 20)
|
||||
await reserve_storage("upload-ok", 50, 300)
|
||||
usage = await get_storage_usage()
|
||||
self.assertEqual(usage["reserved"], 50)
|
||||
self.assertEqual(usage["available"], 30)
|
||||
finally:
|
||||
await close_db()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user