node-formidable / node-formidable/formidable

Multipart parsing does not ignore CRLF in base64 properly

Open Beginner friendly
#1,120 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
7.2k
Forks
689
Avg merge
3d 7h
Merged PRs (30d)
2

Description

Hi! 👋

Firstly, thanks for your work on this project! 🙂

I encountered problem with transferring files from an old legacy system.
This system sends multipart formdata with files being transfer encoded using base64.
Now some files were received fine using formidable, other did not.
It appears that when CRLF characters happen to be near chunk boundaries, they wont get stripped which will result in mangled file data.

I was able to reproduce the issue. And I can confirm that all test cases (16 files of different sizes) now work as expected.

Here is the diff that solved my problem:

diff --git a/node_modules/formidable/src/plugins/multipart.js b/node_modules/formidable/src/plugins/multipart.js
index eebd96a..1e68b3b 100644
--- a/node_modules/formidable/src/plugins/multipart.js
+++ b/node_modules/formidable/src/plugins/multipart.js
@@ -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

This issue body was partially generated by patch-package.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The relevant path shown is node_modules/formidable/src/plugins/multipart.js; inspect the multipart partData handling and reproduce the 16-file base64 cases, especially when CRLF falls across chunk boundaries. Done means those files decode without mangled data and the existing multipart parsing behavior remains intact.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
backend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
70/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.