From 7c7416abf246f38b8e8ec27783b96323c572add3 Mon Sep 17 00:00:00 2001 From: Sebastien Date: Thu, 20 Aug 2015 02:20:18 +0900 Subject: [PATCH 1/3] Improved Content-Disposition parsing (added support for 'token' definition of the 'disp-extension-parm' rule, RFC 2616 section 19.5.1) --- lib/incoming_form.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/incoming_form.js b/lib/incoming_form.js index b4234456..9c2885da 100644 --- a/lib/incoming_form.js +++ b/lib/incoming_form.js @@ -352,7 +352,8 @@ IncomingForm.prototype._initMultipart = function(boundary) { headerField = headerField.toLowerCase(); part.headers[headerField] = headerValue; - var m = headerValue.match(/\bname="([^"]+)"/i); + // matches either a quoted-string or a token (RFC 2616 section 19.5.1) + var m = headerValue.match(/\bname=("([^"]+)"|([^\(\)<>@,;:\\"\/\[\]\?=\{\}\s\t/]+))/i); if (headerField == 'content-disposition') { if (m) { part.name = m[1]; @@ -421,7 +422,8 @@ IncomingForm.prototype._initMultipart = function(boundary) { }; IncomingForm.prototype._fileName = function(headerValue) { - var m = headerValue.match(/\bfilename="(.*?)"($|; )/i); + // matches either a quoted-string or a token (RFC 2616 section 19.5.1) + var m = headerValue.match(/\bfilename=("(.*?)"|([^\(\)<>@,;:\\"\/\[\]\?=\{\}\s\t/]+))($|;\s)/i); if (!m) return; var filename = m[1].substr(m[1].lastIndexOf('\\') + 1); From c2120e1f532864bc783a99f8f4b72bc487b1a88a Mon Sep 17 00:00:00 2001 From: Sebastien Date: Sun, 23 Aug 2015 16:24:04 +0900 Subject: [PATCH 2/3] Fixed quoted-string parsing regular expression for the "name" parameter of the Content-Disposition header (RFC 2616 section 2.2, a quoted-string can be empty) --- lib/incoming_form.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/incoming_form.js b/lib/incoming_form.js index 9c2885da..2d357c5d 100644 --- a/lib/incoming_form.js +++ b/lib/incoming_form.js @@ -353,7 +353,7 @@ IncomingForm.prototype._initMultipart = function(boundary) { part.headers[headerField] = headerValue; // matches either a quoted-string or a token (RFC 2616 section 19.5.1) - var m = headerValue.match(/\bname=("([^"]+)"|([^\(\)<>@,;:\\"\/\[\]\?=\{\}\s\t/]+))/i); + var m = headerValue.match(/\bname=("([^"]*)"|([^\(\)<>@,;:\\"\/\[\]\?=\{\}\s\t/]+))/i); if (headerField == 'content-disposition') { if (m) { part.name = m[1]; From af6bd44464b67c8199997e279283f09c244f4880 Mon Sep 17 00:00:00 2001 From: Sebastien Date: Wed, 2 Sep 2015 22:42:25 +0900 Subject: [PATCH 3/3] Fixed incorrect capturing groups --- lib/incoming_form.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/incoming_form.js b/lib/incoming_form.js index 2d357c5d..e3c8019a 100644 --- a/lib/incoming_form.js +++ b/lib/incoming_form.js @@ -356,7 +356,7 @@ IncomingForm.prototype._initMultipart = function(boundary) { var m = headerValue.match(/\bname=("([^"]*)"|([^\(\)<>@,;:\\"\/\[\]\?=\{\}\s\t/]+))/i); if (headerField == 'content-disposition') { if (m) { - part.name = m[1]; + part.name = m[2] || m[3] || ''; } part.filename = self._fileName(headerValue); @@ -426,7 +426,8 @@ IncomingForm.prototype._fileName = function(headerValue) { var m = headerValue.match(/\bfilename=("(.*?)"|([^\(\)<>@,;:\\"\/\[\]\?=\{\}\s\t/]+))($|;\s)/i); if (!m) return; - var filename = m[1].substr(m[1].lastIndexOf('\\') + 1); + var match = m[2] || m[3] || ''; + var filename = match.substr(match.lastIndexOf('\\') + 1); filename = filename.replace(/%22/g, '"'); filename = filename.replace(/&#([\d]{4});/g, function(m, code) { return String.fromCharCode(code);