Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

append crlf to formdata body #3625

Merged
merged 2 commits into from
Sep 20, 2024
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