Skip to content

Multipart parsing does not ignore CRLF in base64 properly#1120

Description

@FROGGS

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions