-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathmake-middleware.js
More file actions
363 lines (301 loc) · 11.3 KB
/
Copy pathmake-middleware.js
File metadata and controls
363 lines (301 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
var is = require('type-is')
var AsyncResource = require('async_hooks').AsyncResource
var Busboy = require('busboy')
var appendField = require('append-field')
var Counter = require('./counter')
var MulterError = require('./multer-error')
var FileAppender = require('./file-appender')
var removeUploadedFiles = require('./remove-uploaded-files')
// append-field turns a bracket group whose contents are all digits into an
// array index, so `a[3]` produces an array of length 4. The index itself is
// unbounded, which means a single field name can materialize a sparse array
// with a very large length. That costs append-field almost nothing, but it is
// the application that pays when it later iterates or serializes req.body, and
// until now there was no limit to opt into.
function exceedsArrayIndexLimit (fieldname, limit) {
// Only field names append-field parses as a bracket path build an array;
// names it stores as a literal key (e.g. a[6]suffix, [6]) never do, so they
// must not be rejected by the index limit.
if (!/^[^[]+(?:\[[^\]]+\])*(?:\[\])?$/.test(fieldname)) return false
var pattern = /\[(\d+)\]/g
var match
while ((match = pattern.exec(fieldname)) !== null) {
if (Number(match[1]) > limit) return true
}
return false
}
function drainStream (stream) {
stream.on('readable', () => {
while (stream.read() !== null) {}
})
}
// The WHATWG HTML spec requires user agents to escape the bytes 0x0A (LF),
// 0x0D (CR) and 0x22 (") as %0A, %0D and %22 when serialising field names and
// filenames in a multipart/form-data body. Busboy leaves them escaped, so we
// reverse exactly those three sequences here to recover the original name.
// Only these are decoded on purpose: a bare `%` is never escaped by the spec,
// so a full decodeURIComponent would corrupt legitimate names like `50%.pdf`.
// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#multipart-form-data
function decodeFormDataName (str) {
return str.replace(/%0A|%0D|%22/gi, function (match) {
switch (match.toUpperCase()) {
case '%0A': return '\n'
case '%0D': return '\r'
default: return '"'
}
})
}
function makeMiddleware (setup) {
return function multerMiddleware (req, res, next) {
// Preserve the caller's async context (AsyncLocalStorage, CLS) across the
// busboy stream events that eventually call next(). runInAsyncScope exists
// since Node 9 and passes arguments through on every version, unlike the
// static AsyncResource.bind, which drops them on Node 14.
var resource = new AsyncResource('multer')
var originalNext = next
next = function (err) {
resource.runInAsyncScope(originalNext, null, err)
}
if (!is(req, ['multipart'])) return next()
var options = setup()
var limits = options.limits
var busboyLimits = limits
if (limits && Object.prototype.hasOwnProperty.call(limits, 'fileSize')) {
busboyLimits = {}
var key
for (key in limits) {
busboyLimits[key] = limits[key]
}
if (typeof limits.fileSize === 'number' && isFinite(limits.fileSize)) {
busboyLimits.fileSize = limits.fileSize + 1
}
}
var storage = options.storage
var fileFilter = options.fileFilter
var fileStrategy = options.fileStrategy
var preservePath = options.preservePath
var defParamCharset = options.defParamCharset
var defCharset = options.defCharset
var highWaterMark = options.highWaterMark
var fileHwm = options.fileHwm
req.body = Object.create(null)
var busboy
var appender = null
var isDone = false
var readFinished = false
var errorOccured = false
var pendingWrites = new Counter()
var uploadedFiles = []
var pendingFiles = []
function done (err) {
var called = false
function onFinished () {
if (called) return
called = true
next(err)
}
if (isDone) return
isDone = true
if (busboy) {
req.unpipe(busboy)
setImmediate(() => {
busboy.removeAllListeners()
})
}
drainStream(req)
req.resume()
// - if responding with an error, drain request body before calling
// next(err) -- avoids EPIPE on the client (server closing connection
// while the client is still sending the request body)
// - also listen for 'close' so we don't hang when the client aborts (stream may never 'end')
// - skip waiting if the stream is already destroyed (e.g. client aborted)
if (err && req.readable && !req.destroyed) {
req.once('end', onFinished)
req.once('error', onFinished)
req.once('close', onFinished)
return
}
next(err)
}
function indicateDone () {
if (readFinished && pendingWrites.isZero() && !errorOccured) done()
}
function abortWithError (uploadError, skipPendingWait) {
if (errorOccured) return
errorOccured = true
function finishAbort () {
function remove (file, cb) {
storage._removeFile(req, file, cb)
}
var filesToRemove = uploadedFiles.concat(
pendingFiles.filter(function (f) { return f.path })
)
pendingFiles = []
removeUploadedFiles(filesToRemove, remove, function (err, storageErrors) {
if (err) return done(err)
uploadError.storageErrors = storageErrors
done(uploadError)
})
}
if (skipPendingWait) {
finishAbort()
} else {
pendingWrites.onceZero(finishAbort)
}
}
function abortWithCode (code, optionalField) {
abortWithError(new MulterError(code, optionalField))
}
function handleRequestFailure (err) {
if (isDone) return
if (busboy) {
req.unpipe(busboy)
busboy.destroy(err)
}
abortWithError(err, true)
}
req.on('error', function (err) {
handleRequestFailure(err || new Error('Request error'))
})
req.on('aborted', function () {
handleRequestFailure(new Error('Request aborted'))
})
req.on('close', function () {
if (req.readableEnded) return
handleRequestFailure(new Error('Request closed'))
})
try {
busboy = Busboy({
headers: req.headers,
limits: busboyLimits,
preservePath: preservePath,
defParamCharset: defParamCharset,
defCharset: defCharset,
highWaterMark: highWaterMark,
fileHwm: fileHwm
})
} catch (err) {
return next(err)
}
appender = new FileAppender(fileStrategy, req)
// handle text field data
busboy.on('field', function (fieldname, value, { nameTruncated, valueTruncated }) {
if (fieldname == null) return abortWithCode('MISSING_FIELD_NAME')
if (nameTruncated) return abortWithCode('LIMIT_FIELD_KEY')
if (valueTruncated) return abortWithCode('LIMIT_FIELD_VALUE', fieldname)
// Work around bug in Busboy (https://github.com/mscdex/busboy/issues/6)
if (limits && Object.prototype.hasOwnProperty.call(limits, 'fieldNameSize')) {
if (fieldname.length > limits.fieldNameSize) return abortWithCode('LIMIT_FIELD_KEY')
}
if (limits && Object.prototype.hasOwnProperty.call(limits, 'fieldNestingDepth')) {
if (fieldname.split('[').length - 1 > limits.fieldNestingDepth) return abortWithCode('LIMIT_FIELD_NESTING', fieldname)
}
if (limits && Object.prototype.hasOwnProperty.call(limits, 'fieldArrayIndexLimit')) {
if (exceedsArrayIndexLimit(fieldname, limits.fieldArrayIndexLimit)) {
return abortWithCode('LIMIT_FIELD_ARRAY_INDEX', fieldname)
}
}
try {
appendField(req.body, fieldname, value)
} catch {
return abortWithCode('INVALID_FIELD_NAME', fieldname)
}
})
// handle files
busboy.on('file', function (fieldname, fileStream, { filename, encoding, mimeType }) {
var pendingWritesIncremented = false
var aborting = false
var accepted = false
var fileSizeLimitReached = false
function decrementPendingWrites () {
if (!pendingWritesIncremented) return
pendingWritesIncremented = false
pendingWrites.decrement()
}
fileStream.on('error', function (err) {
decrementPendingWrites()
abortWithError(err)
})
// Register 'limit' synchronously so an async fileFilter can't miss it.
// Only abort once the file has been accepted.
fileStream.on('limit', function () {
fileSizeLimitReached = true
if (accepted) {
aborting = true
abortWithCode('LIMIT_FILE_SIZE', fieldname)
}
})
if (fieldname == null) return abortWithCode('MISSING_FIELD_NAME')
// don't attach to the files object, if there is no file
if (!filename) return fileStream.resume()
// Work around bug in Busboy (https://github.com/mscdex/busboy/issues/6)
if (limits && Object.prototype.hasOwnProperty.call(limits, 'fieldNameSize')) {
if (fieldname.length > limits.fieldNameSize) return abortWithCode('LIMIT_FIELD_KEY')
}
var file = {
fieldname: fieldname,
originalname: decodeFormDataName(filename),
encoding: encoding,
mimetype: mimeType
}
var placeholder = appender.insertPlaceholder(file)
fileFilter(req, file, function (err, includeFile) {
if (errorOccured) {
appender.removePlaceholder(placeholder)
return fileStream.resume()
}
if (err) {
appender.removePlaceholder(placeholder)
return abortWithError(err)
}
if (!includeFile) {
appender.removePlaceholder(placeholder)
return fileStream.resume()
}
// 'limit' may have fired while an async fileFilter was pending.
if (fileSizeLimitReached) {
appender.removePlaceholder(placeholder)
return abortWithCode('LIMIT_FILE_SIZE', fieldname)
}
accepted = true
pendingWritesIncremented = true
pendingWrites.increment()
Object.defineProperty(file, 'stream', {
configurable: true,
enumerable: false,
value: fileStream
})
pendingFiles.push(file)
storage._handleFile(req, file, function (err, info) {
var idx = pendingFiles.indexOf(file)
if (idx !== -1) pendingFiles.splice(idx, 1)
if (aborting) {
appender.removePlaceholder(placeholder)
uploadedFiles.push({ ...file, ...info })
return decrementPendingWrites()
}
if (err) {
appender.removePlaceholder(placeholder)
decrementPendingWrites()
return abortWithError(err)
}
var fileInfo = { ...file, ...info }
appender.replacePlaceholder(placeholder, fileInfo)
uploadedFiles.push(fileInfo)
decrementPendingWrites()
indicateDone()
})
})
})
busboy.on('error', function (err) { abortWithError(err) })
busboy.on('partsLimit', function () { abortWithCode('LIMIT_PART_COUNT') })
busboy.on('filesLimit', function () { abortWithCode('LIMIT_FILE_COUNT') })
busboy.on('fieldsLimit', function () { abortWithCode('LIMIT_FIELD_COUNT') })
busboy.on('close', function () {
readFinished = true
indicateDone()
})
req.pipe(busboy)
}
}
module.exports = makeMiddleware