🐛 fix(upload): 校验旧会话末片实际大小
This commit is contained in:
1 parent
c2d743ba51
commit
5c1e1ef368
2 files changed
+31
No files matched your search
@@ -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}")
|
||||
|
||||
@@ -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())
|
||||
Reference in new issue
Block a user