Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/web/fetch/body.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,10 @@ function extractBody (object, keepalive = false) {
}
}

const chunk = textEncoder.encode(`--${boundary}--`)
// CRLF is appended to the body to function with legacy servers and match other implementations.
// https://github.com/curl/curl/blob/3434c6b46e682452973972e8313613dfa58cd690/lib/mime.c#L1029-L1030
// https://github.com/form-data/form-data/issues/63
const chunk = textEncoder.encode(`--${boundary}--\r\n`)
blobParts.push(chunk)
length += chunk.byteLength
if (hasUnknownSizeValue) {
Expand Down
29 changes: 29 additions & 0 deletions test/fetch/issue-3624.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict'

const assert = require('node:assert')
const { File } = require('node:buffer')
const { test } = require('node:test')
const { once } = require('node:events')
const { createServer } = require('node:http')
const { fetch, FormData } = require('../..')

// https://github.com/nodejs/undici/issues/3624
test('crlf is appended to formdata body (issue #3624)', async (t) => {
const server = createServer((req, res) => {
req.pipe(res)
}).listen(0)

t.after(server.close.bind(server))
await once(server, 'listening')

const fd = new FormData()
fd.set('a', 'b')
fd.set('c', new File(['d'], 'd.txt.exe'), 'd.txt.exe')

const response = await fetch(`http://localhost:${server.address().port}`, {
body: fd,
method: 'POST'
})

assert((await response.text()).endsWith('\r\n'))
})
Loading