Skip to content
Open
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
3 changes: 2 additions & 1 deletion src/plugins/multipart.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ function createInitMultipart(boundary) {
if (ctx.name === "partData") {
part.transferBuffer += ctx.buffer
.slice(ctx.start, ctx.end)
.toString("ascii");
.toString("ascii")
.replace(/[\r\n]/g, "");

/*
four bytes (chars) in base64 converts to three bytes in binary
Expand Down
33 changes: 33 additions & 0 deletions test/standalone/content-transfer-encoding.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { join } from "node:path";
import { createServer, request } from "node:http";
import { strictEqual } from "node:assert";
import { PassThrough, Writable } from "node:stream";

import formidable from "../../src/index.js";

Expand Down Expand Up @@ -60,3 +61,35 @@ test("content transfer encoding", (done) => {
req.end(body);
});
});

test("base64 transfer encoding ignores line breaks", async () => {
const body =
"--foo\r\n" +
'Content-Disposition: form-data; name="file"; filename="file"\r\n' +
"Content-Type: application/octet-stream\r\n" +
"Content-Transfer-Encoding: base64\r\n" +
"\r\n" +
"YWJj\r\nZGVm\r\nZ2hp\r\namts\r\n" +
"--foo--\r\n";
const req = new PassThrough();
req.headers = {
"content-type": "multipart/form-data; boundary=foo",
"content-length": Buffer.byteLength(body),
};
const chunks = [];
const form = formidable({
fileWriteStreamHandler: () =>
new Writable({
write(chunk, _, done) {
chunks.push(chunk);
done();
},
}),
});

const parsed = form.parse(req);
req.end(body);
await parsed;

strictEqual(Buffer.concat(chunks).toString(), "abcdefghijkl");
});