fix: possible "Boundary not found" in console
Massimo Melina committed
Mar 20, 2026 at 14:48 UTC
012b06c944f23cc5c886e73648f193aa0cc9e50e
2 files changed
+11
-1
src/multipartUpload.ts
+6
-1
@@ -6,6 +6,7 @@ import { dirname } from 'path'
6
import { uploadWriter } from './upload'
7
import { HTTP_BAD_REQUEST } from './cross-const'
8
import { onFirstEvent } from './first'
9
+import { try_ } from './cross'
10
11
export async function handleMultipartUpload(ctx: Koa.Context, node: VfsNode) {
12
if (ctx.request.type !== 'multipart/form-data')
@@ -14,7 +15,11 @@ export async function handleMultipartUpload(ctx: Koa.Context, node: VfsNode) {
15
const locks: Promise<string>[] = []
16
const fileJobs: Promise<any>[] = []
17
const errors: string[] = []
17
- const bb = Busboy({ headers: ctx.req.headers, preservePath: true })
18
+ const bb = try_(() => Busboy({ headers: ctx.req.headers, preservePath: true }), e => {
19
+ ctx.body = String(e) // busboy validates multipart headers at construction time, so malformed requests must stop here as 400
20
+ ctx.status = HTTP_BAD_REQUEST
21
+ })
22
+ if (!bb) return
23
bb.on('field', (name: string) => {
24
if (name === 'upload')
25
errors.push('empty filename')
tests/test.ts
+5
@@ -194,6 +194,11 @@ describe('basics', () => {
194
if (!['empty filename', 'no files'].includes(errMsg))
195
throw 'missing error'
196
})
197
+ test('upload.post.missing-boundary', async () => {
198
+ const { status } = await curlWithStatus(`printf 'x' | curl -s -u ${auth} -H "Content-Type: multipart/form-data" --data-binary @- ${BASE_URL}${UPLOAD_ROOT}`)
199
+ if (status !== 400)
200
+ throw "unexpected status " + status
201
+ })
202
test('upload.post.absolute filename', async () => {
203
const absPath = resolve(__dirname, `abs-${randomId(6)}.txt`)
204
const absForBody = absPath.replace(/\\\\/g, '/')