From c56460ae2c40cfbe7f893be4806e92df969b7e33 Mon Sep 17 00:00:00 2001 From: Khafra Date: Thu, 19 Sep 2024 19:17:15 -0400 Subject: [PATCH 1/2] append crlf to formdata body --- lib/web/fetch/body.js | 5 ++++- test/fetch/issue-3624.js | 28 ++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 test/fetch/issue-3624.js diff --git a/lib/web/fetch/body.js b/lib/web/fetch/body.js index 683a67400c8..9de2a7e41b9 100644 --- a/lib/web/fetch/body.js +++ b/lib/web/fetch/body.js @@ -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) { diff --git a/test/fetch/issue-3624.js b/test/fetch/issue-3624.js new file mode 100644 index 00000000000..095976311d2 --- /dev/null +++ b/test/fetch/issue-3624.js @@ -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') + + const response = await fetch(`http://localhost:${server.address().port}`, { + body: fd, + method: 'POST' + }) + + assert((await response.text()).endsWith('\r\n')) +}) From d48754e879645fe3818aaded1baef86196b0bcf4 Mon Sep 17 00:00:00 2001 From: Khafra Date: Thu, 19 Sep 2024 19:41:07 -0400 Subject: [PATCH 2/2] fixup! v18 --- test/fetch/issue-3624.js | 1 + 1 file changed, 1 insertion(+) diff --git a/test/fetch/issue-3624.js b/test/fetch/issue-3624.js index 095976311d2..37e722d7e4e 100644 --- a/test/fetch/issue-3624.js +++ b/test/fetch/issue-3624.js @@ -1,6 +1,7 @@ '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')