Skip to content

Commit

Permalink
append crlf to formdata body
Browse files Browse the repository at this point in the history
  • Loading branch information
KhafraDev committed Sep 19, 2024
1 parent c153875 commit c56460a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
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
28 changes: 28 additions & 0 deletions test/fetch/issue-3624.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict'

const assert = require('node:assert')
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')

Check failure on line 20 in test/fetch/issue-3624.js

View workflow job for this annotation

GitHub Actions / test (18, macos-latest) / Test with Node.js 18 on macos-latest

crlf is appended to formdata body (issue #3624)

[Error [ERR_TEST_FAILURE]: File is not defined] { failureType: 'testCodeFailure', cause: ReferenceError [Error]: File is not defined at TestContext.<anonymous> (/Users/runner/work/undici/undici/test/fetch/issue-3624.js:20:19) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async Test.run (node:internal/test_runner/test:632:9) at async startSubtest (node:internal/test_runner/harness:214:3), code: 'ERR_TEST_FAILURE' }

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

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

0 comments on commit c56460a

Please sign in to comment.