From 5c1e1ef36811d588b58927e8f82308126f65abed Mon Sep 17 00:00:00 2001 From: Orion Date: Fri, 18 Sep 2026 17:54:13 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(upload):=20=E6=A0=A1?= =?UTF-8?q?=E9=AA=8C=E6=97=A7=E4=BC=9A=E8=AF=9D=E6=9C=AB=E7=89=87=E5=AE=9E?= =?UTF-8?q?=E9=99=85=E5=A4=A7=E5=B0=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/storage.py | 11 +++++++++++ tests/test_chunk_size_config.py | 20 ++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/core/storage.py b/core/storage.py index a4d877b..5768764 100644 --- a/core/storage.py +++ b/core/storage.py @@ -85,6 +85,12 @@ class FileStorageInterface: """ raise NotImplementedError + @staticmethod + def validate_stored_chunk_size(chunk_info: UploadChunk, index: int, data: bytes): + expected = min(chunk_info.chunk_size, chunk_info.file_size - index * chunk_info.chunk_size) + if len(data) != expected: + raise ValueError(f"分片{index}大小与上传声明不一致,请重新上传") + async def generate_presigned_upload_url(self, save_path: str, expires_in: int = 900) -> Optional[str]: """ 生成预签名上传URL @@ -245,6 +251,7 @@ class SystemFileStorage(FileStorageInterface): raise ValueError(f"分片{i}文件不存在") async with aiofiles.open(chunk_path, "rb") as in_file: chunk_data = await in_file.read() + self.validate_stored_chunk_size(chunk_info, i, chunk_data) current_hash = hashlib.sha256(chunk_data).hexdigest() if current_hash != chunk_record.chunk_hash: raise ValueError(f"分片{i}哈希不匹配: 期望 {chunk_record.chunk_hash}, 实际 {current_hash}") @@ -489,6 +496,7 @@ class S3FileStorage(FileStorageInterface): except Exception as e: raise ValueError(f"分片{i}文件不存在: {e}") + self.validate_stored_chunk_size(chunk_info, i, chunk_data) current_hash = hashlib.sha256(chunk_data).hexdigest() if current_hash != chunk_record.chunk_hash: raise ValueError(f"分片{i}哈希不匹配: 期望 {chunk_record.chunk_hash}, 实际 {current_hash}") @@ -843,6 +851,7 @@ class OneDriveFileStorage(FileStorageInterface): except Exception as e: raise ValueError(f"分片{i}文件不存在: {e}") + self.validate_stored_chunk_size(chunk_info, i, chunk_data) current_hash = hashlib.sha256(chunk_data).hexdigest() if current_hash != chunk_record.chunk_hash: raise ValueError(f"分片{i}哈希不匹配: 期望 {chunk_record.chunk_hash}, 实际 {current_hash}") @@ -1009,6 +1018,7 @@ class OpenDALFileStorage(FileStorageInterface): except Exception as e: raise ValueError(f"分片{i}文件不存在: {e}") + self.validate_stored_chunk_size(chunk_info, i, chunk_data) current_hash = hashlib.sha256(chunk_data).hexdigest() if current_hash != chunk_record.chunk_hash: raise ValueError(f"分片{i}哈希不匹配: 期望 {chunk_record.chunk_hash}, 实际 {current_hash}") @@ -1293,6 +1303,7 @@ class WebDAVFileStorage(WebDAVStreamMixin, WebDAVOffsetMixin, FileStorageInterfa chunk_data = await resp.read() # 验证哈希 + self.validate_stored_chunk_size(chunk_info, i, chunk_data) current_hash = hashlib.sha256(chunk_data).hexdigest() if current_hash != chunk_record.chunk_hash: raise ValueError(f"分片{i}哈希不匹配: 期望 {chunk_record.chunk_hash}, 实际 {current_hash}") diff --git a/tests/test_chunk_size_config.py b/tests/test_chunk_size_config.py index 8912610..96b750c 100644 --- a/tests/test_chunk_size_config.py +++ b/tests/test_chunk_size_config.py @@ -73,3 +73,23 @@ class ChunkSizeConfigTests(unittest.IsolatedAsyncioTestCase): from apps.base.models import FileCodes saved=await FileCodes.get(code=r.json()['detail']['code']) self.assertEqual((Path(tmp)/await saved.get_file_path()).read_bytes(),payload) + async def test_old_legacy_oversized_tail_cannot_publish_beyond_declared_size(self): + from pathlib import Path + from tempfile import TemporaryDirectory + from unittest.mock import patch + from core.storage import SystemFileStorage + from apps.base.models import FileCodes + import hashlib + await self.client.patch('/admin/config/update',json={'file_storage':'local','uploadSize':10}) + with TemporaryDirectory() as tmp,patch('core.storage.data_root',Path(tmp)): + uid='legacy-tail';path='share/legacy/file.txt' + await UploadChunk.create(upload_id=uid,chunk_index=-1,file_name='file.txt',file_size=10,chunk_size=3,total_chunks=4,chunk_hash='a'*64,save_path=path) + storage=SystemFileStorage() + for i in range(4): + data=b'abc';digest=hashlib.sha256(data).hexdigest() + await storage.save_chunk(uid,i,data,digest,path) + await UploadChunk.create(upload_id=uid,chunk_index=i,file_name='file.txt',file_size=10,chunk_size=3,total_chunks=4,chunk_hash=digest,save_path=path,completed=True) + response=await self.client.post(f'/chunk/upload/complete/{uid}',json={'expire_value':1,'expire_style':'day'}) + self.assertEqual(response.status_code,400,response.text) + self.assertFalse(await FileCodes.filter(upload_id=uid).exists()) + self.assertFalse((Path(tmp)/path).exists())