CVE-2021-23597 (published 2022-02-11T17:15Z)

This affects the package fastify-multipart before 5.3.1. By providing a name=constructor property it is still possible to crash the application. **Note:** This is a bypass of CVE-2020-8136 (https://security.snyk.io/vuln/SNYK-JS-FASTIFYMULTIPART-1290382).

Severity: MEDIUM
CVSS v2 base score: 5.0 (AV:N/AC:L/Au:N/C:N/I:N/A:P)
CVSS v3 base score: 7.5, HIGH (CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:N/I:N/A:H)

Weakness information:
cwe_id: CWE-1321
cwe_name: Improperly Controlled Modification of Object Prototype Attributes ('Prototype Pollution')
description: The product receives input from an upstream component that specifies attributes that are to be initialized or updated in an object, but it does not properly control modifications of attributes of the object prototype.
extended_description: 
				By adding or modifying attributes of an object prototype, it is possible to create attributes that exist on every object, or replace critical attributes with malicious ones. This can be problematic if the product depends on existence or non-existence of certain attributes, or uses pre-defined attributes of object prototype (such as hasOwnProperty, toString or valueOf).
				This weakness is usually exploited by using a special attribute of objects called proto,  constructor or prototype. Such attributes give access to the object prototype. This weakness is often found in code that assigns object attributes based on user input, or merges or clones objects recursively.
			
			

Fix information:
repo_url: https://github.com/fastify/fastify-multipart
commit_details: repo_url: https://github.com/fastify/fastify-multipart
author: Matteo Collina
author_date: 2022-02-11 10:44:05+01:00
msg: Merge pull request from GHSA-qh73-qc3p-rjv2
repository: repo_url: https://github.com/fastify/fastify-multipart
repo_name: fastify/fastify-multipart
description: Multipart support for Fastify
repo_language: JavaScript
owner: fastify
file_changes: filename: index.js
change_type: ModificationType.MODIFY
diff: @@ -60,8 +60,8 @@ function attachToBody (options, req, reply, next) {
   }, options)
 
   mp.on('field', (key, value) => {
-    if (key === '__proto__') {
-      mp.destroy(new Error('__proto__ is not allowed as field name'))
+    if (key === '__proto__' || key === 'constructor') {
+      mp.destroy(new Error(`${key} is not allowed as field name`))
       return
     }
     if (body[key] === undefined) {
@@ -257,8 +257,8 @@ function fastifyMultipart (fastify, options, done) {
       log.debug({ field, filename, encoding, mimetype }, 'parsing part')
       files++
       eos(file, waitForFiles)
-      if (field === '__proto__') {
-        file.destroy(new Error('__proto__ is not allowed as field name'))
+      if (field === '__proto__' || field === 'constructor') {
+        file.destroy(new Error(`${field} is not allowed as field name`))
         return
       }
       handler(field, file, filename, encoding, mimetype)

programming_language: JavaScript
filename: append-body.test.js
change_type: ModificationType.MODIFY
diff: @@ -785,3 +785,49 @@ test('addToBody with __proto__ field', t => {
     })
   })
 })
+
+test('addToBody with constructor field', t => {
+  t.plan(3)
+
+  const fastify = Fastify()
+  t.teardown(fastify.close.bind(fastify))
+
+  const opts = {
+    addToBody: true,
+    onFile: (fieldName, stream, filename, encoding, mimetype) => {
+      t.fail('there are not stream')
+    }
+  }
+  fastify.register(multipart, opts)
+
+  fastify.post('/', function (req, reply) {
+    t.fail('should not be called')
+  })
+
+  fastify.listen(0, function () {
+    // request
+    const form = new FormData()
+    const opts = {
+      protocol: 'http:',
+      hostname: 'localhost',
+      port: fastify.server.address().port,
+      path: '/',
+      headers: form.getHeaders(),
+      method: 'POST'
+    }
+
+    const req = http.request(opts, (res) => {
+      t.equal(res.statusCode, 500)
+      res.resume()
+      res.on('end', () => {
+        t.pass('res ended successfully')
+      })
+    })
+
+    form.append('myField', 'hello')
+    form.append('constructor', 'world')
+    pump(form, req, function (err) {
+      t.error(err, 'client pump: no err')
+    })
+  })
+})

programming_language: JavaScript
filename: multipart.test.js
change_type: ModificationType.MODIFY
diff: @@ -478,3 +478,55 @@ test('should not allow __proto__', { skip: process.platform === 'win32' }, funct
     })
   })
 })
+
+test('should not allow constructor', { skip: process.platform === 'win32' }, function (t) {
+  t.plan(5)
+
+  const fastify = Fastify()
+  t.teardown(fastify.close.bind(fastify))
+
+  fastify.register(multipart, { limits: { fields: 1 } })
+
+  fastify.post('/', function (req, reply) {
+    t.ok(req.isMultipart())
+
+    const mp = req.multipart(handler, function (err) {
+      t.equal(err.message, 'constructor is not allowed as field name')
+      reply.code(500).send()
+    })
+
+    mp.on('field', function (name, value) {
+      t.fail('should not be called')
+    })
+
+    function handler (field, file, filename, encoding, mimetype) {
+      t.fail('should not be called')
+    }
+  })
+
+  fastify.listen(0, function () {
+    // request
+    const form = new FormData()
+    const opts = {
+      protocol: 'http:',
+      hostname: 'localhost',
+      port: fastify.server.address().port,
+      path: '/',
+      headers: form.getHeaders(),
+      method: 'POST'
+    }
+
+    const req = http.request(opts, (res) => {
+      t.equal(res.statusCode, 500)
+      res.resume()
+      res.on('end', () => {
+        t.pass('res ended successfully')
+      })
+    })
+    const rs = fs.createReadStream(filePath)
+    form.append('constructor', rs)
+    pump(form, req, function (err) {
+      t.error(err, 'client pump: no err')
+    })
+  })
+})

programming_language: JavaScript