Skip to content

Commit a70dc70

Browse files
authored
Merge pull request from GHSA-qh73-qc3p-rjv2
1 parent 25c0bed commit a70dc70

File tree

3 files changed

+102
-4
lines changed

3 files changed

+102
-4
lines changed

index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ function attachToBody (options, req, reply, next) {
6060
}, options)
6161

6262
mp.on('field', (key, value) => {
63-
if (key === '__proto__') {
64-
mp.destroy(new Error('__proto__ is not allowed as field name'))
63+
if (key === '__proto__' || key === 'constructor') {
64+
mp.destroy(new Error(`${key} is not allowed as field name`))
6565
return
6666
}
6767
if (body[key] === undefined) {
@@ -257,8 +257,8 @@ function fastifyMultipart (fastify, options, done) {
257257
log.debug({ field, filename, encoding, mimetype }, 'parsing part')
258258
files++
259259
eos(file, waitForFiles)
260-
if (field === '__proto__') {
261-
file.destroy(new Error('__proto__ is not allowed as field name'))
260+
if (field === '__proto__' || field === 'constructor') {
261+
file.destroy(new Error(`${field} is not allowed as field name`))
262262
return
263263
}
264264
handler(field, file, filename, encoding, mimetype)

test/legacy/append-body.test.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -785,3 +785,49 @@ test('addToBody with __proto__ field', t => {
785785
})
786786
})
787787
})
788+
789+
test('addToBody with constructor field', t => {
790+
t.plan(3)
791+
792+
const fastify = Fastify()
793+
t.teardown(fastify.close.bind(fastify))
794+
795+
const opts = {
796+
addToBody: true,
797+
onFile: (fieldName, stream, filename, encoding, mimetype) => {
798+
t.fail('there are not stream')
799+
}
800+
}
801+
fastify.register(multipart, opts)
802+
803+
fastify.post('/', function (req, reply) {
804+
t.fail('should not be called')
805+
})
806+
807+
fastify.listen(0, function () {
808+
// request
809+
const form = new FormData()
810+
const opts = {
811+
protocol: 'http:',
812+
hostname: 'localhost',
813+
port: fastify.server.address().port,
814+
path: '/',
815+
headers: form.getHeaders(),
816+
method: 'POST'
817+
}
818+
819+
const req = http.request(opts, (res) => {
820+
t.equal(res.statusCode, 500)
821+
res.resume()
822+
res.on('end', () => {
823+
t.pass('res ended successfully')
824+
})
825+
})
826+
827+
form.append('myField', 'hello')
828+
form.append('constructor', 'world')
829+
pump(form, req, function (err) {
830+
t.error(err, 'client pump: no err')
831+
})
832+
})
833+
})

test/legacy/multipart.test.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,3 +478,55 @@ test('should not allow __proto__', { skip: process.platform === 'win32' }, funct
478478
})
479479
})
480480
})
481+
482+
test('should not allow constructor', { skip: process.platform === 'win32' }, function (t) {
483+
t.plan(5)
484+
485+
const fastify = Fastify()
486+
t.teardown(fastify.close.bind(fastify))
487+
488+
fastify.register(multipart, { limits: { fields: 1 } })
489+
490+
fastify.post('/', function (req, reply) {
491+
t.ok(req.isMultipart())
492+
493+
const mp = req.multipart(handler, function (err) {
494+
t.equal(err.message, 'constructor is not allowed as field name')
495+
reply.code(500).send()
496+
})
497+
498+
mp.on('field', function (name, value) {
499+
t.fail('should not be called')
500+
})
501+
502+
function handler (field, file, filename, encoding, mimetype) {
503+
t.fail('should not be called')
504+
}
505+
})
506+
507+
fastify.listen(0, function () {
508+
// request
509+
const form = new FormData()
510+
const opts = {
511+
protocol: 'http:',
512+
hostname: 'localhost',
513+
port: fastify.server.address().port,
514+
path: '/',
515+
headers: form.getHeaders(),
516+
method: 'POST'
517+
}
518+
519+
const req = http.request(opts, (res) => {
520+
t.equal(res.statusCode, 500)
521+
res.resume()
522+
res.on('end', () => {
523+
t.pass('res ended successfully')
524+
})
525+
})
526+
const rs = fs.createReadStream(filePath)
527+
form.append('constructor', rs)
528+
pump(form, req, function (err) {
529+
t.error(err, 'client pump: no err')
530+
})
531+
})
532+
})

0 commit comments

Comments
 (0)