|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const test = require('node:test') |
| 4 | +const Fastify = require('fastify') |
| 5 | +const multipart = require('..') |
| 6 | +const http = require('node:http') |
| 7 | +const net = require('node:net') |
| 8 | +const { once } = require('node:events') |
| 9 | +const { setTimeout: sleep } = require('node:timers/promises') |
| 10 | + |
| 11 | +const BOUNDARY = 'fix630boundary' |
| 12 | + |
| 13 | +function filePart (fieldname, filename, payload) { |
| 14 | + return Buffer.concat([ |
| 15 | + Buffer.from( |
| 16 | + `--${BOUNDARY}\r\n` + |
| 17 | + `Content-Disposition: form-data; name="${fieldname}"; filename="${filename}"\r\n` + |
| 18 | + 'Content-Type: application/octet-stream\r\n\r\n' |
| 19 | + ), |
| 20 | + payload, |
| 21 | + Buffer.from('\r\n') |
| 22 | + ]) |
| 23 | +} |
| 24 | + |
| 25 | +function fieldPart (fieldname, value) { |
| 26 | + return Buffer.from( |
| 27 | + `--${BOUNDARY}\r\n` + |
| 28 | + `Content-Disposition: form-data; name="${fieldname}"\r\n\r\n` + |
| 29 | + `${value}\r\n` |
| 30 | + ) |
| 31 | +} |
| 32 | + |
| 33 | +const closingBoundary = Buffer.from(`--${BOUNDARY}--\r\n`) |
| 34 | + |
| 35 | +// Reproduces https://github.com/fastify/fastify-multipart/issues/630 |
| 36 | +// The request emits 'close' once its body is fully piped into busboy, |
| 37 | +// which can happen while the consumer is still slowly reading an earlier |
| 38 | +// file part and busboy is still holding undelivered parts. Those trailing |
| 39 | +// parts must still be yielded instead of being cut off by the end marker. |
| 40 | +test('parts() delivers trailing parts even when the request closes while busboy still buffers them', async function (t) { |
| 41 | + t.plan(2) |
| 42 | + |
| 43 | + const fastify = Fastify() |
| 44 | + t.after(() => fastify.close()) |
| 45 | + |
| 46 | + // a large busboy highWaterMark lets the whole request body drain into |
| 47 | + // busboy's writable buffer at once, so the request emits 'close' while |
| 48 | + // busboy still holds undelivered parts. The same ordering happens with |
| 49 | + // the default highWaterMark when a proxy re-segments the stream. |
| 50 | + fastify.register(multipart, { highWaterMark: 2 * 1024 * 1024 }) |
| 51 | + |
| 52 | + fastify.post('/', async function (req) { |
| 53 | + const seen = [] |
| 54 | + for await (const part of req.parts()) { |
| 55 | + if (part.type === 'file') { |
| 56 | + let size = 0 |
| 57 | + for await (const chunk of part.file) { |
| 58 | + size += chunk.length |
| 59 | + // simulate a slow storage write so the request finishes piping |
| 60 | + // (and emits 'close') while we are still consuming this file |
| 61 | + await sleep(10) |
| 62 | + } |
| 63 | + seen.push({ type: 'file', fieldname: part.fieldname, size }) |
| 64 | + } else { |
| 65 | + seen.push({ type: 'field', fieldname: part.fieldname }) |
| 66 | + } |
| 67 | + } |
| 68 | + return { seen } |
| 69 | + }) |
| 70 | + |
| 71 | + await fastify.listen({ port: 0 }) |
| 72 | + |
| 73 | + const image = Buffer.alloc(200 * 1024, 'a') |
| 74 | + const mask = Buffer.alloc(6 * 1024, 'b') |
| 75 | + const body = Buffer.concat([ |
| 76 | + filePart('file', 'image.bin', image), |
| 77 | + filePart('mask', 'mask.bin', mask), |
| 78 | + fieldPart('clientJobId', 'job-1'), |
| 79 | + fieldPart('format', 'png'), |
| 80 | + fieldPart('quality', '90'), |
| 81 | + closingBoundary |
| 82 | + ]) |
| 83 | + |
| 84 | + const req = http.request({ |
| 85 | + protocol: 'http:', |
| 86 | + hostname: 'localhost', |
| 87 | + port: fastify.server.address().port, |
| 88 | + path: '/', |
| 89 | + method: 'POST', |
| 90 | + headers: { |
| 91 | + 'content-type': `multipart/form-data; boundary=${BOUNDARY}`, |
| 92 | + 'content-length': body.length |
| 93 | + } |
| 94 | + }) |
| 95 | + req.end(body) |
| 96 | + |
| 97 | + const [res] = await once(req, 'response') |
| 98 | + t.assert.strictEqual(res.statusCode, 200) |
| 99 | + |
| 100 | + const chunks = [] |
| 101 | + for await (const chunk of res) { |
| 102 | + chunks.push(chunk) |
| 103 | + } |
| 104 | + const { seen } = JSON.parse(Buffer.concat(chunks)) |
| 105 | + t.assert.deepStrictEqual(seen, [ |
| 106 | + { type: 'file', fieldname: 'file', size: image.length }, |
| 107 | + { type: 'file', fieldname: 'mask', size: mask.length }, |
| 108 | + { type: 'field', fieldname: 'clientJobId' }, |
| 109 | + { type: 'field', fieldname: 'format' }, |
| 110 | + { type: 'field', fieldname: 'quality' } |
| 111 | + ]) |
| 112 | +}) |
| 113 | + |
| 114 | +test('parts() rejects when the multipart data is truncated instead of ending cleanly', async function (t) { |
| 115 | + t.plan(3) |
| 116 | + |
| 117 | + const fastify = Fastify() |
| 118 | + t.after(() => fastify.close()) |
| 119 | + |
| 120 | + fastify.register(multipart) |
| 121 | + |
| 122 | + fastify.post('/', async function (req, reply) { |
| 123 | + try { |
| 124 | + for await (const part of req.parts()) { |
| 125 | + if (part.file) { |
| 126 | + await part.toBuffer() |
| 127 | + } |
| 128 | + } |
| 129 | + } catch (err) { |
| 130 | + t.assert.strictEqual(err.message, 'Unexpected end of multipart data') |
| 131 | + return reply.code(400).send({ error: 'truncated' }) |
| 132 | + } |
| 133 | + return reply.send({ error: 'iteration ended cleanly' }) |
| 134 | + }) |
| 135 | + |
| 136 | + await fastify.listen({ port: 0 }) |
| 137 | + |
| 138 | + // a complete first part but no closing boundary: the request body ends |
| 139 | + // normally at the HTTP layer while the multipart data is truncated |
| 140 | + const body = Buffer.concat([ |
| 141 | + filePart('file', 'image.bin', Buffer.alloc(1024, 'a')), |
| 142 | + fieldPart('clientJobId', 'job-1') |
| 143 | + ]) |
| 144 | + |
| 145 | + const req = http.request({ |
| 146 | + protocol: 'http:', |
| 147 | + hostname: 'localhost', |
| 148 | + port: fastify.server.address().port, |
| 149 | + path: '/', |
| 150 | + method: 'POST', |
| 151 | + headers: { |
| 152 | + 'content-type': `multipart/form-data; boundary=${BOUNDARY}`, |
| 153 | + 'content-length': body.length |
| 154 | + } |
| 155 | + }) |
| 156 | + req.end(body) |
| 157 | + |
| 158 | + const [res] = await once(req, 'response') |
| 159 | + t.assert.strictEqual(res.statusCode, 400) |
| 160 | + res.resume() |
| 161 | + await once(res, 'end') |
| 162 | + t.assert.ok('request completed') |
| 163 | +}) |
| 164 | + |
| 165 | +test('parts() rejects when the client aborts mid-upload instead of ending cleanly', async function (t) { |
| 166 | + t.plan(1) |
| 167 | + |
| 168 | + const fastify = Fastify() |
| 169 | + t.after(() => fastify.close()) |
| 170 | + |
| 171 | + fastify.register(multipart) |
| 172 | + |
| 173 | + let handled |
| 174 | + const handledPromise = new Promise((resolve) => { handled = resolve }) |
| 175 | + |
| 176 | + fastify.post('/', async function (req, reply) { |
| 177 | + try { |
| 178 | + for await (const part of req.parts()) { |
| 179 | + if (part.file) { |
| 180 | + for await (const chunk of part.file) { |
| 181 | + chunk.toString() |
| 182 | + } |
| 183 | + } |
| 184 | + } |
| 185 | + handled({ outcome: 'clean end' }) |
| 186 | + } catch (err) { |
| 187 | + handled({ outcome: 'error', message: err.message }) |
| 188 | + } |
| 189 | + return reply.send() |
| 190 | + }) |
| 191 | + |
| 192 | + await fastify.listen({ port: 0 }) |
| 193 | + |
| 194 | + const body = Buffer.concat([ |
| 195 | + filePart('file', 'image.bin', Buffer.alloc(64 * 1024, 'a')), |
| 196 | + fieldPart('clientJobId', 'job-1'), |
| 197 | + closingBoundary |
| 198 | + ]) |
| 199 | + |
| 200 | + const socket = net.connect(fastify.server.address().port, 'localhost') |
| 201 | + await once(socket, 'connect') |
| 202 | + socket.write( |
| 203 | + 'POST / HTTP/1.1\r\n' + |
| 204 | + 'Host: localhost\r\n' + |
| 205 | + `Content-Type: multipart/form-data; boundary=${BOUNDARY}\r\n` + |
| 206 | + `Content-Length: ${body.length}\r\n` + |
| 207 | + '\r\n' |
| 208 | + ) |
| 209 | + // send only part of the body, then abort the connection |
| 210 | + socket.write(body.subarray(0, 16 * 1024)) |
| 211 | + await sleep(100) |
| 212 | + socket.destroy() |
| 213 | + |
| 214 | + const result = await handledPromise |
| 215 | + t.assert.strictEqual(result.outcome, 'error', `iteration must reject on abort, got: ${JSON.stringify(result)}`) |
| 216 | +}) |
0 commit comments