fix: "no content-type" errors receiving random POST requests #317 #327
Massimo Melina committed
Aug 27, 2023 at 20:29 UTC
e685812673a806f8fe5f7c73bf1ed62b2ca56d5a
1 file changed
+9
-5
src/middlewares.ts
+9
-5
@@ -6,7 +6,7 @@ import {
6
ADMIN_URI, API_URI,
7
BUILD_TIMESTAMP,
8
DEV, DAY,
9
- HTTP_FORBIDDEN, HTTP_NOT_FOUND, HTTP_FOOL, HTTP_UNAUTHORIZED,
9
+ HTTP_FORBIDDEN, HTTP_NOT_FOUND, HTTP_FOOL, HTTP_UNAUTHORIZED, HTTP_BAD_REQUEST,
10
} from './const'
11
import { FRONTEND_URI } from './const'
12
import { statusCodeForMissingPerm, nodeIsDirectory, urlToNode, vfs, walkNode, VfsNode, getNodeName } from './vfs'
@@ -23,7 +23,7 @@ import { zipStreamFromFolder } from './zip'
23
import { serveFile, serveFileNode } from './serveFile'
24
import { serveGuiFiles } from './serveGuiFiles'
25
import mount from 'koa-mount'
26
-import { once, Readable } from 'stream'
26
+import { Readable } from 'stream'
27
import { applyBlock } from './block'
28
import { getAccount } from './perm'
29
import { socket2connection, updateConnection, normalizeIp } from './connections'
@@ -121,15 +121,19 @@ export const serveGuiAndSharedFiles: Koa.Middleware = async (ctx, next) => {
121
if (!node)
122
return sendErrorPage(ctx, HTTP_NOT_FOUND)
123
if (ctx.method === 'POST') { // curl -F upload=@file url/
124
+ if (ctx.request.type !== 'multipart/form-data')
125
+ return ctx.status = HTTP_BAD_REQUEST
126
ctx.body = {}
127
const form = formidable({
128
maxFileSize: Infinity,
129
+ allowEmptyFiles: true,
130
//@ts-ignore wrong in the .d.ts file
131
fileWriteStreamHandler: f => uploadWriter(node, f.originalFilename, ctx)
132
})
130
- form.parse(ctx.req)
131
- await once(form, 'end').catch(()=> {})
132
- return
133
+ return new Promise<void>(res => form.parse(ctx.req, err => {
134
+ if (err) console.error(String(err))
135
+ res()
136
+ }))
137
}
138
if (!await nodeIsDirectory(node))
139
return !node.source && await next()