Skip to content

Commit 44cffbe

Browse files
tsctxcrysmags
authored andcommitted
fix: check the content-type of invalid formData (nodejs#2541)
1 parent a82ec9a commit 44cffbe

File tree

2 files changed

+40
-5
lines changed

2 files changed

+40
-5
lines changed

lib/fetch/body.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const { FormData } = require('./formdata')
1414
const { kState } = require('./symbols')
1515
const { webidl } = require('./webidl')
1616
const { Blob, File: NativeFile } = require('buffer')
17-
const { kBodyUsed } = require('../core/symbols')
17+
const { kBodyUsed, kHeadersList } = require('../core/symbols')
1818
const assert = require('assert')
1919
const { isErrored } = require('../core/util')
2020
const { isUint8Array, isArrayBuffer } = require('util/types')
@@ -369,10 +369,12 @@ function bodyMixinMethods (instance) {
369369

370370
throwIfAborted(this[kState])
371371

372-
const contentType = this.headers.get('Content-Type')
372+
const contentType = this.headers[kHeadersList].get('content-type', true)
373+
374+
const mimeType = contentType !== null ? parseMIMEType(contentType) : 'failure'
373375

374376
// If mimeType’s essence is "multipart/form-data", then:
375-
if (/multipart\/form-data/.test(contentType)) {
377+
if (mimeType !== 'failure' && mimeType.essence === 'multipart/form-data') {
376378
const headers = {}
377379
for (const [key, value] of this.headers) headers[key] = value
378380

@@ -430,7 +432,7 @@ function bodyMixinMethods (instance) {
430432
await busboyResolve
431433

432434
return responseFormData
433-
} else if (/application\/x-www-form-urlencoded/.test(contentType)) {
435+
} else if (mimeType !== 'failure' && mimeType.essence === 'application/x-www-form-urlencoded') {
434436
// Otherwise, if mimeType’s essence is "application/x-www-form-urlencoded", then:
435437

436438
// 1. Let entries be the result of parsing bytes.

test/fetch/response.js

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
const { test } = require('tap')
44
const {
5-
Response
5+
Response,
6+
FormData
67
} = require('../../')
78
const {
89
Blob: ThirdPartyBlob,
@@ -254,3 +255,35 @@ test('Issue#2465', async (t) => {
254255
const response = new Response(new SharedArrayBuffer(0))
255256
t.equal(await response.text(), '[object SharedArrayBuffer]')
256257
})
258+
259+
test('Check the Content-Type of invalid formData', (t) => {
260+
t.plan(4)
261+
262+
t.test('_application/x-www-form-urlencoded', async (t) => {
263+
t.plan(1)
264+
const response = new Response('x=y', { headers: { 'content-type': '_application/x-www-form-urlencoded' } })
265+
await t.rejects(response.formData(), TypeError)
266+
})
267+
268+
t.test('_multipart/form-data', async (t) => {
269+
t.plan(1)
270+
const formData = new FormData()
271+
formData.append('x', 'y')
272+
const response = new Response(formData, { headers: { 'content-type': '_multipart/form-data' } })
273+
await t.rejects(response.formData(), TypeError)
274+
})
275+
276+
t.test('application/x-www-form-urlencoded_', async (t) => {
277+
t.plan(1)
278+
const response = new Response('x=y', { headers: { 'content-type': 'application/x-www-form-urlencoded_' } })
279+
await t.rejects(response.formData(), TypeError)
280+
})
281+
282+
t.test('multipart/form-data_', async (t) => {
283+
t.plan(1)
284+
const formData = new FormData()
285+
formData.append('x', 'y')
286+
const response = new Response(formData, { headers: { 'content-type': 'multipart/form-data_' } })
287+
await t.rejects(response.formData(), TypeError)
288+
})
289+
})

0 commit comments

Comments
 (0)