From f7bae99fe45e474e991ad7551a2a15c02344c5b1 Mon Sep 17 00:00:00 2001 From: Orion Date: Tue, 6 Oct 2026 00:52:12 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix:=20=E4=B8=8A=E4=BC=A0?= =?UTF-8?q?=E5=89=8D=E6=8B=A6=E6=88=AA=E7=A9=BA=E6=96=87=E4=BB=B6=E5=B9=B6?= =?UTF-8?q?=E6=98=8E=E7=A1=AE=E6=8F=90=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 普通选择和拖拽均拒绝零字节文件,清空无效选择及文件哈希 * 提交时复核单文件和打包结果,阻止分片或预签名初始化空文件 * 补充空文件入口及非空上传回归,记录生产分片代理限制修复 --- src/composables/useSendFlow.ts | 6 +++ src/composables/useSendSubmit.ts | 5 +-- tests/README.md | 7 ++++ tests/emptyUpload.integration.test.mjs | 54 ++++++++++++++++++++++++++ 4 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 tests/emptyUpload.integration.test.mjs diff --git a/src/composables/useSendFlow.ts b/src/composables/useSendFlow.ts index e2c877d..cbfb55d 100644 --- a/src/composables/useSendFlow.ts +++ b/src/composables/useSendFlow.ts @@ -87,6 +87,12 @@ export function useSendFlow() { } const checkFileSize = (file: File) => { + if (file.size === 0) { + alertStore.showAlert(t('send.messages.emptyFileError'), 'error') + selectedFile.value = null + fileHash.value = '' + return false + } if (file.size > config.value.uploadSize) { alertStore.showAlert( t('send.messages.fileSizeExceeded', { size: getStorageUnit(config.value.uploadSize) }), diff --git a/src/composables/useSendSubmit.ts b/src/composables/useSendSubmit.ts index f3654f9..eb85a7b 100644 --- a/src/composables/useSendSubmit.ts +++ b/src/composables/useSendSubmit.ts @@ -111,9 +111,6 @@ export function useSendSubmit(options: UseSendSubmitOptions) { if (selectedFiles.length > 0) { options.notify('正在打包文件...', 'success') fileToUpload = await packFilesAsZip(selectedFiles) - if (!validateFileSize(fileToUpload)) { - return null - } options.onHashCalculated(await calculateFileHash(fileToUpload)) } @@ -121,6 +118,8 @@ export function useSendSubmit(options: UseSendSubmitOptions) { throw new Error(options.translate('send.messages.selectFile')) } + if (!validateFileSize(fileToUpload)) return null + operation.check() options.onProgress({ loaded:0,total:fileToUpload.size,percentage:0 }) const result = await (enableChunk diff --git a/tests/README.md b/tests/README.md index aea3c4b..79fc3eb 100644 --- a/tests/README.md +++ b/tests/README.md @@ -143,3 +143,10 @@ Playwright Page。Vite 固定运行于 `127.0.0.1:5180`;分别传入 `http` 可使用 `FCB_TEST_TOKEN` 或 `FCB_TEST_PASSWORD`;可选本地前端产物路径。 - 当前前端单元/集成 75 项通过,架构/类型/构建通过;Node 覆盖率不覆盖 SSR 动态 Vue 模块, 交互另经真实浏览器验证。此前自动随列表刷新的行为已撤销。 + +### 空文件上传校验(2026-10-06) + +- `emptyUpload.integration.test.mjs` 验证选文件、拖拽和提交复核;分片/预签名初始化前均拒绝零字节文件。 +- 非空文件仍携带真实大小初始化,多文件沿用原有 ZIP 打包行为。 +- 生产 90 MiB 分片需要反向代理请求体上限不小于分片大小;本次 413 来自 OpenResty 全局 50 MiB 限制。 + FileCodeBox 站点与测试站统一设置 128 MiB,关闭请求缓冲,读写超时 600 秒。 diff --git a/tests/emptyUpload.integration.test.mjs b/tests/emptyUpload.integration.test.mjs new file mode 100644 index 0000000..b980f98 --- /dev/null +++ b/tests/emptyUpload.integration.test.mjs @@ -0,0 +1,54 @@ +import { test, before, after } from 'node:test' +import assert from 'node:assert/strict' +import { createServer } from 'vite' +import { fileURLToPath } from 'node:url' +import { createSSRApp, h } from 'vue' +import { renderToString } from 'vue/server-renderer' +import { createPinia } from 'pinia' +import { createI18n } from 'vue-i18n' +let server, createFlow, service, presign, alerts, config, zh, oldStorage, oldWindow +before(async () => { + oldStorage=globalThis.localStorage;oldWindow=globalThis.window + globalThis.localStorage={getItem:()=>null,setItem(){},removeItem(){}} + globalThis.window={isSecureContext:true,location:{origin:'https://example.test'}} + server=await createServer({resolve:{alias:{'file-saver':fileURLToPath(new URL('./fixtures/browser-save.stub.mjs',import.meta.url))}},server:{middlewareMode:true,watch:null},appType:'custom'}) + createFlow=(await server.ssrLoadModule('/src/composables/useSendFlow.ts')).useSendFlow + const exports=await server.ssrLoadModule('/src/services/index.ts');service=exports.FileService;presign=exports.PresignUploadService + alerts=(await server.ssrLoadModule('/src/stores/alertStore.ts')).useAlertStore + config=(await server.ssrLoadModule('/src/stores/configStore.ts')).useConfigStore + zh=(await server.ssrLoadModule('/src/i18n/locales/zh-CN.ts')).default +}) +after(async()=>{await server?.close();globalThis.localStorage=oldStorage;globalThis.window=oldWindow}) +async function flow(enableChunk=1) { + let f,notifications=[] + const app=createSSRApp({setup(){alerts().showAlert=(message,type)=>notifications.push({message,type});config().config.enableChunk=enableChunk;f=createFlow();return()=>h('div')}}) + app.use(createPinia());app.use(createI18n({legacy:false,locale:'zh-CN',messages:{'zh-CN':zh}}));await renderToString(app) + return {f,notifications} +} +test('empty selection and drop are rejected with a friendly message and cleared file',async()=>{ + for(const kind of ['select','drop']){ + const {f,notifications}=await flow() + const file=new File([],'BNApp64-v3.13.5.apk') + if(kind==='select')await f.handleFileSelected(file);else await f.handleFileDrop({dataTransfer:{files:[file]}}) + assert.equal(f.selectedFile.value,null);assert.deepEqual(notifications,[{message:'无法读取空文件',type:'error'}]) + } +}) +test('submit revalidates selected file before both chunk and presign initialization',async()=>{ + const originals={chunk:service.initChunkUpload,presign:presign.initUpload};let requests=0 + service.initChunkUpload=presign.initUpload=async()=>{requests++;throw Error('request must not be sent')} + try{ + for(const enabled of [0,1]){ + const {f,notifications}=await flow(enabled);f.selectedFile.value=new File([],'empty.apk') + await f.handleSubmit();assert.equal(requests,0);assert.equal(f.selectedFile.value,null);assert.equal(f.isSubmitting.value,false) + assert.deepEqual(notifications,[{message:'无法读取空文件',type:'error'}]) + } + }finally{service.initChunkUpload=originals.chunk;presign.initUpload=originals.presign} +}) +test('nonempty file initialization still carries its actual size',async()=>{ + const {f}=await flow();const original=service.initChunkUpload;let request + service.initChunkUpload=async data=>{request=data;throw Error('stop after initialization')} + try{ + f.selectedFile.value=new File(['apk-data'],'valid.apk');await f.handleSubmit() + assert.equal(request.file_size,8);assert.equal(request.chunk_size,8);assert.equal(f.isSubmitting.value,false) + }finally{service.initChunkUpload=original} +})