@samitouri / QOSami-HFS / commits / 782fd4b7

fix: some bad PUT requests were logged with 200 instead of 400

Massimo Melina committed Apr 3, 2025 at 15:16 UTC 782fd4b722d9555db280e917eee8d86d250545a6
3 files changed +6 -2
src/misc.ts
+1 -1
@@ -134,7 +134,7 @@ export function createStreamLimiter(limit: number) {
134 const left = limit - got
135 got += chunk.length
136 if (left > 0) {
137 - this.push(chunk.length >= left ? chunk.slice(0, left) : chunk)
137 + this.push(chunk.length > left ? chunk.slice(0, left) : chunk)
138 if (got >= limit)
139 this.end()
140 }
src/serveGuiAndSharedFiles.ts
+4 -1
@@ -70,7 +70,10 @@ export const serveGuiAndSharedFiles: Koa.Middleware = async (ctx, next) => {
70 })
71 ctx.req.on('close', () => dest.end())
72 const uri = await dest.lockMiddleware // we need to wait more than just the stream
73 - ctx.body = { uri }
73 + if (uri) // falsy = aborted
74 + ctx.body = { uri }
75 + else
76 + ctx.status = 400 // nodejs already sent 400, but koa ignores it (ctx.headersSent is false and ctx.status is 404), so we adjust to have correct data in the log
77 }
78 return
79 }
tests/test.ts
+1
@@ -238,6 +238,7 @@ describe('after-login', () => {
238 it('upload.too much', async () => {
239 const fn = 'temp/tooMuch'
240 const wrongSize = BIG_CONTENT.length / 2
241 + // the 200 is the result when nodejs doesn't intercept the mismatch (sending 400), and the case is handled by the application layer. Nodejs intervention can vary with the version and the declared size.
242 await reqUpload(UPLOAD_ROOT + fn, 200, BIG_CONTENT, wrongSize)()
243 const { size } = statSync(ROOT + fn)
244 if (size !== wrongSize)